I have problem with push notification bar in my android app... When user tries to press button on it, my app crashes throwing null object reference error:
E/AndroidRuntime: FATAL EXCEPTION: IntentService[NotificationActionService] Process: tk.ypod.ypod, PID: 24013 java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
I tried to fix this error for couple of hours with no success, so now I am asking for help from you.
My code:
package tk.ypod.ypod;
import android.app.IntentService;
import android.content.Intent;
import android.media.MediaPlayer;
import android.util.Log;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
public class NotificationActionService extends IntentService {
static final String LOG_TAG = MainActivity.class.getSimpleName();
private MediaPlayer mp;
private WebView webview;
public NotificationActionService() {
super( "NotificationActionService" );
}
@Override
protected void onHandleIntent(Intent intent) {
webview = webview.findViewById(R.id.activity_notification_webview);
webview.setWebChromeClient( new WebChromeClient() );
webview.setWebViewClient(new WebClient() {
});
String action = intent.getAction();
Log.e( LOG_TAG, "Received notification action: " + action );
if ("Previous".equals( action )) {
webview.loadUrl( "javascript: previous();" );
} else if ("Next".equals( action )) {
webview.loadUrl( "javascript: next();" );
} else if ("Stop".equals( action )) {
if (mp.isPlaying()) {
webview.loadUrl( "javascript: stopVideo();" );
mp.stop();
}
} else if ("Play".equals( action )) {
if (mp.isPlaying()) {
webview.loadUrl( "javascript: playVideo();" );
mp.start();
}
}
}
}
My manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="tk.ypod.ypod" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:label="yPOD"
android:hardwareAccelerated="true"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
<activity
android:name="tk.ypod.ypod.MainActivity"
android:label="yPOD" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".NotificationActionService" />
</application>
Edit, notification creating: Intent
Intent intent = new Intent(MainActivity.this, MainActivity.class);
intent.putExtra("Next",true);
PendingIntent nextIntent = PendingIntent.getActivity(MainActivity.this, 1, intent, 0);
Checking in onCreate()
boolean nextFlag = getIntent().getBooleanExtra("Next",false);
if(nextFlag) {
webview.evaluateJavascript( "javascript:next()",
new ValueCallback <String>() {
@Override
public void onReceiveValue(String value) {
Log.e( LOG_TAG, "Next workded " + value );
}
}
);
}