Я создаю приложение курса, и когда я пытаюсь открыть существующее действие, щелкнув кнопку, он выдает следующую ошибку:
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>