我有一個活動,它通過採用者類實例化和填充片段。我想傳遞值/對象到這個片段採用者類,以便我可以口授佈局等。如何將參數傳遞到活動的片段
我試過使用setArgument方法傳遞包,但我的代碼炸彈出來,不知道爲什麼。據我所知,我可以在採用者類的onCreate方法中收集該包。我看到參數在註釋代碼之後立即被正確設置,但是之後它崩潰了,我不明白爲什麼。如果我刪除了捆綁代碼,它不會崩潰。 :(
下面是我的代碼,沒有任何人知道我應該把包代碼,註釋掉?
public class LoginScreen extends ActionBarActivity {
private final String ARG_SELECTED_LAYOUT_ID = "selectedLayoutId";
private final int DEFAULT_LAYOUT = R.layout.layout_list;
private int mSelectedLayoutId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
mSelectedLayoutId = DEFAULT_LAYOUT;
if (savedInstanceState != null) {
mSelectedLayoutId = savedInstanceState.getInt(ARG_SELECTED_LAYOUT_ID);
}
addLayoutTab(
actionBar, R.layout.layout_list, R.drawable.ic_list, "list");
addLayoutTab(
actionBar, R.layout.layout_grid, R.drawable.ic_grid, "grid");
addLayoutTab(
actionBar, R.layout.layout_staggered_grid, R.drawable.ic_staggered, "staggered");
addLayoutTab(
actionBar, R.layout.layout_spannable_grid, R.drawable.ic_spannable, "spannable");
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(ARG_SELECTED_LAYOUT_ID, mSelectedLayoutId);
}
private void addLayoutTab(ActionBar actionBar, int layoutId, int iconId, String tag) {
ActionBar.Tab tab = actionBar.newTab()
.setText("")
.setIcon(iconId)
.setTabListener(new TabListener(layoutId, tag));
actionBar.addTab(tab, layoutId == mSelectedLayoutId);
}
public class TabListener implements ActionBar.TabListener {
private LayoutFragment mFragment;
private final int mLayoutId;
private final String mTag;
public TabListener(int layoutId, String tag) {
mLayoutId = layoutId;
mTag = tag;
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
mFragment = (LayoutFragment) getSupportFragmentManager().findFragmentByTag(mTag);
if (mFragment == null) {
mFragment = (LayoutFragment) LayoutFragment.newInstance(mLayoutId);
ft.add(R.id.content, mFragment, mTag);
} else {
ft.attach(mFragment);
}
/*
Bundle bundle = new Bundle();
bundle.putInt("noTiles", 4);
mFragment.setArguments(bundle);
*/
mSelectedLayoutId = mFragment.getLayoutId();
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
if (mFragment != null) {
ft.detach(mFragment);
}
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
}
}
logcat的
03-03 11:56:53.675 32507-32507/weebuns.predictionking E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: weebuns.predictionking, PID: 32507
java.lang.RuntimeException: Unable to start activity ComponentInfo{weebuns.predictionking/weebuns.predictionking.LoginScreen}: java.lang.NullPointerException: Attempt to invoke virtual method 'void weebuns.predictionking.LayoutFragment.setArguments(android.os.Bundle)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void weebuns.predictionking.LayoutFragment.setArguments(android.os.Bundle)' on a null object reference
您使用未註釋的代碼獲得的logcat錯誤是什麼? – 2015-03-03 10:35:15
添加logcat :) – Fearghal 2015-03-03 11:04:19
當您創建實例時發送包:mFragment =(LayoutFragment)LayoutFragment.newInstance(mLayoutId,YourBundle); – 2015-03-03 11:11:48