此使用額外的意圖是錯誤logcat的:錯誤在FragmentTab
01-09 21:44:27.461: E/AndroidRuntime(25935): FATAL EXCEPTION: main
01-09 21:44:27.461: E/AndroidRuntime(25935): java.lang.RuntimeException: Unable to start activity ComponentInfo{xx.xx.xx/xx.xx.xx.Mainclass}: java.lang.NullPointerException
01-09 21:44:27.461: E/AndroidRuntime(25935): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
01-09 21:44:27.461: E/AndroidRuntime(25935): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
01-09 21:44:27.461: E/AndroidRuntime(25935): at android.app.ActivityThread.access$600(ActivityThread.java:153)
01-09 21:44:27.461: E/AndroidRuntime(25935): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
01-09 21:44:27.461: E/AndroidRuntime(25935): at android.os.Handler.dispatchMessage(Handler.java:99)
01-09 21:44:27.461: E/AndroidRuntime(25935): at android.os.Looper.loop(Looper.java:137)
01-09 21:44:27.461: E/AndroidRuntime(25935): at android.app.ActivityThread.main(ActivityThread.java:5227)
01-09 21:44:27.461: E/AndroidRuntime(25935): at java.lang.reflect.Method.invokeNative(Native Method)
01-09 21:44:27.461: E/AndroidRuntime(25935): at java.lang.reflect.Method.invoke(Method.java:511)
01-09 21:44:27.461: E/AndroidRuntime(25935): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
01-09 21:44:27.461: E/AndroidRuntime(25935): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
01-09 21:44:27.461: E/AndroidRuntime(25935): at dalvik.system.NativeStart.main(Native Method)
01-09 21:44:27.461: E/AndroidRuntime(25935): Caused by: java.lang.NullPointerException
01-09 21:44:27.461: E/AndroidRuntime(25935): at xx.xx.xx.FragmentTab1.onCreateView(FragmentTab1.java:52)
01-09 21:44:27.461: E/AndroidRuntime(25935): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
01-09 21:44:27.461: E/AndroidRuntime(25935): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
01-09 21:44:27.461: E/AndroidRuntime(25935): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
01-09 21:44:27.461: E/AndroidRuntime(25935): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
01-09 21:44:27.461: E/AndroidRuntime(25935): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
01-09 21:44:27.461: E/AndroidRuntime(25935): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:551)
01-09 21:44:27.461: E/AndroidRuntime(25935): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1164)
01-09 21:44:27.461: E/AndroidRuntime(25935): at android.app.Activity.performStart(Activity.java:5114)
01-09 21:44:27.461: E/AndroidRuntime(25935): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2271)
01-09 21:44:27.461: E/AndroidRuntime(25935): ... 11 more
我以前用的代碼很好,但我交換我的應用程序引起的問題,使用標籤。
這是解決該問題的code
:
Bundle extras = getActivity().getIntent().getExtras();
TextView Title = (TextView) getActivity().findViewById(R.id.tvTitle);
if (extras != null) {
userName = extras.getString("title");
Title.setText(userName);
}
Title.setText(userName);
是線52。
搜索'TextView'(這我假設是片段的一部分)在'onCreateView()'方法中膨脹,而不是使用'getActivity()'。 – Luksprog