gpt4 book ai didi

java - 该应用程序无法打开,并且会在 log cat 中给出 java.lang.NullPointerException,有什么解决办法吗?

转载 作者:行者123 更新时间:2023-12-02 01:02:38 25 4
gpt4 key购买 nike

它会给出这个错误

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference at com.example.calculator.tab1.onCreateView(tab1.java:91)

logcat 显示错误位于第 91 行的 tab1 中,其中包含 btn0=getView().findViewById(R.id.btn0);

public class tab1 extends Fragment {
// ... Code ...
Button btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btnclear,btnback,btnpercent,btndiv,btnmul,btnmin,btnplus,btnequal,btndot,btn0,btnch;
Button btnsin,btncos,btntan,btnxy,btnlg,btnin,btnbro,btnbrc,btnroot,btnnot,btndiv1,btnpi,btnsinin,btncosin,btntanin;
TextView tvin,tvout;
JsEvaluator jsEvaluator = new JsEvaluator(getActivity());

String proc;
String pro1;
Double ans;
FloatingActionButton fab;
BottomSheetBehavior bottomSheetBehavior;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

btn0=getView().findViewById(R.id.btn0);
btn1=getView().findViewById(R.id.btn1);
btn2=getView().findViewById(R.id.btn2);
btn3=getView().findViewById(R.id.btn3);
btn4=getView().findViewById(R.id.btn4);
btn5=getView().findViewById(R.id.btn5);
btn6=getView().findViewById(R.id.btn6);
btn7=getView().findViewById(R.id.btn7);
btn8=getView().findViewById(R.id.btn8);
btn9=getView().findViewById(R.id.btn9);
btnback=getView().findViewById(R.id.btnback);
btnch=getView().findViewById(R.id.btnch);
btnclear=getView().findViewById(R.id.btnclear);
btnpercent=getView().findViewById(R.id.btnpercent);
btnmul=getView().findViewById(R.id.btnmul);
btnmin=getView().findViewById(R.id.btnmin);
btnplus=getView().findViewById(R.id.btnpus);
btndot=getView().findViewById(R.id.btndot);
btndiv=getView().findViewById(R.id.btndiv);
btnequal=getView().findViewById(R.id.btneql);
tvin=getView().findViewById(R.id.textView2);
tvout=getView().findViewById(R.id.textView);
btnsin=getView().findViewById(R.id.btnsin);
btncos=getView().findViewById(R.id.btncos);
btntan=getView().findViewById(R.id.btntan);
btnxy=getView().findViewById(R.id.btnxy);
btnlg=getView().findViewById(R.id.btnlg);
btnroot=getView().findViewById(R.id.btnroot);
btnnot=getView().findViewById(R.id.btnnot);
btndiv1=getView().findViewById(R.id.btn1by);
btnpi=getView().findViewById(R.id.btnpi);
btnsinin=getView().findViewById(R.id.btnsinin);
btncosin=getView().findViewById(R.id.btncosin);
btntanin=getView().findViewById(R.id.btntanin);
View bttm =getView().findViewById(R.id.btoom);
bottomSheetBehavior =BottomSheetBehavior.from(bttm);
fab=getView().findViewById(R.id.floating_action_button);

btnbrc=getView().findViewById(R.id.btnbrc);
btnbro=getView().findViewById(R.id.btnbro);
btnin=getView().findViewById(R.id.btnin);



// ... Event listener code ...

// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_tab1, container, false);
}
}

我是 android studio 的新手,如果你能提供帮助,那就太好了

最佳答案

您应该首先创建 View 层次结构,然后再请求 View 。因此,onCreateView 方法应如下所示:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {


View v = inflater.inflate(R.layout.fragment_tab1, container, false);

btn0 = v.findViewById(R.id.btn0); /// use created View here
btn1 = v.findViewById(R.id.btn1);

// and so on...


// return created view
return v;
}

关于java - 该应用程序无法打开,并且会在 log cat 中给出 java.lang.NullPointerException,有什么解决办法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60469372/

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