代码神秘地运行两次后,TextsView为null

时间:2018-09-20 21:15:19

标签: java android

我创建了一个可以发送和接收文本并在textView中显示文本的应用程序。该应用程序可以在更早的时候完美运行,我似乎看不到为什么发生了什么变化,但是现在我在设置TextsView文本时得到了NPE。有趣的是,尽管它显示为非null并正确设置,然后由于某些未知原因再次运行相同的代码,但这一次textview为null。

我知道NPE是什么,为什么会发生。我只需要帮助找到代码为什么运行两次,以及为什么第二次运行textsview为null即可。

Textsview在类的顶部定义,然后在OnCreateView中设置。

班级:

public class Rel1 extends Fragment {
    TextView textsView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.rel1, container, false);
        getContext().registerReceiver(smsReceiver, new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));
        textsView = view.findViewById(R.id.texts1);
        return view;
    }

public BroadcastReceiver smsReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        SmsMessage[] msgs = null;
        String str = "";
        String num = null, msg = null;
        if (bundle != null) {
            //---retrieve the SMS message received---
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];
            for (int i = 0; i < msgs.length; i++) {
                msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                num = msgs[i].getOriginatingAddress();
                msg = msgs[i].getMessageBody();
            }
            //---display the new SMS message---
            Log.d("SmsReceiver", "onReceive: Message Received = " + msg + " msgs = " + msgs + " bundle = " + bundle);
            Log.d("textsview", "onReceive: can SmsReceiver find texts view?" + textsView);
            setMsg(msg, num, textsView);
        }
    }
};
public void setMsg(String msg, String num, TextView textsView) {
        Log.d("SetMsg: ", "setMsg: msg = " + msg + " num = " + num);
        Log.d("texts view", "setMsg: is textsview existing? : " + textsView);
        if (textsView != null) {

            Log.d("setMSG", "setMsg: Function called" + msg);
            if (num.equals(txtPhoneNo.getText().toString()) || convNum(num).equals(txtPhoneNo.getText().toString())) {
                Log.d("setmsg: ", "setMsg: num checks out");
                if (msg.equals("REL 1 OFF==REL 2 OFF")) {
                    Log.d("Parse: ", "setMsg: REL 1 OFF==REL 2 OFF");
                    h = true;
                    w = true;
                } else if (msg.equals("REL 1 ON==REL 2 OFF")) {
                        Log.d("Parse: ", "setMsg: REL 1 ON==REL 2 OFF");
                        h = false;
                        w = true;
                    } else if (msg.equals("REL 1 OFF==REL 2 ON")) {
                            Log.d("Parse: ", "setMsg: REL 1 OFF==REL 2 ON");
                            h = true;
                            w = false;
                        } else if (msg.equals("REL 1 ON==REL 2 ON")) {
                                Log.d("Parse: ", "setMsg: REL 1 ON==REL 2 ON");
                                h = false;
                                w = false;
                            } else {
                                Toast.makeText(getContext().getApplicationContext(),
                                "Check Number! Incorrect Message Recieved!",
                                Toast.LENGTH_LONG).show();
                            }
            }

            sendNotification(h, w);
            if(h && w) {
                textsView.setText(R.string.hON_wON);
                //((EditText)view2.findViewById(R.id.texts1)).setText(R.string.hON_wON);
            } else if (h && !w) {
                textsView.setText(R.string.hON_wOFF);
                //((EditText)view2.findViewById(R.id.texts1)).setText(R.string.hON_wOFF);
            } else if (!h && w) {
                textsView.setText(R.string.hOFF_wON);
                //((EditText)view2.findViewById(R.id.texts1)).setText(R.string.hOFF_wON);
            } else if (!h && !w) {
                textsView.setText(R.string.hOFF_wOFF);
                //((EditText)view2.findViewById(R.id.texts1)).setText(R.string.hOFF_wOFF);
            }
        }

    }
    @Override
    public void onPause() {
        getContext().unregisterReceiver(smsReceiver);
        super.onPause();
    }

    @Override
    public void onDestroy() {
        getContext().unregisterReceiver(smsReceiver);
        super.onDestroy();
    }

    @Override
    public void onResume() {
        getContext().registerReceiver(smsReceiver, new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));
        super.onResume();
    }
从正确的号码发送正确的消息时,

Logcat: 请注意,代码如何运行两次,并且textsview在那里运行一次,之后就消失了

09-20 21:56:50.571 11037-11037/com.mypackage.myprogram D/SmsReceiver: onReceive: Message Received = REL 1 OFF==REL 2 ON msgs = [Landroid.telephony.SmsMessage;@5bd4de0 bundle = Bundle[{format=3gpp, pdus=[[B@a949899, slot=0, phone=0, rTime=1537477010526, subscription=1, upload_flag=1}]
09-20 21:56:50.571 11037-11037/com.mypackage.myprogram D/textsview: onReceive: can SmsReceiver find texts view?android.support.v7.widget.AppCompatTextView{2156868 V.ED..... ........ 0,628-720,834 #7f0800b8 app:id/texts1}
09-20 21:56:50.572 11037-11037/com.mypackage.myprogram D/SetMsg:: setMsg: msg = REL 1 OFF==REL 2 ON num = ###NUMBER OMMITTED
09-20 21:56:50.572 11037-11037/com.mypackage.myprogram D/texts view: setMsg: is textsview existing? : android.support.v7.widget.AppCompatTextView{2156868 V.ED..... ........ 0,628-720,834 #7f0800b8 app:id/texts1}
09-20 21:56:50.572 11037-11037/com.mypackage.myprogram D/setMSG: setMsg: Function calledREL 1 OFF==REL 2 ON
09-20 21:56:50.594 11037-11037/com.mypackage.myprogram D/ActivityThread: BDC-Calling finishReceiver: IIntentReceiver=38d2555
09-20 21:56:50.635 11037-11037/com.mypackage.myprogram D/SmsReceiver: onReceive: Message Received = REL 1 OFF==REL 2 ON msgs = [Landroid.telephony.SmsMessage;@936fb6a bundle = Bundle[{format=3gpp, pdus=[[B@aff885b, slot=0, phone=0, rTime=1537477010526, subscription=1, upload_flag=1}]
09-20 21:56:50.636 11037-11037/com.mypackage.myprogram D/textsview: onReceive: can SmsReceiver find texts view?null
09-20 21:56:50.636 11037-11037/com.mypackage.myprogram D/SetMsg:: setMsg: msg = REL 1 OFF==REL 2 ON num = ###NUMBER OMMITTED
09-20 21:56:50.636 11037-11037/com.mypackage.myprogram D/texts view: setMsg: is textsview existing? : null

任何人都可以给出一个原因,使它从一条消息中运行两次,并且第二次textsView为空吗?

1 个答案:

答案 0 :(得分:0)

知道了。在版本2中,我将“文本”视图设置为R.id.textsview,该视图不存在,我要做的就是将其更改为R.id.texts1。