fragment.onCreateView 导致空指针异常
Posted
技术标签:
【中文标题】fragment.onCreateView 导致空指针异常【英文标题】:fragment.onCreateView causes null pointer exception 【发布时间】:2014-04-02 07:23:49 【问题描述】:所以我正在使用片段并尝试在它们上连接点击侦听器。
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View contentView = inflater.inflate(R.layout.layout1, null);
// View contentView=super.onCreateView(inflater,null,savedInstanceState,R.layout.layout1);
startButton= (Button) getView().findViewById(R.id.button);
这是按钮的 XML
<Button
android:layout_
android:layout_
android:text="Start Time"
android:id="@+id/button"
android:layout_alignParentLeft="true"
android:layout_marginLeft="28dp"
android:layout_marginTop="10dp"
android:background="#1e497c"
android:textColor="#ffffff"/>
这是我的例外
02-28 16:49:57.162 14375-14375/com.example.listviewandroid E/AndroidRuntime: 致命
EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.listviewandroid/com.example.listviewandroid.MainActivity: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.access$700(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:175)
at android.app.ActivityThread.main(ActivityThread.java:5279)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.fragment.Fragment1.onCreateView(Fragment1.java:43)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:556)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1181)
at android.app.Activity.performStart(Activity.java:5293)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2218)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.access$700(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:175)
at android.app.ActivityThread.main(ActivityThread.java:5279)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
很明显它无法找到按钮或发生了我无法弄清楚的事情。非常感谢任何帮助。
【问题讨论】:
【参考方案1】:只需在您的 onCreateView
中使用它
View contentView = inflater.inflate(R.layout.layout1, container, false);
startButton= (Button) contentView.findViewById(R.id.button);
return contentView;
【讨论】:
【参考方案2】:在onCreateView()
返回之前,您不能使用getView()
,因为它是负责创建所述视图的方法。您应该将代码更改为
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View contentView = inflater.inflate(R.layout.layout1, null);
startButton = (Button) contentView.findViewById(R.id.button);
或者,覆盖onViewCreated()
并在该方法中实现您的按钮设置(使用getView()
。
【讨论】:
【参考方案3】:这行没有意义:
startButton= (Button) getView().findViewById(R.id.button);
您还没有视图,您仍在创建它..
相反,您可以这样做:
startButton= (Button)contentView.findViewById(R.id.button);
或者等到 onCreateView 方法返回你刚刚创建的实际视图,然后你就可以在 onCreateView 返回视图后使用“getView”方法获取视图的引用了..
希望对你有帮助!
问候!
【讨论】:
以上是关于fragment.onCreateView 导致空指针异常的主要内容,如果未能解决你的问题,请参考以下文章
是啥导致 Visual Studio 2017 中的文件名为空
为啥空字符串上的 .ToString() 会导致空错误,而 .ToString() 在具有空值的可空 int 上工作正常?