2016-07-01 3 views
1

알림을 받으면 충돌하는 Android 앱이 있습니다. 알림은 내 앱 서버에서 메시지를받습니다. 알림을 탭하면 웹보기 활동이 열립니다. 내 문제는 웹 링크에서 열려면 URL 링크를 하드 코드 할 때 작동합니다. 하지만, 내 애플 리케이션 서버에서 보낸 URL 링크 (문자열 값)를 사용하여 공유 환경 설정에 저장 한 다음이를 사용하여 webview 활동을 시작하면 앱이 다운됩니다. 내가 메시지를 수신하고 통지 보낼 곳Android 알림에 내 앱이 충돌 함

이것은 GCMPushReceiverClass입니다 : - -은 다음과 같이

코드는이라는 내 웹보기 활동

public class GCMPushReceiverService extends GcmListenerService { 

    Context applicationContent = FirstActivity.getApplicationData(); 

    @Override 
    public void onMessageReceived(String from, Bundle data) { 
     String message = data.getString("message"); 
     String link = data.getString("link"); 
     sendNotification(message); 
     SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(applicationContent); 
     SharedPreferences.Editor writer = mPrefs.edit(); 
     writer.putString("LINK",link); 
     writer.commit(); 
    } 

    private void sendNotification(String message) { 
      Intent intent = new Intent(this, ThirdActivity.class); 
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      int requestCode = 0; 
      Random random = new Random(); 
      int m = random.nextInt(9999-1000)+1000; 
      PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode, intent, PendingIntent.FLAG_ONE_SHOT); 
      int color = getResources().getColor(R.color.notColour); 
      NotificationCompat.Builder notBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this) 
       .setSmallIcon(R.drawable.sovran_icon) 
       .setDefaults(Notification.DEFAULT_ALL) 
       .setPriority(1) 
       .setLargeIcon(BitmapFactory.decodeResource(getBaseContext().getResources(), R.mipmap.ic_launcher)) 
       .setColor(color) 
       .setContentTitle("New Message") 
       .setContentText(message) 
       .setAutoCancel(true) 
       .setContentIntent(pendingIntent); 

      NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
      notificationManager.notify(m, notBuilder.build()); 
     } 
} 

입니다 알림 및 시작에서 의도를 수신하는 ThirdActivity 이 충돌 보고서를하다

public class ThirdActivity extends AppCompatActivity { 

    private WebView mWebView; 


    Context applicationContent = FirstActivity.getApplicationData(); 
    SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(applicationContent); 
    String link = mPrefs.getString("LINK",""); 

    public class mWebViewClient extends WebViewClient { 

      @Override 
      public boolean shouldOverrideUrlLoading(WebView view, String url) { 
       view.loadUrl(url); 
       return false; 
      } 
    } 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_third); 

      mWebView = (WebView) findViewById(R.id.webView1); 
      mWebView.setWebViewClient(new mWebViewClient()); 
      mWebView.getSettings().setJavaScriptEnabled(true); 
      mWebView.getSettings().setBuiltInZoomControls(true); 
      mWebView.loadUrl(link); 
    } 
} 

웹보기 활동

01 23,516,
> 07-01 14:05:57.101 2370-2423/com.sovraan E/AndroidRuntime: FATAL 
> EXCEPTION: AsyncTask #1 
>               Process: com.sovraan, PID: 2370 
>               java.lang.NullPointerException: Attempt to invoke virtual method 
> 'java.lang.String android.content.Context.getPackageName()' on a null 
> object reference 
>                at android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:375) 
>                at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:370) 
>                at com.sovraan.GCMPushReceiverService.onMessageReceived(GCMPushReceiverService.java:30) 
>                at com.google.android.gms.gcm.GcmListenerService.zzq(Unknown Source) 
>                at com.google.android.gms.gcm.GcmListenerService.zzp(Unknown Source) 
>                at com.google.android.gms.gcm.GcmListenerService.zzo(Unknown Source) 
>                at com.google.android.gms.gcm.GcmListenerService.zza(Unknown Source) 
>                at com.google.android.gms.gcm.GcmListenerService$1.run(Unknown Source) 
>                at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
>                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
>                at java.lang.Thread.run(Thread.java:818) 

한 가지 중요한 점은

가 충돌 앱이 실행되고 있지 않은 경우에만 발생주의합니다.

알림을 받기 전에 앱을 시작하면 알림이 수신되고 앱이 정상적으로 작동합니다. 다시 앱을 종료하면 알림을 받으면 같은 오류가 발생합니다. 컨텍스트/그 상태를 변경할 수 있습니다 실시간으로 진화 무언가이다 :

+0

항상 충돌과 관련된 오류를 게시하십시오. 또한 컨텍스트는 무엇입니까? applicationContent = FirstActivity.getApplicationData()? 어떤 조건에서 null이 반환 될 수 있습니까? –

+0

@ An-droid FirstActivity 클래스에서 applicationData라는 정적 컨텍스트를 가져 왔습니다. – BabiSter

+0

[NullPointerException은 무엇이며 어떻게 수정합니까?] (http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it)) –

답변

0

상황에 맞는 문제

Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference at ... 

정적 컨텍스트 나에게 위험한 것을 것 같다.

당신은 충돌 앱이 실행되고 있지 않은 경우에만 발생하기 때문에 서비스가 컨텍스트를 얻을 GSM의 메시지 만 activty을받을 수 있다고에서

이 GCMPushReceiverService의 맥락에서 접근 시도 초기화되지 않았습니다 필요한 시간

+0

미리 감사드립니다. 나는 그것을 조사 할 것입니다, 아마도 당신은 옳습니다. – BabiSter

+0

GCMPushReceiverService에서 어떻게 액세스합니까? – BabiSter

+0

"this"를 전달하거나 "getApplicaitonContext()"에 액세스 할 수 없습니까? –

관련 문제