gpt4 book ai didi

java - 使用抽屉导航时尝试从空对象引用的字段 'int android.support.v4.app.Fragment.mContainerId' 读取

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:39:12 25 4
gpt4 key购买 nike

下面是我在使用抽屉导航进行导航时在 Android Studio 中遇到的错误的 StackTrace。这是一个新错误,因为我以前没有发生过。所以我不确定如果我改变了什么。

08-11 23:16:37.823 15951-15951/com.example.aids.a09application
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.aids.a09application, PID: 15951
java.lang.NullPointerException: Attempt to read from field 'int android.support.v4.app.Fragment.mContainerId' on a null object reference
at android.support.v4.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1017)
at android.support.v4.app.FragmentTransition.calculateFragments(FragmentTransition.java:976)
at android.support.v4.app.FragmentTransition.startTransitions(FragmentTransition.java:95)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2146)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:710)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6692)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)

下面是 Java 类,其中我有我的抽屉导航的 fragment 事务。

public class MainActivity extends AppCompatActivity {
DrawerLayout drawerLayout;
Toolbar toolbar;
ActionBarDrawerToggle actionBarDrawerToggle;
FragmentTransaction fragmentTransaction;
NavigationView navigationView;
Intent intent;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close);

drawerLayout.addDrawerListener(actionBarDrawerToggle);
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.main_container, new HomeFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle("Drift Fan");
navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.Home:
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container, new HomeFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle("Home Fragment");
item.setCheckable(true);
drawerLayout.closeDrawers();
break;
case R.id.my_account:
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container, new myAccountFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle("My Account");
item.setCheckable(true);
drawerLayout.closeDrawers();
break;
case R.id.nav_about:
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container, new AboutDriftingFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle("About Drifting");
item.setCheckable(true);
drawerLayout.closeDrawers();
break;
case R.id.nav_shop:
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container, new ShopFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle("Shop");
item.setCheckable(true);
drawerLayout.closeDrawers();
break;
case R.id.nav_news:
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container, new NewsFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle("News");
item.setCheckable(true);
drawerLayout.closeDrawers();
break;
case R.id.nav_media:
intent = new Intent(MainActivity.this, media_main.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
MainActivity.this.startActivity(intent);
break;
case R.id.nav_results:
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container, new ResultsFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle("Results");
item.setCheckable(true);
drawerLayout.closeDrawers();
break;

}


return false;
}

});

}

@Override
protected void onPostCreate(@Nullable Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
actionBarDrawerToggle.syncState();
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.drawer_menu, menu);
return true;



}
}

最佳答案

我遇到了同样的问题,但是当我从 v4.app.Fragment 类扩展时。而不是 android.app.Fragment,它工作正常,没有崩溃。

另见 this它提供了一些您可以尝试的解决方案。

关于java - 使用抽屉导航时尝试从空对象引用的字段 'int android.support.v4.app.Fragment.mContainerId' 读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45644553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com