我的Android应用程序中的推送通知栏有问题...当用户尝试按下按钮时,我的应用程序崩溃抛出空对象引用错误:
E / AndroidRuntime:致命异常:IntentService [NotificationActionService] 处理:tk.ypod.ypod,PID:24013 java.lang.NullPointerException:尝试调用虚方法' android.view.View android.view.View.findViewById(int)'在空对象引用上
我试图修复此错误几个小时没有成功,所以现在我向您寻求帮助。
我的代码:
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();
}
}
}
}
我的清单
<?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>
编辑,通知创建: 的意图
Intent intent = new Intent(MainActivity.this, MainActivity.class);
intent.putExtra("Next",true);
PendingIntent nextIntent = PendingIntent.getActivity(MainActivity.this, 1, intent, 0);
签入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 );
}
}
);
}
答案 0 :(得分:0)
我不确定您要实现的目标,但我确信您的方法需要改进
首先,空指针是因为,您尚未初始化WebView,但您调用了以下内容:
webview = webview.findViewById(R.id.activity_notification_webview)
其次,您正在从IntentService执行UI操作(在您的情况下使用WebView),由于IntentService在工作线程中运行,因此无法正常工作。
我建议您将处理WebView的代码移动到Activity或Fragment。
答案 1 :(得分:0)
当您使用服务时,您无法访问视图(通常用于活动或片段),您需要使用广播接收器或待处理的意图,以便将数据从您的服务发送到活动,您的活动将读取数据并相应地加载/更改视图。
答案 2 :(得分:0)
E / AndroidRuntime:致命异常: IntentService [NotificationActionService]进程:tk.ypod.ypod,PID: 24013 java.lang.NullPointerException:尝试调用虚方法 &#39; android.view.View android.view.View.findViewById(int)&#39;在null 对象参考
错误是因为您尝试从Service
访问UI组件,但这是不可能的。您应该在Activity
中托管上述代码,并使用Service
或LocalBroadcastmanager
与Messenger
进行通信。