gpt4 book ai didi

java - WebView 空引用

转载 作者:行者123 更新时间:2023-11-30 10:17:28 25 4
gpt4 key购买 nike

我的 android 应用程序中的推送通知栏有问题...当用户尝试按下按钮时,我的应用程序崩溃并抛出空对象引用错误:

E/AndroidRuntime:致命异常:IntentService[NotificationActionService]进程:tk.ypod.ypod,PID:24013java.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();
}
}
}
}

我的 list

<?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 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 );
}
}
);
}

最佳答案

我不确定你想要达到什么目的,但我确信你的方法需要改进

首先,空指针是因为,您还没有初始化 WebView,但您调用了以下内容:webview = webview.findViewById(R.id.activity_notification_webview)

其次,您正在从 IntentService 执行 UI 操作(在您的情况下使用 WebView),这将不起作用,因为 IntentService 在工作线程中运行。

我建议您将处理 WebView 的代码移动到 Activity 或 Fragment。

关于java - WebView 空引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49717056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com