Android 片段的oncreateView()中的NullPointerException

Android 片段的oncreateView()中的NullPointerException,android,android-fragments,nullpointerexception,android-tabhost,Android,Android Fragments,Nullpointerexception,Android Tabhost,下面的代码显示了添加片段的部分活动 mTab是一个TabHost,其中srkt_frag显示为内容。 fragment类如下所示 执行项目后,我在第21行得到nullpointerexception,即 TextView text = (TextView) getView().findViewById(R.id.srkt_power); 我的xml文件如下所示 我想不出这个问题,有什么解决办法吗 编辑: 第22行:Float result=getArguments().getFlo

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



mTab是一个TabHost,其中srkt_frag显示为内容。 fragment类如下所示



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

TextView text = (TextView) getView().findViewById(R.id.srkt_power);
我的xml文件如下所示



我想不出这个问题,有什么解决办法吗

编辑: 第22行:Float result=getArguments().getFloat(“结果”)

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

IOLPower_srkt=Srkt();

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

                srkt_frag.setArguments(srkt_bundle);

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



更换

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

不要使用刚刚膨胀的视图,因为视图尚未返回,您将得到一个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;

如何解决:日志的线索是由java.lang.NullPointerException引起的

在com.example.iolcalic.Srkt_x.onCreateView(Srkt_x.java:21)

这表示在
Srkt_x.java第21行存在
Null指针异常
,因此转到第21行,查看哪个值等于
Null
,并抛出异常

代码的问题在于
TextView text=(TextView)getView()其中
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的末尾时,
getView()
将返回
view
值,而不是
null


如Android文档页面中所建议的,请参见
TitlesFragment
DetailsFragment
,使用类似下面的代码来设置参数

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

错误和混乱的原因是由于不理解

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


您应该执行
getActivity()。findViewById

尝试在方法
onViewCreated()
中执行您需要执行的操作,您应该覆盖该方法。别忘了在上面调用
super()

很抱歉,从现在开始就可以了:)没问题,只是一点提示:)您能指出代码中哪一行是22吗?你应该发布整个代码,这对我们来说更容易。我能看看你是如何设置你的参数的吗??无法在发布的代码中看到。请尝试在片段的newInstance构造函数中设置参数。Android建议这样做。查看我在代码中的修改,现在您应该会在文本视图中看到
结果未包含
,直到您解决了所发布代码中缺少参数的问题,没有调用
setArguments
方法查看Android开发人员如何使用片段搜索setArguments/getArguments
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);
TextView text = (TextView) getView().findViewById(R.id.srkt_power);
TextView text = (TextView) getActivity().findViewById(R.id.srkt_power);
View view=inflater.inflate(R.layout.activity_srkt_x,container,false);
TextView text = (TextView) view.findViewById(R.id.srkt_power);

//*** do something***

return view;
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;

    }

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