Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java WebView空引用_Java_Android_Android Webview_Nullreferenceexception - Fatal编程技术网

Java WebView空引用

Java WebView空引用,java,android,android-webview,nullreferenceexception,Java,Android,Android Webview,Nullreferenceexception,我的android应用程序中的推送通知栏有问题。。。当用户试图按下按钮时,我的应用程序崩溃,抛出空对象引用错误: E/AndroidRuntime:致命异常:IntentService[NotificationActionService] 流程:tk.ypod.ypod,PID:24013 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“android.view.view android.view.view.findViewById(int)” 我尝

我的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(“下一步”,false);
if(nextFlag){
evaluateJavascript(“javascript:next()”,
新值回调(){
@凌驾
公共无效onReceiveValue(字符串值){
Log.e(日志标签,“下一个工作日”+值);
}
}
);
}

我不确定您想要实现什么,但我确信您的方法需要改进

首先,空指针是因为您尚未初始化WebView,但调用了以下命令:
webview=webview.findViewById(R.id.activity\u notification\u webview)

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


我建议您将处理WebView的代码移动到“活动”或“片段”中。

当您使用的服务无法访问视图(通常在“活动”或“片段”中使用)时,您需要使用广播接收器或挂起的意图,以便将数据从服务发送到“活动”,您的活动将读取数据并相应地加载/更改视图

E/AndroidRuntime:致命异常: IntentService[NotificationActionService]进程:tk.ypod.ypod,PID: 24013 java.lang.NullPointerException:尝试调用虚拟方法 空值上的“android.view.view android.view.view.findViewById(int)” 对象引用


错误是因为您正试图从
服务
访问UI组件,而这是不可能的。您应该在
活动中托管上述代码
,并通过
服务
使用
LocalBroadcastmanager
Messenger
与之通信。此目的用于处理通知按钮,没有它,我不知道热处理什么按钮用户按下你可以按照这一点,我试图这样做,但再次没有运气。。。现在用户可以按下按钮,它不会使应用程序崩溃,但我的webview.loadurl加载主页而不是javascript执行,并在新视图中显示它。我尝试这样做:
Intent Intent=new Intent(MainActivity.this,MainActivity.class);intent.putExtra(“下一个”,true);PendingEvent-nextIntent=PendingEvent.getActivity(MainActivity.this,1,intent,0)boolean nextFlag=getIntent().getBooleanExtra(“下一步”,false);如果(nextFlag){webview.loadUrl(“javascript:next()”);}
您的意图是在主页上执行javascript方法吗?在哪里定义next();?
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 );
            }
        }
    );
}