gpt4 book ai didi

android - 在 FragmentTab 中使用额外 Intent 时出错

转载 作者:行者123 更新时间:2023-11-30 03:10:42 25 4
gpt4 key购买 nike

这是错误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

我以前可以很好地使用代码,但我改用我的应用程序中的选项卡导致了问题。

这是解决问题的代码:

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 行

最佳答案

首先Title是一个变量,应该写成小写。问题在于 TextView 为 null,因为 getActivity().findViewById(R.id.tvTitle) 每次都返回 null。

相反,您必须在 Fragments onCreateView() 方法期间保存所需的 View ,如下所示:

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;
}

关于android - 在 FragmentTab 中使用额外 Intent 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21029355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com