I have this problem as I said at title with the above code.The error it is at the line that I have the arrow.Probably the problem is because of the extends Fragment
because this code before I put it there it was at an Activity and it works fine.Please tell me what to change.
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 Tab1Signal(this.getActivity().getApplicationContext()), 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);
}
Tab1Signal.java
public class Tab1Signal 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 Tab1Signal(Context context){
mcontext=context;
->main=(Tab1Values) mcontext;
}
public void onSignalStrengthsChanged(SignalStrength signalStrength){
super.onSignalStrengthsChanged(signalStrength);
if (signalStrength.isGsm()) {
value1=signalStrength.getGsmBitErrorRate();
value2=signalStrength.getGsmSignalStrength();
}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);
}
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();
}
}
}
Thank you
That's because Activity
is a subclass of Context
, and Fragment
is not. One easy way to fix this is to change the constructor of your Tab1Signal
to take in Tab1Values
as parameter:
public Tab1Signal(Tab1Values fragment){
mcontext = fragment.getContext();
main = fragment;
}