Fragment getArguments() return 空指针异常
Fragment getArguments() return null pointer exception
片段getArguments()
return空指针异常。
已经连续一天思考如何修复此代码,整个互联网都在寻找答案。请帮我。代码喜欢忠实的,但由于某种原因崩溃 NullPointerException?
P.S。抱歉我的英语不好,但我想你明白我想问什么。))
//片段
public static AppsManagerFragment userFragList(int a){
AppsManagerFragment f = new AppsManagerFragment();
Bundle bundle = new Bundle();
bundle.putInt("secretKey", a);
f.setArguments(bundle);
return f;
};
public static AppsManagerFragment systemFragList(int a){
AppsManagerFragment f = new AppsManagerFragment();
Bundle bundle = new Bundle();
bundle.putInt("secretKey", a);
f.setArguments(bundle);
return f;
};
int getFragmentArg() {
return this.getArguments().getInt("secretKey"); // here there NPE
}
//Activity
private FragmentTabHost mTabHost;
private DataApps dApps;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.apps_manager_activity);
dApps = new DataApps(this);
// instance initialization
AppsManagerFragment userFrag = AppsManagerFragment.userFragList(1);
AppsManagerFragment systemFrag = AppsManagerFragment.systemFragList(2);
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Tab1"), userFrag.getClass(), null);
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Tab2"), systemFrag.getClass(), null);
}
Fragment getArguments() return null pointer exception:
从您的代码中,我可以理解您正在返回片段的静态实例,但是在 getFragmentArg()
方法中您正在访问片段的非静态实例。实际上,您应该遵循 findFragmentByTag()
方法,然后在该实例上使用 getArguments()
。它会解决你的问题。
片段getArguments()
return空指针异常。
已经连续一天思考如何修复此代码,整个互联网都在寻找答案。请帮我。代码喜欢忠实的,但由于某种原因崩溃 NullPointerException?
P.S。抱歉我的英语不好,但我想你明白我想问什么。))
//片段
public static AppsManagerFragment userFragList(int a){
AppsManagerFragment f = new AppsManagerFragment();
Bundle bundle = new Bundle();
bundle.putInt("secretKey", a);
f.setArguments(bundle);
return f;
};
public static AppsManagerFragment systemFragList(int a){
AppsManagerFragment f = new AppsManagerFragment();
Bundle bundle = new Bundle();
bundle.putInt("secretKey", a);
f.setArguments(bundle);
return f;
};
int getFragmentArg() {
return this.getArguments().getInt("secretKey"); // here there NPE
}
//Activity
private FragmentTabHost mTabHost;
private DataApps dApps;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.apps_manager_activity);
dApps = new DataApps(this);
// instance initialization
AppsManagerFragment userFrag = AppsManagerFragment.userFragList(1);
AppsManagerFragment systemFrag = AppsManagerFragment.systemFragList(2);
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Tab1"), userFrag.getClass(), null);
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Tab2"), systemFrag.getClass(), null);
}
Fragment getArguments() return null pointer exception:
从您的代码中,我可以理解您正在返回片段的静态实例,但是在 getFragmentArg()
方法中您正在访问片段的非静态实例。实际上,您应该遵循 findFragmentByTag()
方法,然后在该实例上使用 getArguments()
。它会解决你的问题。