这是错误 logcat :
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 :(得分:1)
首先,由于Title是一个变量,所以它应该写成小写。
问题是TextView为null,因为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;
}