在Android中实现Fragments时出错

时间:2014-04-06 17:39:54

标签: android android-fragments

我正在开发一个Android应用程序。我想像UI这样的Playstore,所以我把那个水平的抽屉放进去,把我所有的活动都当作碎片。现在,当我打开我的活动时,它会崩溃,意味着“不幸的应用程序停止了”。

日志中的消息:

04-06 17:24:07.616: E/AndroidRuntime(1059): FATAL EXCEPTION: main
04-06 17:24:07.616: E/AndroidRuntime(1059): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bingo.main/com.bingo.main.Start}: java.lang.NullPointerException
04-06 17:24:07.616: E/AndroidRuntime(1059):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-06 17:24:07.616: E/AndroidRuntime(1059):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-06 17:24:07.616: E/AndroidRuntime(1059):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-06 17:24:07.616: E/AndroidRuntime(1059):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-06 17:24:07.616: E/AndroidRuntime(1059):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-06 17:24:07.616: E/AndroidRuntime(1059):     at android.os.Looper.loop(Looper.java:137)
04-06 17:24:07.616: E/AndroidRuntime(1059):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-06 17:24:07.616: E/AndroidRuntime(1059):     at java.lang.reflect.Method.invokeNative(Native Method)
04-06 17:24:07.616: E/AndroidRuntime(1059):     at java.lang.reflect.Method.invoke(Method.java:511)
04-06 17:24:07.616: E/AndroidRuntime(1059):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-06 17:24:07.616: E/AndroidRuntime(1059):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-06 17:24:07.616: E/AndroidRuntime(1059):     at dalvik.system.NativeStart.main(Native Method)
04-06 17:24:07.616: E/AndroidRuntime(1059): Caused by: java.lang.NullPointerException
04-06 17:24:07.616: E/AndroidRuntime(1059):     at com.bingo.main.Recruitment.onCreateView(Recruitment.java:21)
04-06 17:24:07.616: E/AndroidRuntime(1059):     at android.app.Fragment.performCreateView(Fragment.java:1695)
04-06 17:24:07.616: E/AndroidRuntime(1059):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
04-06 17:24:07.616: E/AndroidRuntime(1059):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
04-06 17:24:07.616: E/AndroidRuntime(1059):     at android.app.BackStackRecord.run(BackStackRecord.java:682)
04-06 17:24:07.616: E/AndroidRuntime(1059):     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
04-06 17:24:07.616: E/AndroidRuntime(1059):     at android.app.Activity.performStart(Activity.java:5113)
04-06 17:24:07.616: E/AndroidRuntime(1059):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)
04-06 17:24:07.616: E/AndroidRuntime(1059):     ... 11 more

如果删除以下语句,它可以工作但没有片段:

 FragmentManager fragmentManager = getFragmentManager(); 
 FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
 fragmentTransaction.replace(R.id.content_frame, fragment);
 fragmentTransaction.commit();`

请帮帮我!

1 个答案:

答案 0 :(得分:0)

正如Marco所说,你有一个名为Recruitment的类,在它的onCreateView()方法中有一个Null PointerException - 具体看第21行。尝试调试程序并找出什么是null - 可能是fragmentManager本身或者在这里行:

fragmentTransaction.replace(R.id.content_frame, fragment);

您可能还没有实例化“片段”。