片段和标签

Fragments and tabs

我的小米应用有问题。首先,我构建了 3 个带有操作栏和片段的选项卡,并且仅与文本视图完美配合。但是当我开始开发我的应用程序时,我发现了一个问题,当我更改选项卡时,应用程序停止了。我认为我需要暂停听众或类似的东西,但我没有找到任何相关信息。你怎么看?

这是第一个标签代码:

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

    }
};
}

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

view.setOnClickListener(null);

我找到了解决办法,让听众听none。

@覆盖 public void onPause() {

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

}

谢谢!