我創建了一個導航類型爲「tabs + swipe」的新活動。我作出這些變化爲getItem()方法:執行橫向導航時應用崩潰
@Override
public Fragment getItem(int i) {
Fragment fragment ;
switch(i){
case 0:
fragment = new Fragment1();
break;
case 1:
fragment = new Fragment2();
break;
case 3:
fragment = new Fragment3();
break;
//
default : return null;
}
//set args if necessary
//return fragment
return fragment;
}
所以爲每個標籤我定義一個單獨的類。一類是這樣的:
public static class Fragment1 extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public Fragment1() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Create a new TextView and set its text to the fragment's section
// number argument value.
View v = inflater.inflate(R.layout.center, null);
return v;
}
}
當我運行新的活動啓動成功,但是當我滑動屏幕的第二個選項卡,然後應用程序崩潰......這是logcat的應用程序:
02-16 08:48:43.328: E/AndroidRuntime(21541): FATAL EXCEPTION: main
02-16 08:48:43.328: E/AndroidRuntime(21541): java.lang.NullPointerException
02-16 08:48:43.328: E/AndroidRuntime(21541): at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:394)
02-16 08:48:43.328: E/AndroidRuntime(21541): at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:389)
02-16 08:48:43.328: E/AndroidRuntime(21541): at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:99)
02-16 08:48:43.328: E/AndroidRuntime(21541): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:801)
02-16 08:48:43.328: E/AndroidRuntime(21541): at android.support.v4.view.ViewPager.populate(ViewPager.java:992)
02-16 08:48:43.328: E/AndroidRuntime(21541): at android.support.v4.view.ViewPager.populate(ViewPager.java:881)
02-16 08:48:43.328: E/AndroidRuntime(21541): at android.support.v4.view.ViewPager$3.run(ViewPager.java:237)
02-16 08:48:43.328: E/AndroidRuntime(21541): at android.os.Handler.handleCallback(Handler.java:605)
02-16 08:48:43.328: E/AndroidRuntime(21541): at android.os.Handler.dispatchMessage(Handler.java:92)
02-16 08:48:43.328: E/AndroidRuntime(21541): at android.os.Looper.loop(Looper.java:137)
02-16 08:48:43.328: E/AndroidRuntime(21541): at android.app.ActivityThread.main(ActivityThread.java:4575)
02-16 08:48:43.328: E/AndroidRuntime(21541): at java.lang.reflect.Method.invokeNative(Native Method)
02-16 08:48:43.328: E/AndroidRuntime(21541): at java.lang.reflect.Method.invoke(Method.java:511)
02-16 08:48:43.328: E/AndroidRuntime(21541): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
02-16 08:48:43.328: E/AndroidRuntime(21541): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
02-16 08:48:43.328: E/AndroidRuntime(21541): at dalvik.system.NativeStart.main(Native Method)
請解決我的問題。
找不到這個logcat的東西.....你忘了'case 2'嗎? – 2013-02-16 03:46:27
你需要提供更多的代碼。有幾個地方可能會導致NullPointerException,但您的代碼不足以識別它。 – StarPinkER 2013-02-16 03:47:38
哦!對不起,我..其實我忘了案例2 ...我真的很抱歉,這個問題.. – omerjerk 2013-02-16 03:58:43