2

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 );
            }
        }
    );
}
DonatasP
  • 37
  • 8

3 Answers3

1

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.

Sagar
  • 23,903
  • 4
  • 62
  • 62
  • This intent is using for handling notification buttons, without it I don't know hot to handle what button user pressed – DonatasP Apr 08 '18 at 14:10
  • You can follow this [SO](https://stackoverflow.com/questions/19092559/perform-action-on-button-click-in-custom-notification-android) for that – Sagar Apr 08 '18 at 14:12
  • I tried to do something like that but again with no luck... Now user can press button and it's not crashing app, but my webview.loadurl loads homepage instead of javascript execution and shows it in new view. – DonatasP Apr 09 '18 at 10:19
  • I tried to do this: `Intent intent = new Intent(MainActivity.this, MainActivity.class); intent.putExtra("Next",true); PendingIntent nextIntent = PendingIntent.getActivity(MainActivity.this, 1, intent, 0);` and then in onCreate this: `boolean nextFlag = getIntent().getBooleanExtra("Next",false); if(nextFlag){ webview.loadUrl("javascript:next()"); }` – DonatasP Apr 09 '18 at 10:21
  • Is your intention is to execute javascript method on homepage? Where do you define next();? – Sagar Apr 09 '18 at 10:29
  • javascript: next() should be loaded in users current page not in homepage – DonatasP Apr 09 '18 at 10:46
  • Have you tried using `webView.evaluateJavascript()`? Instead of `loadUrl ()` – Sagar Apr 09 '18 at 11:10
  • Can you update your question with code for Activity and code of creating the Intent for Notifications? Its bit troublesome to read in comments. – Sagar Apr 09 '18 at 11:32
  • You are relaunching same activity (`MainActivity`) in your case. Depending on flag you sent in Manifest, it might no call onCreate. You need to use broadcast receiver for that. You can check this [SO](https://stackoverflow.com/questions/42404778/click-on-android-notification-icon-does-not-trigger-broadcast-receiver) – Sagar Apr 09 '18 at 12:58
  • I need to create class in separate than my activity java file for receiver yes? If so I could not call webview from there, could I? – DonatasP Apr 09 '18 at 16:37
0

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.

Anton Makov
  • 825
  • 2
  • 9
  • 25
0

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.

pradithya aria
  • 657
  • 5
  • 10