【问题标题】:NullPointerException in oncreateView() of FragmentFragment 的 oncreateView() 中的 NullPointerException
【发布时间】:2013-03-16 16:39:15
【问题描述】:

以下代码显示了添加片段的部分活动。


    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.fragment_container, srkt_frag);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    ft.commit();

    TabSpec specs1=mTab.newTabSpec("tag1");
    specs1.setContent(R.id.fragment_container);
    specs1.setIndicator("SRK/T");
    mTab.addTab(specs1);

mTab 是 TabHost,其中 srkt_frag 显示为内容。 片段类如下所示


public class Srkt_x extends Fragment 
{
    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            View view=inflater.inflate(R.layout.activity_srkt_x,container,false);
            TextView text = (TextView) getView().findViewById(R.id.srkt_power);
            Float result=getArguments().getFloat("RESULT");
            text.setText(String.valueOf(result));
            return view;

        }

}

执行项目后,我在第 21 行得到 nullpointerexception,即在

TextView text = (TextView) getView().findViewById(R.id.srkt_power);

我的xml文件如下所示


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"

tools:context=".Srkt_x" >

<ScrollView
    android:id="@+id/srkt_scrollView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:background="@color/Black" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="433dp" >

        <TextView
            android:id="@+id/srkt_power"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/iolpower"
            android:layout_toRightOf="@+id/iolpower"
            android:clickable="false"
            android:ems="10"
            android:enabled="false"
            android:fadingEdge="horizontal"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:gravity="center|left"
            android:longClickable="false"
            android:paddingTop="10dp"
            android:textColor="@color/DeepSkyBlue"
            android:textSize="15sp" />

        <TextView
            android:id="@+id/iolpower"
            android:layout_width="100dp"
            android:layout_height="30dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="20dp"
            android:gravity="bottom|center"
            android:textColor="@color/White"
            android:inputType="text"
            android:text="@string/power"
            android:textSize="20sp"
            android:visibility="visible" />

        <TableLayout
            android:layout_width="228dp"
            android:layout_height="match_parent"
            android:layout_below="@+id/srkt_power"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="33dp"
            android:paddingTop="10dp" >

            <TableRow
                android:id="@+id/srkttableRow1"
                android:layout_height="wrap_content"
                android:background="@color/SkyBlue"
                android:orientation="vertical"
                android:showDividers="middle" >

                <TextView
                    android:id="@+id/iolCol"
                    android:layout_height="wrap_content"
                    android:layout_gravity="left"
                    android:layout_weight="1"
                    android:background="@color/SkyBlue"
                    android:gravity="center"
                    android:text="@string/iol"
                    android:textColor="@color/White"
                    android:textSize="20sp" />

                <TextView
                    android:id="@+id/refCol"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:layout_weight="1"
                    android:background="@color/SkyBlue"
                    android:gravity="center"
                    android:text="@string/refr"
                    android:textColor="@color/White"
                    android:textSize="20sp" />
            </TableRow>

            <TableRow
                android:id="@+id/srkttableRow2"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@color/Black" >

                <TextView
                    android:id="@+id/srktrnd2"
                    android:layout_gravity="left|center"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:textColor="@color/White"
                    android:textSize="15sp" />

                <TextView
                    android:id="@+id/srktref2"
                    android:layout_gravity="right|center"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:textColor="@color/White"
                    android:textSize="15sp" />
            </TableRow>

            <TableRow
                android:id="@+id/srkttableRow3"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@color/Black" >

                <TextView
                    android:id="@+id/srktrnd3"
                    android:layout_gravity="left|center"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:textColor="@color/White"
                    android:textSize="15sp" />

                <TextView
                    android:id="@+id/srktref3"
                    android:layout_gravity="right|center"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:textColor="@color/White"
                    android:textSize="15sp" />
            </TableRow>

            <TableRow
                android:id="@+id/srkttableRow4"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@color/Black" >

                <TextView
                    android:id="@+id/srktrnd4"
                    android:layout_gravity="left|center"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:textColor="@color/White"
                    android:textSize="15sp" />

                <TextView
                    android:id="@+id/srktref4"
                    android:layout_gravity="right|center"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:textColor="@color/White"
                    android:textSize="15sp" />
            </TableRow>

            <TableRow
                android:id="@+id/srkttableRow5"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@color/Black" >

                <TextView
                    android:id="@+id/srktrnd5"
                    android:layout_gravity="left|center"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:textColor="@color/White"
                    android:textSize="15sp" />

                <TextView
                    android:id="@+id/srktref5"
                    android:layout_gravity="right|center"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:textColor="@color/White"
                    android:textSize="15sp" />
            </TableRow>

            <TableRow
                android:id="@+id/srkttableRow6"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@color/Black" >

                <TextView
                    android:id="@+id/srktrnd6"
                    android:layout_gravity="left|center"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:textColor="@color/White"
                    android:textSize="15sp" />

                <TextView
                    android:id="@+id/srktref6"
                    android:layout_gravity="right|center"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:textColor="@color/White"
                    android:textSize="15sp" />
            </TableRow>

            <TableRow
                android:id="@+id/srkttableRow7"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@color/Black" >

                <TextView
                    android:id="@+id/srktrnd7"
                    android:layout_gravity="left|center"
                    android:layout_weight="1"
                    android:background="@color/DeepSkyBlue"
                    android:gravity="center"
                    android:textColor="@color/White"
                    android:textSize="15sp" />

                <TextView
                    android:id="@+id/srktref7"
                    android:layout_gravity="right|center"
                    android:layout_weight="1"
                    android:background="@color/DeepSkyBlue"
                    android:gravity="center"
                    android:textColor="@color/White"
                    android:textSize="15sp" />
            </TableRow>

            <TableRow
                android:id="@+id/srkttableRow8"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@color/Black" >

                <TextView
                    android:id="@+id/srktrnd8"
                    android:layout_gravity="left|center"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:textColor="@color/White"
                    android:textSize="15sp" />

                <TextView
                    android:id="@+id/srktref8"
                    android:layout_gravity="right|center"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:textColor="@color/White"
                    android:textSize="15sp" />
            </TableRow>

            <TableRow
                android:id="@+id/srkttableRow9"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@color/Black" >

                <TextView
                    android:id="@+id/srktrnd9"
                    android:layout_gravity="left|center"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:textColor="@color/White"
                    android:textSize="15sp" />

                <TextView
                    android:id="@+id/srktref9"
                    android:layout_gravity="right|center"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:textColor="@color/White"
                    android:textSize="15sp" />
            </TableRow>

            <TableRow
                android:id="@+id/srkttableRow10"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@color/Black" >

                <TextView
                    android:id="@+id/srktrnd10"
                    android:layout_gravity="left|center"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:textColor="@color/White"
                    android:textSize="15sp" />

                <TextView
                    android:id="@+id/srktref10"
                    android:layout_gravity="right|center"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:textColor="@color/White"
                    android:textSize="15sp" />
            </TableRow>

            <TableRow
                android:id="@+id/srkttableRow11"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@color/Black" >

                <TextView
                    android:id="@+id/srktrnd11"
                    android:layout_gravity="left|center"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:textColor="@color/White"
                    android:textSize="15sp" />

                <TextView
                    android:id="@+id/srktref11"
                    android:layout_gravity="right|center"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:textColor="@color/White"
                    android:textSize="15sp" />
            </TableRow>

            <TableRow
                android:id="@+id/srkttableRow12"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@color/Black" >

                <TextView
                    android:id="@+id/srktrnd12"
                    android:layout_gravity="left|center"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:textColor="@color/White"
                    android:textSize="15sp" />

                <TextView
                    android:id="@+id/srktref12"
                    android:layout_gravity="right|center"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:textColor="@color/White"
                    android:textSize="15sp" />
            </TableRow>
        </TableLayout>

    </RelativeLayout>
</ScrollView>


日志文件如下所示


   03-26 23:49:22.333: D/AndroidRuntime(497): Shutting down VM
03-26 23:49:22.333: W/dalvikvm(497): threadid=1: thread exiting with uncaught exception (group=0x40015560)
03-26 23:49:22.353: E/AndroidRuntime(497): FATAL EXCEPTION: main
03-26 23:49:22.353: E/AndroidRuntime(497): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.iolcalci/com.example.iolcalci.Menu}: java.lang.NullPointerException
03-26 23:49:22.353: E/AndroidRuntime(497):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-26 23:49:22.353: E/AndroidRuntime(497):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-26 23:49:22.353: E/AndroidRuntime(497):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-26 23:49:22.353: E/AndroidRuntime(497):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-26 23:49:22.353: E/AndroidRuntime(497):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-26 23:49:22.353: E/AndroidRuntime(497):  at android.os.Looper.loop(Looper.java:123)
03-26 23:49:22.353: E/AndroidRuntime(497):  at android.app.ActivityThread.main(ActivityThread.java:3683)
03-26 23:49:22.353: E/AndroidRuntime(497):  at java.lang.reflect.Method.invokeNative(Native Method)
03-26 23:49:22.353: E/AndroidRuntime(497):  at java.lang.reflect.Method.invoke(Method.java:507)
03-26 23:49:22.353: E/AndroidRuntime(497):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-26 23:49:22.353: E/AndroidRuntime(497):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-26 23:49:22.353: E/AndroidRuntime(497):  at dalvik.system.NativeStart.main(Native Method)
03-26 23:49:22.353: E/AndroidRuntime(497): Caused by: java.lang.NullPointerException
03-26 23:49:22.353: E/AndroidRuntime(497):  at com.example.iolcalci.Srkt_x.onCreateView(Srkt_x.java:21)
03-26 23:49:22.353: E/AndroidRuntime(497):  at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
03-26 23:49:22.353: E/AndroidRuntime(497):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
03-26 23:49:22.353: E/AndroidRuntime(497):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
03-26 23:49:22.353: E/AndroidRuntime(497):  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
03-26 23:49:22.353: E/AndroidRuntime(497):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
03-26 23:49:22.353: E/AndroidRuntime(497):  at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:551)
03-26 23:49:22.353: E/AndroidRuntime(497):  at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
03-26 23:49:22.353: E/AndroidRuntime(497):  at android.app.Activity.performStart(Activity.java:3791)
03-26 23:49:22.353: E/AndroidRuntime(497):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1620)
03-26 23:49:22.353: E/AndroidRuntime(497):  ... 11 more

我无法找出问题。任何解决方案?

编辑: 第 22 行:Float 结果=getArguments().getFloat("RESULT");

编辑 2: 设置参数如下所示:

IOLPower_srkt=Srkt();

                Bundle srkt_bundle=new Bundle();
                srkt_bundle.putFloat("RESULT", IOLPower_srkt);

                srkt_frag.setArguments(srkt_bundle);

这里 Srkt() 返回浮点值 查看详细代码


IOLPower_srkt=Srkt();
            Bundle srkt_bundle=new Bundle();
            srkt_bundle.putFloat("RESULT", IOLPower_srkt);
            srkt_frag.setArguments(srkt_bundle);



            IOLPower_bink=Binkhorst();
            Bundle bink_bundle=new Bundle();
            bink_bundle.putFloat("RESULT", IOLPower_bink);
            bink_frag.setArguments(bink_bundle);


            IOLPower_srk2=Srk2();
            if((Math.ceil(IOLPower_srk2)-IOLPower_srk2)>0.5){
                srk2_rnd=(float) Math.floor(IOLPower_srk2);
            }else{
                srk2_rnd=(float) Math.ceil(IOLPower_srk2);
            }
            Bundle srk2_bundle=new Bundle();
            srk2_bundle.putFloat("RESULT", srk2_rnd);
            srk2_frag.setArguments(srk2_bundle);



            IOLPower_holl=Holladay();
            Bundle holl_bundle=new Bundle();
            holl_bundle.putFloat("RESULT", IOLPower_holl);
            holl_frag.setArguments(holl_bundle);

        }
            });

    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.fragment_container, srkt_frag);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    ft.commit();

    TabSpec specs1=mTab.newTabSpec("tag1");
    specs1.setContent(R.id.fragment_container);
    specs1.setIndicator("SRK/T");
    mTab.addTab(specs1);


    TabSpec specs2=mTab.newTabSpec("tag2");
    specs2.setContent(R.id.fragment_container);
    specs2.setIndicator("SRK II");
    mTab.addTab(specs2);


    TabSpec specs3=mTab.newTabSpec("tag3");
    specs3.setContent(R.id.fragment_container);
    specs3.setIndicator("HOLLADAY");
    mTab.addTab(specs3);


    TabSpec specs4=mTab.newTabSpec("tag4");
    specs4.setContent(R.id.fragment_container);
    specs4.setIndicator("BINKHORST");
    mTab.addTab(specs4);

【问题讨论】:

    标签: android android-fragments nullpointerexception android-tabhost


    【解决方案1】:

    如何解决:你的日志线索是Caused by: java.lang.NullPointerException at com.example.iolcalci.Srkt_x.onCreateView(Srkt_x.java:21)

    这表示Srkt_x.java line 21 处有一个Null Pointer Exception,因此转到第 21 行,查看哪些值等于 null 并抛出异常。

    您的代码的问题在于TextView text = (TextView) getView().findViewById(R.id.srkt_power);,其中getView()null,因为它尚未创建。

    正确的代码是:

    public class Srkt_x extends Fragment 
    {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View view=inflater.inflate(R.layout.activity_srkt_x,container,false);
    
            // use view instead of getView()
            TextView text = (TextView) view.findViewById(R.id.srkt_power);
            if(getArguments() != null) {
                Float result=getArguments().getFloat("RESULT");
                text.setText(String.valueOf(result));
            } else {
                text.setText("result not included");
            }
            return view;
    
        }
    
    }
    

    您必须使用膨胀视图才能通过 id 获取所需的 TextView。

    onCreateView 的末尾,当return view; 执行时,getView() 将返回view 值而不是null


    Fragment 的 Android 文档页面中所建议,请参阅 TitlesFragmentDetailsFragment,使用类似于以下代码的内容来设置参数

    public static Srkt_x newInstance(Float result) {
        Srkt_x f = new Srkt_x ();
    
        // Supply index input as an argument.
        Bundle args = new Bundle();
        args.putFloat("RESULT", result);
        f.setArguments(args);
    
        return f;
    }
    

    【讨论】:

    • 查看我在代码中的修改,现在您应该在文本视图中看到result not included,直到您解决缺少参数的问题
    • 在您发布的代码中没有调用 setArguments 方法,请查看 android 开发人员如何使用片段搜索 setArguments/getArguments developer.android.com/reference/android/app/Fragment.html
    【解决方案2】:

    尝试在方法onViewCreated() 中执行您需要执行的操作,您应该覆盖该方法。别忘了给super()打电话。

    【讨论】:

      【解决方案3】:

      你的bug和困惑的原因是不了解how the Android event loop works.

      您正在调用getView(),此时该片段还没有一个。该片段只有在onCreateView 完成运行后才会有视图。 onCreateView 的重点是“让片段实例化它的用户界面视图”。

      你应该做getActivity().findViewById

      【讨论】:

        【解决方案4】:

        不要使用getView(),而是使用您刚刚膨胀的视图,因为该视图尚未返回,您将获得一个 NPE 可能是这样的:

        View view=inflater.inflate(R.layout.activity_srkt_x,container,false);
        TextView text = (TextView) view.findViewById(R.id.srkt_power);
        
        //*** do something***
        
        return view;
        

        【讨论】:

        • 很抱歉,从现在开始会这样做:)
        • 没问题,只是一个小提示:)
        • 您能指出代码中的哪一行是 22 吗?您应该发布整个代码,这对我们来说会更容易。
        • 我可以看看你是如何设置参数的吗?在发布的代码中看不到。
        • 尝试在片段的 newInstance 构造函数中设置参数。 Android 建议这样做。
        【解决方案5】:

        替换

        TextView text = (TextView) getView().findViewById(R.id.srkt_power);
        

        TextView text = (TextView) getActivity().findViewById(R.id.srkt_power);
        

        【讨论】:

          猜你喜欢
          • 2013-02-18
          • 1970-01-01
          • 1970-01-01
          • 2017-09-22
          • 2014-09-26
          • 1970-01-01
          • 2019-04-08
          • 1970-01-01
          相关资源
          最近更新 更多