Androidの画面にエラーメッセージが表示されました。これが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でオブジェクトを定義しました。