片段和标签

时间:2015-03-16 14:55:05

标签: android android-fragments android-tabs

我的mi app有问题。首先,我使用操作栏和片段构建3个选项卡,并且只使用textview完美地工作。但是当我开始开发我的应用程序时,我发现了一个问题,当我更改选项卡时,应用程序停止了。我认为我需要暂停听众或类似的东西,但我没有找到任何关于它的东西。你觉得怎么样?

这是第一个标签代码:

public class DriveFragment extends Fragment {

TelephonyManager        Tel;
int a;
MyPhoneStateListener    MyListener;
MyPhoneLocationListener MyLocListener;

@Override
public View onCreateView(LayoutInflater inflater,
        ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.drivexml, container, false);


    return v;
}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}
@Override
public void onActivityCreated(Bundle state) {
    super.onActivityCreated(state);

    TextView texto1 = (TextView) getView().findViewById(R.id.textView1);


    Tel       = ( TelephonyManager )getActivity().getSystemService(Context.TELEPHONY_SERVICE);
    a = Tel.getNetworkType();
    if (a==1 || a==2){
     texto1.setText("TECNOLOGÍA: GSM");
    }
    else
     texto1.setText("TECNOLOGÍA: UMTS");
    MyListener   = new MyPhoneStateListener();
    MyLocListener   = new MyPhoneLocationListener();
    Tel.listen(MyListener ,PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
    Tel.listen(MyLocListener ,PhoneStateListener.LISTEN_CELL_LOCATION);

    }
private class MyPhoneStateListener extends PhoneStateListener
{
     @Override
     /* Get the Signal strength from the provider, each tiome there is an update */
     public void onSignalStrengthsChanged(final SignalStrength signalStrength)
     {

       super.onSignalStrengthsChanged(signalStrength);
       TextView texto2 = (TextView) getView().findViewById(R.id.textView2);

       int b = signalStrength.getGsmSignalStrength();

       if(a == 2 || a == 1){
           texto2.setText("RSSI [dBm]: "+String.valueOf(2*b - 113));

       }
       else{
            texto2.setText("RSCP [dBm]: "+String.valueOf(b - 116));

       }


     }
};
private class MyPhoneLocationListener extends PhoneStateListener
{

    @Override
    public void onCellLocationChanged(CellLocation location)
    {

        super.onCellLocationChanged(location);
        TextView texto3 = (TextView) getView().findViewById(R.id.textView3);
        TextView texto4 = (TextView) getView().findViewById(R.id.textView4);
        TextView texto5 = (TextView) getView().findViewById(R.id.textView5);

        GsmCellLocation c = (GsmCellLocation)Tel.getCellLocation();

        if(a==1||a==2){
            texto3.setText("LAC: " + c.getLac());
            texto4.setText("CELLID: " + c.getCid());

        }
        else{
            texto3.setText("LAC: " + c.getLac());
            texto4.setText("CELLID: " + c.getCid());
            texto5.setText("RNC: " + c.getCid()/65536);
        }

    }
};
}

2 个答案:

答案 0 :(得分:0)

如果你需要暂停监听器,只需将null传递给你想要暂停的视图监听器:

view.setOnClickListener(null);

答案 1 :(得分:0)

我找到了解决方案,让听众听不到。

@覆盖     public void onPause(){

    super.onPause();
    Tel.listen(MyListener ,PhoneStateListener.LISTEN_NONE);
    Tel.listen(MyLocListener ,PhoneStateListener.LISTEN_NONE);

}

谢谢!