首页 > 解决方案 > java.lang.NullPointerException - .onCreate - 尝试在空对象引用上调用虚拟方法

问题描述

我正在创建一个课程应用程序,当我尝试通过单击一个按钮打开现有活动时,它给了我这个错误:

java.lang.NullPointerException: Attempt to invoke virtual method 
android.view.View android.widget.Button.findViewById(int)' on a null object 
reference at com.lorenzotribuiani.tailorin.HomePage.onCreate```

有人知道为什么吗?

这是java代码:

/* code for creating a new Activity by clicking the btn */
    Button btnEmbrodery = findViewById(R.id.btn_embrodery);
    btnEmbrodery.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(HomePage.this,EmbroderyTheoryActivity.class));
        }
    });

我真的不知道为什么会这样……

这是关于java文件的完整代码:

public class HomePage extends AppCompatActivity {

private AppBarConfiguration mAppBarConfiguration;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_page);

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);



 /* code for creating a new Activity by clicking the btn */
    Button btnEmbrodery = findViewById(R.id.btn_embrodery);
    btnEmbrodery.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(HomePage.this,EmbroderyTheoryActivity.class));
        }
    });

DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
        R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
        .setDrawerLayout(drawer)
        .build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);

    Intent intent = getIntent();
    boolean logged = intent.getBooleanExtra("isLogged", false);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu, menu);
    return true;
}

@Override public boolean onSupportNavigateUp() {
     NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    return NavigationUI.navigateUp(navController, mAppBarConfiguration)
             || super.onSupportNavigateUp();  } }

AndroidManifest 代码:

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@mipmap/logo"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/logo_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">


 <activity
        android:name=".EmbroderyTheoryActivity"
        android:label="@string/title_activity_embrodery_theory"
        android:theme="@style/AppTheme.NoActionBar"></activity>
<activity
    android:name=".Registration"
    android:theme="@style/FirstRunLogin" />
<activity
    android:name=".FirstRun_Login"
    android:theme="@style/FirstRunLogin" />
<activity
    android:name=".HomePage"
    android:theme="@style/AppTheme.NoActionBar" />
<activity
    android:name=".SplashScreen"
    android:theme="@style/SplashScreen">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

标签: androidandroid-studioandroid-layoutandroid-fragments

解决方案


只需运行以下代码并注释所有其余代码,然后运行

以下代码对我有用

 public class MainActivity  extends AppCompatActivity {


//  private AppBarConfiguration mAppBarConfiguration;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_page);

    // Toolbar toolbar = findViewById(R.id.toolbar);
    // setSupportActionBar(toolbar);

    /* code for creating a new Activity by clicking the btn */
    Button btnEmbrodery = findViewById(R.id.btn_embrodery)
      Btnembrodery. Setonclickliner{......
    Fragment fragment = CustomFragment.newInstance();

     FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

    ft.replace(R.id.container, fragmentObject);
    ft.commit();
        }
    });}}

推荐阅读