2

我在安卓屏幕上有一条错误消息。这是Logcat -

02-08 09:33:42.471: E/AndroidRuntime(11592): java.lang.RuntimeException: Unable to start receiver something.MainActivity$CancelReceiver: java.lang.NullPointerException

为什么程序在错误日志后正常工作?(程序没有被杀死)当它第一次抛出广播时,就会出现这个错误。

这是我的代码的一部分

public static class CancelReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intents) {

            intents.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            String OKorNOT = intents.getExtras().getString("OKorNOT");
            String str = intents.getExtras().getString("something");

            if(OKorNOT.equals("OK")){
                //OKの場合

                tv2.setText(str);
                //this line may throws error.tv means TextView object findview already.
            }else if(OKorNOT.equals("NOT")){
                //NOTの場合
                tv.setText("yeah");//this line also throws error
                pushB.setText("NO!");   
                tv2.setText("");
            }

怎么了?

编辑。这个 onReceive 在静态类中。

和关于电视的oncreate,tv2是

public class MainActivity extends Activity {

   static TextView tv;
   static TextView tv2;
   static Button pushB;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

         pushB = (Button)findViewById(R.id.button2);
         tv = (TextView)findViewById(R.id.textView6);
         tv2 = (TextView)findViewById(R.id.textView7);
}
}

我确实在 onCreate 中定义了对象。

4

0 回答 0