Reputation: 37
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 );
}
}
);
}
Upvotes: 2
Views: 1104
Reputation: 687
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
The error is because you are trying to access UI component from Service
and that is not possible. You should host the above code in an Activity
and communicate with it from your Service
using LocalBroadcastmanager
or Messenger
.
Upvotes: 0
Reputation: 845
When you are using a service you don't have access to the views (which are usually used in actvities or fragments) you need to use either a broadcast reciver or a pending intent in order to send data from your service to the activity, your activity will read the data and load/change the views accordingly.
Upvotes: 0
Reputation: 24907
I am not sure what you are trying to achieve, but I am sure your approach needs to be improved
First of all the null pointer is because, you haven't initialized WebView and yet you called following:
webview = webview.findViewById(R.id.activity_notification_webview)
Secondly, you are performing UI operations (Using WebView in your case) from IntentService, which won't work since IntentService runs in worker thread.
I would recommend you to move your code, handling WebView, to either Activity or Fragment.
Upvotes: 1