android - 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>
解决方案
只需运行以下代码并注释所有其余代码,然后运行
以下代码对我有用
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();
}
});}}
推荐阅读
- node.js - 使用 Webpack 在生产环境中将资产拆分为捆绑包
- liferay - 如何从 portlet 检索数据到 portal_normal.flt - Liferay
- python - Python 正则表达式替换所有模式,除非它位于重复模式旁边
- c# - 使用组合框中的选定文本更改 sql 查询
- git - 事后如何更改创建分支的位置
- python - 人脸检测,不是人脸检测
- python - 抑制 pweave 中的警告
- javascript - 当字段不可访问时,不会触发 HTML5 验证
- asp.net-core - 如何为不同的数据库配置具有不同 url/端口的 ASP.NET Core 2 应用程序
- reactjs - 类型上不存在属性“onRemove”