WebView 上的 NullPointerException (WebView.requestFocus())

发布于 2024-11-09 15:56:21 字数 6839 浏览 0 评论 0原文

我有一个问题:

在我的 Android 应用程序中,有一个包含 WebView 的活动。此 WebView 包含播放的 Flash 内容。当我从该应用程序切换到主屏幕时,Flash 内容在后台流畅运行。当我切换到主屏幕然后关闭屏幕时,Flash 内容也在运行。但是,当我关闭屏幕时,当我在应用程序本身中时,WebView 会在文本末尾抛出一个 NullPointerException ,如下所示。我真的不知道如何解决这个问题。我希望有人可以帮助我。

编辑:我为Android 2.2开发。经过一些测试,我发现了一些特别的东西。当我在运行应用程序时关闭屏幕时,我会得到上述异常,但是当我及时重新打开屏幕时,我“仅”得到以下异常。我知道这没什么特别的并且已经众所周知,但也许它有帮助。

05-27 07:46:10.067: ERROR/AndroidRuntime(8064): FATAL EXCEPTION: main
05-27 07:46:10.067: ERROR/AndroidRuntime(8064): java.lang.NullPointerException
05-27 07:46:10.067: ERROR/AndroidRuntime(8064):     at android.webkit.WebView.requestFocus(WebView.java:6737)
05-27 07:46:10.067: ERROR/AndroidRuntime(8064):     at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:1085)
05-27 07:46:10.067: ERROR/AndroidRuntime(8064):     at android.view.ViewGroup.requestFocus(ViewGroup.java:1041)
05-27 07:46:10.067: ERROR/AndroidRuntime(8064):     at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:1085)
05-27 07:46:10.067: ERROR/AndroidRuntime(8064):     at android.view.ViewGroup.requestFocus(ViewGroup.java:1041)
05-27 07:46:10.067: ERROR/AndroidRuntime(8064):     at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:1085)
05-27 07:46:10.067: ERROR/AndroidRuntime(8064):     at android.view.ViewGroup.requestFocus(ViewGroup.java:1044)
05-27 07:46:10.067: ERROR/AndroidRuntime(8064):     at android.view.View.requestFocus(View.java:3671)
05-27 07:46:10.067: ERROR/AndroidRuntime(8064):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1224)
05-27 07:46:10.067: ERROR/AndroidRuntime(8064):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1870)
05-27 07:46:10.067: ERROR/AndroidRuntime(8064):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-27 07:46:10.067: ERROR/AndroidRuntime(8064):     at android.os.Looper.loop(Looper.java:130)
05-27 07:46:10.067: ERROR/AndroidRuntime(8064):     at android.app.ActivityThread.main(ActivityThread.java:3694)
05-27 07:46:10.067: ERROR/AndroidRuntime(8064):     at java.lang.reflect.Method.invokeNative(Native Method)
05-27 07:46:10.067: ERROR/AndroidRuntime(8064):     at java.lang.reflect.Method.invoke(Method.java:507)
05-27 07:46:10.067: ERROR/AndroidRuntime(8064):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
05-27 07:46:10.067: ERROR/AndroidRuntime(8064):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
05-27 07:46:10.067: ERROR/AndroidRuntime(8064):     at dalvik.system.NativeStart.main(Native Method)

这是创建 WebView 的方法:

protected void onCreate(Bundle savedInstanceState){
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.externalplayer);

    final Intent j = this.getIntent();

    webview = (WebView)findViewById(R.id.webview);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.getSettings().setAllowFileAccess(true);
    webview.getSettings().setPluginsEnabled(true);
    webview.freeMemory();
    webview.setPictureListener(new picListener());

    webview.setWebViewClient(new WebViewClient(){
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url){
            return true;
        }   
    });

    if(Controller.unbuggedStart == true){
        Controller.unbuggedStart = false;
        webview.destroy();
    } else{
        Controller.unbuggedStart = true;
        d = ProgressDialog.show(externalPlayer.this, "Loading...", "Wait a moment...");
        webview.loadUrl(j.getCharSequenceExtra("link").toString());
    }
}


05-26 20:13:29.825: ERROR/AndroidRuntime(4754): FATAL EXCEPTION: main
05-26 20:13:29.825: ERROR/AndroidRuntime(4754): java.lang.RuntimeException: Unable to start activity ComponentInfo{xxxApp}: java.lang.NullPointerException
05-26 20:13:29.825: ERROR/AndroidRuntime(4754):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1655)
05-26 20:13:29.825: ERROR/AndroidRuntime(4754):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1671)
05-26 20:13:29.825: ERROR/AndroidRuntime(4754):     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:2840)
05-26 20:13:29.825: ERROR/AndroidRuntime(4754):     at android.app.ActivityThread.access$1600(ActivityThread.java:117)
05-26 20:13:29.825: ERROR/AndroidRuntime(4754):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
05-26 20:13:29.825: ERROR/AndroidRuntime(4754):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-26 20:13:29.825: ERROR/AndroidRuntime(4754):     at android.os.Looper.loop(Looper.java:130)
05-26 20:13:29.825: ERROR/AndroidRuntime(4754):     at android.app.ActivityThread.main(ActivityThread.java:3694)
05-26 20:13:29.825: ERROR/AndroidRuntime(4754):     at java.lang.reflect.Method.invokeNative(Native Method)
05-26 20:13:29.825: ERROR/AndroidRuntime(4754):     at java.lang.reflect.Method.invoke(Method.java:507)
05-26 20:13:29.825: ERROR/AndroidRuntime(4754):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
05-26 20:13:29.825: ERROR/AndroidRuntime(4754):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
05-26 20:13:29.825: ERROR/AndroidRuntime(4754):     at dalvik.system.NativeStart.main(Native Method)
05-26 20:13:29.825: ERROR/AndroidRuntime(4754): Caused by: java.lang.NullPointerException
05-26 20:13:29.825: ERROR/AndroidRuntime(4754):     at android.webkit.WebView.requestFocus(WebView.java:6737)
05-26 20:13:29.825: ERROR/AndroidRuntime(4754):     at android.view.View.requestFocus(View.java:3671)
05-26 20:13:29.825: ERROR/AndroidRuntime(4754):     at android.view.View.requestFocus(View.java:3649)
05-26 20:13:29.825: ERROR/AndroidRuntime(4754):     at com.android.internal.policy.impl.PhoneWindow.restoreHierarchyState(PhoneWindow.java:1497)
05-26 20:13:29.825: ERROR/AndroidRuntime(4754):     at android.app.Activity.onRestoreInstanceState(Activity.java:844)
05-26 20:13:29.825: ERROR/AndroidRuntime(4754):     at android.app.Activity.performRestoreInstanceState(Activity.java:816)
05-26 20:13:29.825: ERROR/AndroidRuntime(4754):     at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1096)
05-26 20:13:29.825: ERROR/AndroidRuntime(4754):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1633)
05-26 20:13:29.825: ERROR/AndroidRuntime(4754):     ... 12 more

I've got a question:

In my Android app there is an activity containing a WebView. This WebView contains Flash content which is played back. When I switch out of this App to my Homescreen, the Flash contents is running smooth in the background. When I switch to the Homescreen an then turn the screen off, the Flash content is also running. But when I turn the screen off, while I'm in the App itself, the WebView throws a NullPointerException like the following at the end of the text. I have seriously no clue how to fix this issue. I hope that somebody might help me out.

Edit: I develop for Android 2.2. After some testing I found something special. When I turn off the screen while I'am running the app I get the mentioned exception, but when I turn the screen back on in time, I get "only" the following exception. I know it's nothing special and already known, but maybe it helps.

05-27 07:46:10.067: ERROR/AndroidRuntime(8064): FATAL EXCEPTION: main
05-27 07:46:10.067: ERROR/AndroidRuntime(8064): java.lang.NullPointerException
05-27 07:46:10.067: ERROR/AndroidRuntime(8064):     at android.webkit.WebView.requestFocus(WebView.java:6737)
05-27 07:46:10.067: ERROR/AndroidRuntime(8064):     at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:1085)
05-27 07:46:10.067: ERROR/AndroidRuntime(8064):     at android.view.ViewGroup.requestFocus(ViewGroup.java:1041)
05-27 07:46:10.067: ERROR/AndroidRuntime(8064):     at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:1085)
05-27 07:46:10.067: ERROR/AndroidRuntime(8064):     at android.view.ViewGroup.requestFocus(ViewGroup.java:1041)
05-27 07:46:10.067: ERROR/AndroidRuntime(8064):     at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:1085)
05-27 07:46:10.067: ERROR/AndroidRuntime(8064):     at android.view.ViewGroup.requestFocus(ViewGroup.java:1044)
05-27 07:46:10.067: ERROR/AndroidRuntime(8064):     at android.view.View.requestFocus(View.java:3671)
05-27 07:46:10.067: ERROR/AndroidRuntime(8064):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1224)
05-27 07:46:10.067: ERROR/AndroidRuntime(8064):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1870)
05-27 07:46:10.067: ERROR/AndroidRuntime(8064):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-27 07:46:10.067: ERROR/AndroidRuntime(8064):     at android.os.Looper.loop(Looper.java:130)
05-27 07:46:10.067: ERROR/AndroidRuntime(8064):     at android.app.ActivityThread.main(ActivityThread.java:3694)
05-27 07:46:10.067: ERROR/AndroidRuntime(8064):     at java.lang.reflect.Method.invokeNative(Native Method)
05-27 07:46:10.067: ERROR/AndroidRuntime(8064):     at java.lang.reflect.Method.invoke(Method.java:507)
05-27 07:46:10.067: ERROR/AndroidRuntime(8064):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
05-27 07:46:10.067: ERROR/AndroidRuntime(8064):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
05-27 07:46:10.067: ERROR/AndroidRuntime(8064):     at dalvik.system.NativeStart.main(Native Method)

And here is the the method where the WebView is created:

protected void onCreate(Bundle savedInstanceState){
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.externalplayer);

    final Intent j = this.getIntent();

    webview = (WebView)findViewById(R.id.webview);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.getSettings().setAllowFileAccess(true);
    webview.getSettings().setPluginsEnabled(true);
    webview.freeMemory();
    webview.setPictureListener(new picListener());

    webview.setWebViewClient(new WebViewClient(){
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url){
            return true;
        }   
    });

    if(Controller.unbuggedStart == true){
        Controller.unbuggedStart = false;
        webview.destroy();
    } else{
        Controller.unbuggedStart = true;
        d = ProgressDialog.show(externalPlayer.this, "Loading...", "Wait a moment...");
        webview.loadUrl(j.getCharSequenceExtra("link").toString());
    }
}


05-26 20:13:29.825: ERROR/AndroidRuntime(4754): FATAL EXCEPTION: main
05-26 20:13:29.825: ERROR/AndroidRuntime(4754): java.lang.RuntimeException: Unable to start activity ComponentInfo{xxxApp}: java.lang.NullPointerException
05-26 20:13:29.825: ERROR/AndroidRuntime(4754):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1655)
05-26 20:13:29.825: ERROR/AndroidRuntime(4754):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1671)
05-26 20:13:29.825: ERROR/AndroidRuntime(4754):     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:2840)
05-26 20:13:29.825: ERROR/AndroidRuntime(4754):     at android.app.ActivityThread.access$1600(ActivityThread.java:117)
05-26 20:13:29.825: ERROR/AndroidRuntime(4754):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
05-26 20:13:29.825: ERROR/AndroidRuntime(4754):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-26 20:13:29.825: ERROR/AndroidRuntime(4754):     at android.os.Looper.loop(Looper.java:130)
05-26 20:13:29.825: ERROR/AndroidRuntime(4754):     at android.app.ActivityThread.main(ActivityThread.java:3694)
05-26 20:13:29.825: ERROR/AndroidRuntime(4754):     at java.lang.reflect.Method.invokeNative(Native Method)
05-26 20:13:29.825: ERROR/AndroidRuntime(4754):     at java.lang.reflect.Method.invoke(Method.java:507)
05-26 20:13:29.825: ERROR/AndroidRuntime(4754):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
05-26 20:13:29.825: ERROR/AndroidRuntime(4754):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
05-26 20:13:29.825: ERROR/AndroidRuntime(4754):     at dalvik.system.NativeStart.main(Native Method)
05-26 20:13:29.825: ERROR/AndroidRuntime(4754): Caused by: java.lang.NullPointerException
05-26 20:13:29.825: ERROR/AndroidRuntime(4754):     at android.webkit.WebView.requestFocus(WebView.java:6737)
05-26 20:13:29.825: ERROR/AndroidRuntime(4754):     at android.view.View.requestFocus(View.java:3671)
05-26 20:13:29.825: ERROR/AndroidRuntime(4754):     at android.view.View.requestFocus(View.java:3649)
05-26 20:13:29.825: ERROR/AndroidRuntime(4754):     at com.android.internal.policy.impl.PhoneWindow.restoreHierarchyState(PhoneWindow.java:1497)
05-26 20:13:29.825: ERROR/AndroidRuntime(4754):     at android.app.Activity.onRestoreInstanceState(Activity.java:844)
05-26 20:13:29.825: ERROR/AndroidRuntime(4754):     at android.app.Activity.performRestoreInstanceState(Activity.java:816)
05-26 20:13:29.825: ERROR/AndroidRuntime(4754):     at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1096)
05-26 20:13:29.825: ERROR/AndroidRuntime(4754):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1633)
05-26 20:13:29.825: ERROR/AndroidRuntime(4754):     ... 12 more

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼眸 2024-11-16 15:56:21

好的,伙计们,我解决了这个问题。

解决方案的线索是屏幕方向。就像您在第一篇文章的代码中看到的那样,我在 onCreate() 方法中调用了: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

此调用的结果是,该活动被调用两次,因为方向改变。这就是我使用的原因:

if(Controller.unbuggedStart == true){
        Controller.unbuggedStart = false;
        webview.destroy();
    } else{
        Controller.unbuggedStart = true;
        d = ProgressDialog.show(externalPlayer.this, "Loading...", "Wait a moment...");
        webview.loadUrl(j.getCharSequenceExtra("link").toString());
    }

此代码可防止 WebView 启动两次。您可以看到我销毁了两个创建的 WebView 之一。现在,当我关闭屏幕时,操作系统想要调用被破坏的 WebView,这会导致 NullPointerException。

我仔细考虑了 onCreate 方法的机制并更改了它。现在,清单中描述了方向:

android:screenOrientation="landscape"

经过这次修改,问题并没有真正解决。当我关闭屏幕时,操作系统再次启动了带有 WebView 的 Activity,因为当我关闭屏幕时,方向从横向切换为纵向。我在清单中添加了以下内容:

android:configChanges="keyboardHidden|orientation"

现在一切正常。

Ok guys, I solved this problem.

The clue to the solution was the screen orientation. Like you can see in the code of the first post, I called in the onCreate() method: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

The result of this call was, that the activity was called twice because of the orientation change. Thats the reason why I used:

if(Controller.unbuggedStart == true){
        Controller.unbuggedStart = false;
        webview.destroy();
    } else{
        Controller.unbuggedStart = true;
        d = ProgressDialog.show(externalPlayer.this, "Loading...", "Wait a moment...");
        webview.loadUrl(j.getCharSequenceExtra("link").toString());
    }

This code prevents the WebView for being started twice. You can see that I destroy one of the two created WebViews. Now, when I turned off the screen, the OS wants to call the destroyed WebView which leads to the NullPointerException.

I overthougt the mechanisms of my onCreate method and changed it. Now, the orientation is discribed in the manifest:

android:screenOrientation="landscape"

After this modification, the problem wasn't truley solved. When I turned off the screen, the OS launched the Activity with the WebView in it again, because the orientation switched from Landscape to Portrait when I switched off the screen. I added in the Manifest the following:

android:configChanges="keyboardHidden|orientation"

and now everything works perfectly.

属性 2024-11-16 15:56:21

在不知道您正在运行的 Android 版本的情况下,我查看了 WebView 的头部修订版,但似乎没有NPE 有很多机会。一种可能性是,当屏幕关闭时,WebView.destroy() 方法将被调用(将 mWebViewCore 设置为 null),即使 Activity 仍保留在该方法上并尝试在状态被改变时将焦点集中到它上。恢复了。

您可以尝试重写 Activity 中的 onRestoreInstanceState 并查看此时 WebView 是否仍具有一致的状态。再次,查看头部修订版,如果您要调用“getSettings()”,这将出于同样的原因抛出 NPE,并且您可以相对确定这就是问题所在。

Without knowing the version of Android that you're running, I took a look at the implementation of requestFocus() in the head revision of WebView and there don't appear to be that many opportunities for an NPE. One possibility is that when the screen is turned off, that the WebView.destroy() method is getting called (setting mWebViewCore to null), even though the Activity is still holding onto it and trying to give focus to it when the state is being restored.

You might try overriding onRestoreInstanceState in your Activity and seeing whether the WebView still has a consistent state at that point. Again, looking at the head revision, if you were to call "getSettings()", this will throw an NPE for the same reason and you could be relatively certain that this is the issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文