fragment.onCreateView causes null pointer exceptio

2019-02-18 15:14发布

So i am using fragments and trying to wire on click listeners on them.

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

This is the XML for the button

 <Button
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            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"/>

This is my Exception

02-28 16:49:57.162 14375-14375/com.example.listviewandroid E/AndroidRuntime﹕ FATAL

EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.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)

Its obvious its not able to find button or something is happening which i am not able to figure out. Any help greatly appreciated.

3条回答
唯我独甜
2楼-- · 2019-02-18 15:54

You cannot use getView() until onCreateView() returns since it is the method in charge of creating said view. You should change your code to

public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View contentView = inflater.inflate(R.layout.layout1, null);
        startButton = (Button) contentView.findViewById(R.id.button);

Or alternatively, override onViewCreated() and implement your Button setup in that method (using getView().

查看更多
劳资没心,怎么记你
3楼-- · 2019-02-18 16:00

simply use this in your onCreateView

    View contentView = inflater.inflate(R.layout.layout1, container, false);
    startButton= (Button) contentView.findViewById(R.id.button);
    return contentView;
查看更多
家丑人穷心不美
4楼-- · 2019-02-18 16:03

This line makes no sense:

startButton= (Button) getView().findViewById(R.id.button);

You don't have a View yet, you are still creating it..

In stead you can do this:

 startButton= (Button)contentView.findViewById(R.id.button);

Or wait until the onCreateView method returns the actual View you just created and then you will be able to get a reference of the view using the "getView" method after onCreateView returns the view..

Hope it Helps!

Regards!

查看更多
登录 后发表回答