android – WebView上的NullPointerException(WebView.requestFocus())

前端之家收集整理的这篇文章主要介绍了android – WebView上的NullPointerException(WebView.requestFocus())前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我有一个问题:

在我的Android应用程序中,有一个包含WebView的活动.此WebView包含播放的Flash内容.当我将此应用程序切换到我的主屏幕时,Flash内容后台运行顺畅.当我切换到主屏幕然后关闭屏幕时,Flash内容也在运行.但是当我关闭屏幕时,当我在App本身时,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
最佳答案
好的,我解决了这个问题.

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

调用的结果是,由于方向更改,活动被调用了两次.这就是我使用的原因:

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

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

我超越了我的onCreate方法的机制并改变了它.现在,清单中描述了方向:

android:screenOrientation="landscape"

经过这次修改后,问题并未解决.当我关闭屏幕时,操作系统再次使用WebView启动了Activity,因为当我关闭屏幕时,方向从Landscape切换到Portrait.我在Manifest中添加了以下内容

android:configChanges="keyboardHidden|orientation"

现在一切都很完美.

猜你在找的Android相关文章