【问题标题】:java.lang.NullPointerException - .onCreate - Attempt to invoke virtual method on a null object referencejava.lang.NullPointerException - .onCreate - 尝试在空对象引用上调用虚拟方法
【发布时间】:2020-07-14 11:27:27
【问题描述】:

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

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>

【问题讨论】:

  • 贴出你的全部代码
  • 你可能在找到视图之前错过了 setcontentview()
  • @Wini 我已经做到了,亲爱的
  • @TheCodersCompany 你的代码我运行我的android studio..它工作正常..onclick 按钮它带我去下一个活动......在你的manifest 中声明EmbroderyTheoryActivity
  • @Wini 是的.. 我已经做到了...等一下,看看我会添加清单代码

标签: android android-studio android-layout android-fragments


【解决方案1】:

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

以下代码对我有用

 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();
        }
    });}}

【讨论】:

  • startActivity(new Intent(getApplicationContext(), EmbroderyTheoryActivity.class));你用getapplicationContext()代替HomePage.this????@TheCodersCompany
  • 我忘了告诉你我使用的是片段页面而不是普通页面
  • 表示EmbroderyTheoryActivity.class 是一个片段页面/类? @TheCodersCompany
  • 是的,它是.. 我创建了一个 Menu Activity,并且自己创建了片段,并在这些片段中放置了布局
  • @TheCodersCompany 我更新了答案.... 替换 clicklisterner 上的框架布局
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-07
  • 2020-09-17
  • 2015-04-20
  • 2016-07-30
  • 2015-08-17
  • 2015-02-09
  • 1970-01-01
相关资源
最近更新 更多