1

新しい SMS を受信するたびにメイン アクティビティに警告するブロードキャスト レシーバーがあり、新しい SMS を受信するたびにアプリケーションがクラッシュします。

Logcat は次のように述べています。

10-14 15:56:03.465: E/AndroidRuntime(27068): FATAL EXCEPTION: main
10-14 15:56:03.465: E/AndroidRuntime(27068): java.lang.RuntimeException: Unable to start receiver com.testapp.SMSReceiver: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

エラーの原因と思われるレシーバーからのコードブロックを次に示します。

Intent passSMStoMain = new Intent(context, MainActivity.class);
                passSMStoMain.putExtra("SENDER", msg_from);
                passSMStoMain.putExtra("MESSAGE", msgBody);
                context.startActivity(passSMStoMain);//this line makes the app crash

このエラーを修正するにはどうすればよいですか?

4

2 に答える 2

0

getApplicationContext() を試す

Intent passSMStoMain = new Intent(context, MainActivity.class);
passSMStoMain.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
passSMStoMain.putExtra("SENDER", msg_from);
passSMStoMain.putExtra("MESSAGE", msgBody);
context.getApplicationContext().startActivity(passSMStoMain);
于 2013-10-14T15:59:28.260 に答える
0

これを試して:

Intent passSMStoMain = new Intent(context, MainActivity.class);
passSMStoMain.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
passSMStoMain.putExtra("SENDER", msg_from);
passSMStoMain.putExtra("MESSAGE", msgBody);
context.startActivity(passSMStoMain);
于 2013-10-14T15:51:20.370 に答える