2016-11-26 4 views
-4

このエラーを修正するにはどうすればよいですか?アンドロイドのNullPointerException:コンテキストを設定する方法?

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference 
ACTIVITY.onNewIntent(Adult1Activity.java:243) 

これは、このエラーを含む方法である:

private void ExtendedNotification(String time) { 
    Intent resultIntent = new Intent(this, Adult1Activity.class); 

    resultIntent.putExtra("A", "restore"); 
    PendingIntent restoreIntent = PendingIntent.getActivity(Adult1Activity.this, 0, resultIntent, 
      PendingIntent.FLAG_UPDATE_CURRENT); 

    resultIntent.putExtra("A", "close"); 
    PendingIntent closeIntent = PendingIntent.getActivity(Adult1Activity.this, 2, resultIntent, 
      PendingIntent.FLAG_UPDATE_CURRENT); 

    final NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(Adult1Activity.this) 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setContentTitle("App Name") 
      .setContentText(time) 
      .setAutoCancel(true) 
      .addAction(new NotificationCompat.Action(R.mipmap.ic_launcher, "Restore", restoreIntent)) 
      .addAction(new NotificationCompat.Action(R.mipmap.ic_launcher, "Close", closeIntent)) 
      .setContentIntent(restoreIntent); 

    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

    Notification notification = notificationBuilder.build(); 
    notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT; 

    notificationManager.notify(0, notification); 
} 
@Override 
protected void onNewIntent(Intent intent) { 
    switch (intent.getStringExtra("A")) { 
     case "restore": 
      tv.setText(timeString); 
      break; 

     case "close": 
      countDownTimer.cancel(); 
      isRunning = false; 
      notificationManager.cancel(0); 
      CloseApp(); 
      break; 
    } 

私は、問題はそれでコンテキストを使用していないと思います。

私のアプリにStopBTNのカウンターがあり、2つのボタンで通知を作成します。それらの1つは、ACTIVITYクラスを示すRestoreBTNです。 StopBTNをクリックすると、このエラーが表示されます。

EDIThandleIntentメソッドにNullPointerエラーがあります。

+0

[NullPointerExceptionがある、と私はそれをどのように修正すればよいか?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-の可能性のある重複i-fix-it) – sushildlh

+0

'intent'の代わりに' getIntent() 'を使います....... – sushildlh

+0

@sushildlh。それは動作していません! –

答えて

1

誤った言語でタイプされたAのように見えるタイプミスなどを避けるために、余分なキーに定数を設定することができます。また、それはアクション

private static final String EXTRA_A = "A"; 
private static final String A_RESTORE = "restore"; 
private static final String A_CLOSE = "close"; 

の定数を持って良いことだ私は、あなたがしなければ分からないが、手動でonNewIntent()を呼び出すことはありません。また、通知をクリックしたときにアクティビティが破棄された場合は、onNewIntent()が呼び出されないことがあります。クラッシュがあなたのことを意味し

@Override 
protected void onCreate(Bundle b) { 
    super.onCreate(b); 
    // init views and stuff, and in final onCreate line handle the intent 
    handleIntent(getIntent()); 
} 

@Override 
protected void onNewIntent(Intent intent) { 
    super.onNewIntent(intent); // Make sure to call super 
    handleIntent(intent); 
} 

)あなたは活動がすでに実行されている場合、意図をチェックするための1つを2つのエントリを作成する必要があり、それを再作成しています場合は onNewIntent()、および1を取得し、あなたがのonCreateで意図を(取得しますインテントは本当に余分な "A"を含んでいません。 null文字列に switchを使用することはできません。したがって、Aがnullであるかどうかを確認すると安全です。

private void handleIntent(Intent intent) { 
    final String a = intent.getStringExtra(EXTRA_A); 
    if (a != null) { 
     switch (a) { 
      case A_RESTORE: 
       ... 

      case A_CLOSE: 
       ... 

異なるPendingIntentsに対して同じ意図を変更しないでください。それらは変更可能であり、getActivity()は防御的なコピーを作成しません。

final Intent resultIntentRestore = new Intent(this, Adult1Activity.class); 
resultIntentRestore.putExtra(EXTRA_A, A_RESTORE); 
PendingIntent restoreIntent = PendingIntent.getActivity(Adult1Activity.this, 
     0, resultIntentRestore, PendingIntent.FLAG_UPDATE_CURRENT); 

final Intent resultIntentClose = new Intent(this, Adult1Activity.class); 
resultIntentClose.putExtra(EXTRA_A, A_CLOSE); 
PendingIntent closeIntent = PendingIntent.getActivity(Adult1Activity.this, 
     2, resultIntentClose, PendingIntent.FLAG_UPDATE_CURRENT); 
+0

お返事ありがとう! –

+0

あなたのコードを使用していますが、今は 'handleIntent' method.whatにnullPointerエラーがあります。私にあなたの電子メールをお願いしますか? –

+0

@MinaDaheshあなたはおそらく、外部のonCreate()からgetIntent()を使用しているか、それを引き起こしているhandleIntent()に何かを追加しました。私はここに私の電子メールを投稿しませんが、あなたは私のバイオにリンクしているFacebook経由で私に連絡することができます。 –

関連する問題