NullPointerException异常上的WebView(WebView.requestFocus()) [英] NullPointerException on WebView (WebView.requestFocus())

查看:473
本文介绍了NullPointerException异常上的WebView(WebView.requestFocus())的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题:

在我的Andr​​oid应用程序存在含有的WebView 的活动。这的WebView 包含了播放Flash内容。当我切换出这个应用程序,以我的主屏幕时,Flash内容在后台运行流畅。当我切换到主屏幕的则关闭屏幕,Flash内容也在运行。但是,当我关闭屏幕,而我在App本身,的WebView 抛出一个 NullPointerException异常像下面的文本的末尾。我认真地不知道如何解决这个问题。我希望有人可以帮助我。

编辑:我制定的Andr​​oid 2.2 。经过一些测试,我发现一些特别的东西。当我关掉屏幕,同时我'运行的应用程序,我得到的提到的异常,但是当我打开屏幕的时候回来,我得到的唯一,以下情况例外。我知道这是没有什么特别的和已知的,但也许会有所帮助。

  7月5日至27日:46:10.067:ERROR / AndroidRuntime(8064):致命异常:主要
七月五日至27号:46:10.067:ERROR / AndroidRuntime(8064):显示java.lang.NullPointerException
七月五日至27号:46:10.067:ERROR / AndroidRuntime(8064):在android.webkit.WebView.requestFocus(WebView.java:6737)
七月五日至27号:46:10.067:ERROR / AndroidRuntime(8064):在android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:1085)
七月五日至27号:46:10.067:ERROR / AndroidRuntime(8064):在android.view.ViewGroup.requestFocus(ViewGroup.java:1041)
七月五日至27号:46:10.067:ERROR / AndroidRuntime(8064):在android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:1085)
七月五日至27号:46:10.067:ERROR / AndroidRuntime(8064):在android.view.ViewGroup.requestFocus(ViewGroup.java:1041)
七月五日至27号:46:10.067:ERROR / AndroidRuntime(8064):在android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:1085)
七月五日至27号:46:10.067:ERROR / AndroidRuntime(8064):在android.view.ViewGroup.requestFocus(ViewGroup.java:1044)
七月五日至27号:46:10.067:ERROR / AndroidRuntime(8064):在android.view.View.requestFocus(View.java:3671)
七月五日至27号:46:10.067:ERROR / AndroidRuntime(8064):在android.view.ViewRoot.performTraversals(ViewRoot.java:1224)
七月五日至27号:46:10.067:ERROR / AndroidRuntime(8064):在android.view.ViewRoot.handleMessage(ViewRoot.java:1870)
七月五日至27号:46:10.067:ERROR / AndroidRuntime(8064):在android.os.Handler.dispatchMessage(Handler.java:99)
七月五日至27号:46:10.067:ERROR / AndroidRuntime(8064):在android.os.Looper.loop(Looper.java:130)
七月五日至27号:46:10.067:ERROR / AndroidRuntime(8064):在android.app.ActivityThread.main(ActivityThread.java:3694)
七月五日至27号:46:10.067:ERROR / AndroidRuntime(8064):在java.lang.reflect.Method.invokeNative(本机方法)
七月五日至27号:46:10.067:ERROR / AndroidRuntime(8064):在java.lang.reflect.Method.invoke(Method.java:507)
七月五日至27号:46:10.067:ERROR / AndroidRuntime(8064):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:841)
七月五日至27号:46:10.067:ERROR / AndroidRuntime(8064):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
七月五日至27号:46:10.067:ERROR / AndroidRuntime(8064):在dalvik.system.NativeStart.main(本机方法)
 

这里是其中的WebView 被创建的方法:

 保护无效的onCreate(包savedInstanceState){
    // TODO自动生成方法存根
    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);

    最终意图J = this.getIntent();

    的WebView =(web视图)findViewById(R.id.webview);
    webview.getSettings()setJavaScriptEnabled(真)。
    。webview.getSettings()setAllowFileAccess(真正的);
    webview.getSettings()setPluginsEnabled(真)。
    webview.freeMemory();
    webview.setPictureListener(新picListener());

    webview.setWebViewClient(新WebViewClient(){
        @覆盖
        公共布尔shouldOverrideUrlLoading(web视图查看,字符串URL){
            返回true;
        }
    });

    如果(Controller.unbuggedStart ==真){
        Controller.unbuggedStart = FALSE;
        webview.destroy();
    } 其他{
        Controller.unbuggedStart =真;
        D = ProgressDialog.show(externalPlayer.this,载入中...,等一下......);
        webview.loadUrl(j.getCharSequenceExtra(链接)的toString());
    }
}


05-26 20:13:29.825:ERROR / AndroidRuntime(4754):致命异常:主要
05-26 20:13:29.825:ERROR / AndroidRuntime(4754):java.lang.RuntimeException的:无法启动的活动ComponentInfo {xxxApp}:显示java.lang.NullPointerException
05-26 20:13:29.825:ERROR / AndroidRuntime(4754):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1655)
05-26 20:13:29.825:ERROR / AndroidRuntime(4754):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1671)
05-26 20:13:29.825:ERROR / AndroidRuntime(4754):在android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:2840)
05-26 20:13:29.825:ERROR / AndroidRuntime(4754):在android.app.ActivityThread.access $ 1600(ActivityThread.java:117)
05-26 20:13:29.825:ERROR / AndroidRuntime(4754):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:935)
05-26 20:13:29.825:ERROR / AndroidRuntime(4754):在android.os.Handler.dispatchMessage(Handler.java:99)
05-26 20:13:29.825:ERROR / AndroidRuntime(4754):在android.os.Looper.loop(Looper.java:130)
05-26 20:13:29.825:ERROR / AndroidRuntime(4754):在android.app.ActivityThread.main(ActivityThread.java:3694)
05-26 20:13:29.825:ERROR / AndroidRuntime(4754):在java.lang.reflect.Method.invokeNative(本机方法)
05-26 20:13:29.825:ERROR / AndroidRuntime(4754):在java.lang.reflect.Method.invoke(Method.java:507)
05-26 20:13:29.825:ERROR / AndroidRuntime(4754):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:841)
05-26 20:13:29.825:ERROR / AndroidRuntime(4754):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
05-26 20:13:29.825:ERROR / AndroidRuntime(4754):在dalvik.system.NativeStart.main(本机方法)
05-26 20:13:29.825:ERROR / AndroidRuntime(4754):由:显示java.lang.NullPointerException
05-26 20:13:29.825:ERROR / AndroidRuntime(4754):在android.webkit.WebView.requestFocus(WebView.java:6737)
05-26 20:13:29.825:ERROR / AndroidRuntime(4754):在android.view.View.requestFocus(View.java:3671)
05-26 20:13:29.825:ERROR / AndroidRuntime(4754):在android.view.View.requestFocus(View.java:3649)
05-26 20:13:29.825:ERROR / AndroidRuntime(4754):在com.android.internal.policy.impl.PhoneWindow.restoreHierarchyState(PhoneWindow.java:1497)
05-26 20:13:29.825:ERROR / AndroidRuntime(4754):在android.app.Activity.onRestoreInstanceState(Activity.java:844)
05-26 20:13:29.825:ERROR / AndroidRuntime(4754):在android.app.Activity.performRestoreInstanceState(Activity.java:816)
05-26 20:13:29.825:ERROR / AndroidRuntime(4754):在android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1096)
05-26 20:13:29.825:ERROR / AndroidRuntime(4754):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1633)
05-26 20:13:29.825:ERROR / AndroidRuntime(4754):12 ...更多
 

解决方案

好球员,我解决了这个问题。

该线索的解决方案是屏幕方向。就像你可以在第一篇文章中code看,我打电话的onCreate()方法: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

此调用的结果是,该活动是因为方向变化的叫了两声。这就是为什么我使用的原因:

 如果(Controller.unbuggedStart ==真){
        Controller.unbuggedStart = FALSE;
        webview.destroy();
    } 其他{
        Controller.unbuggedStart =真;
        D = ProgressDialog.show(externalPlayer.this,载入中...,等一下......);
        webview.loadUrl(j.getCharSequenceExtra(链接)的toString());
    }
 

这code prevents的的WebView 为正在启动两次。你可以看到,我毁掉两个创建WebViews之一。现在,当我关掉屏幕,操作系统要调用的破坏的WebView 这导致了 NullPointerException异常

我overthougt我的onCreate方法的机制,改变了它。现在,定向discribed在清单:

 安卓screenOrientation =风景
 

这个修改后,这个问题并没有truley解决。当我关掉屏幕,操作系统发起了其中的的WebView 的活动了,因为方向从横向切换到肖像时,我关掉屏幕。我在清单中增加了以下内容:

 安卓configChanges =keyboardHidden |定位
 

和现在一切都完美的作品。

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

解决方案

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.

这篇关于NullPointerException异常上的WebView(WebView.requestFocus())的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆