当我运行项目时,应用程序崩溃,并且打印出我的错误是NullPointerException
,它是测试功能的参考。下面是与问题有关的类。
Tab1Values.java
public class Tab1Values extends Fragment {
TextView tv1,tv2,tv3;
int value=0,dbm=0;
String ptype="",ntype="";
public Tab1Values() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragment_tab1, container, false);
tv1=(TextView)v.findViewById(R.id.info);
tv2=(TextView)v.findViewById(R.id.info2);
tv3=(TextView)v.findViewById(R.id.info3);
TelephonyManager tm=(TelephonyManager)getActivity().getSystemService(Context.TELEPHONY_SERVICE);
->tm.listen(new Tab1Signal2(this.getActivity()), PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
return v;
}
public void data(int value1,int value2){
tv2.setText("GsmBitErrorRate:"+value1+" \nGsmSignalStrength:"+value2);
}
public void test(int LteSignalStrength,int LteRsrp,int LteRsrq,int LteRssnr,int LteCqi){
tv3.setText("\nLteSignalStrength:"+dbm+" dbm"+
"\nLteRsrp: "+LteRsrp+
"\nLteRsrq: "+LteRsrq+
"\nLteRssnr: "+LteRssnr+
"\nLteCqi: "+ LteCqi);
}
}
Tab1Signal2.java
public class Tab1Signal2 extends PhoneStateListener {
Context mcontext;
int LteSignalStrength=0,LteRsrp=0,LteRsrq=0,LteRssnr=0,LteCqi=0;
int value1=0,value2=0,CdmaDbm=0,CdmaEcio=0,EvdoDbm=0,EvdoEcio=0,EvdoSnr=0;
//int cid=0,lac=0,psc=0,statid=0,netid=0,sysid=0,lat=0,lon=0;
String error,ss,val1,ccloc;
private Tab1Values main;
->public Tab1Signal2(Context context){
mcontext=context;
//main=(Tab1Values) mcontext;
}
public void onSignalStrengthsChanged(SignalStrength signalStrength){
super.onSignalStrengthsChanged(signalStrength);
if (signalStrength.isGsm()) {
value1=signalStrength.getGsmBitErrorRate();
//error="\nGsmBitErrorRate:"+value1;
value2=signalStrength.getGsmSignalStrength();
//ss="\nGsmSignalStrength:"+value2;
//main.data(value1,value2);
}else if (signalStrength.getCdmaDbm() > 0) {
CdmaDbm=signalStrength.getCdmaDbm();
CdmaEcio=signalStrength.getCdmaEcio();
} else {
EvdoDbm=signalStrength.getEvdoDbm();
EvdoEcio=signalStrength.getEvdoEcio();
EvdoSnr=signalStrength.getEvdoSnr();
}
try {
Method[] methods = android.telephony.SignalStrength.class
.getMethods();
for (Method mthd : methods) {
if (mthd.getName().equals("getLteSignalStrength")){
//val1=mthd.getName() ;
LteSignalStrength=(Integer)mthd.invoke(signalStrength);
//main.test(val2);
}
if (mthd.getName().equals("getLteRsrp")){
LteRsrp=(Integer)mthd.invoke(signalStrength);
}
if (mthd.getName().equals("getLteRsrq")){
LteRsrq=(Integer)mthd.invoke(signalStrength);
}
if (mthd.getName().equals("getLteRssnr")){
LteRssnr=(Integer)mthd.invoke(signalStrength);
}
if (mthd.getName().equals("getLteCqi")){
LteCqi=(Integer)mthd.invoke(signalStrength);
}
LINE 59 main.test(LteSignalStrength,LteRsrp,LteRsrq,LteRssnr,LteCqi);
}
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
我认为问题在于上下文构造函数在2个类之间的通信。在Tab1Signal2
构造函数中,我注释了一行,并将其删除,因为它显示了错误,并且更具体地讲:{{ 1}}我在必须纠正的线条之前放了一个箭头,但找不到正确的方法。
如果有人知道请帮忙。谢谢