2014-01-09 22 views
0

此使用額外的意圖是錯誤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

+0

搜索'TextView'(這我假設是片段的一部分)在'onCreateView()'方法中膨脹,而不是使用'getActivity()'。 – Luksprog

回答

1

起初由於標題是一個變量,它應該寫成小寫。 問題在於TextView爲空,因爲getActivity().findViewById(R.id.tvTitle)每次都返回null。

相反,你必須拯救你的碎片onCreateView()方法的過程中所需要的意見,喜歡這裏:你使用的視圖

private TextView textView; 

... 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.yourlayout, container, false); 
    textView = (TextView) v.findViewById(R.id.textViewId); 

    return v; 
} 
+0

textview是從xml –

+0

你在哪裏膨脹的XML?你是否覆蓋片段中的onCreateView()? – Endzeit

+0

你的第二個答案是對的,我應該用v。不是getactivity。謝謝。 –