0

Android 应用程序现在给我一个错误:

package com.martijngijselaar.rooster;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WebviewActivity extends MainActivity {

    private WebView myWebView;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);

        WebView myWebView = (WebView)findViewById(R.id.webview);

        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        myWebView.setWebViewClient(new WebViewClient());

        myWebView.requestFocus(View.FOCUS_DOWN);
        myWebView.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                    case MotionEvent.ACTION_UP:
                        if (!v.hasFocus()) {
                            v.requestFocus();
                        }
                        break;
                }
                return false;
            }
        });
    }   

    public void onResume() {
        super.onResume();
        if ( isOnline() == true )
            myWebView.loadUrl(webLink);
        else if ( isOnline() == false )
            showNoConnectionDialog();
    }
}

这是logcat:

11-25 12:33:34.697:E/AndroidRuntime(494):致命异常:主要 11-25 12:33:34.697:E/AndroidRuntime(494):java.lang.RuntimeException:无法恢复活动 {com.martijngijselaar .rooster/com.martijngijselaar.rooster.WebviewActivity}: java.lang.NullPointerException 11-25 12:33:34.697: E/AndroidRuntime(494): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3128) 11- 25 12:33:34.697: E/AndroidRuntime(494): 在 android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3143) 11-25 12:33:34.697: E/AndroidRuntime(494): 在 android.app。 ActivityThread.handleLaunchActivity(ActivityThread.java:2684) 11-25 12:33:34.697: E/AndroidRuntime(494): 在 android.app.ActivityThread.access$2300(ActivityThread.java:125) 11-25 12:33:34.697 : E/AndroidRuntime(494): 在 android.app.ActivityThread$H。handleMessage(ActivityThread.java:2033) 11-25 12:33:34.697: E/AndroidRuntime(494): 在 android.os.Handler.dispatchMessage(Handler.java:99) 11-25 12:33:34.697: E/ AndroidRuntime(494): 在 android.os.Looper.loop(Looper.java:123) 11-25 12:33:34.697: E/AndroidRuntime(494): 在 android.app.ActivityThread.main(ActivityThread.java:4627 ) 11-25 12:33:34.697: E/AndroidRuntime(494): at java.lang.reflect.Method.invokeNative(Native Method) 11-25 12:33:34.697: E/AndroidRuntime(494): at java. lang.reflect.Method.invoke(Method.java:521) 11-25 12:33:34.697: E/AndroidRuntime(494): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868 ) 11-25 12:33:34.697: E/AndroidRuntime(494): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 11-25 12:33:34.697: E/AndroidRuntime(494 ):在达尔维克。system.NativeStart.main(Native Method) 11-25 12:33:34.697: E/AndroidRuntime(494): Caused by: java.lang.NullPointerException 11-25 12:33:34.697: E/AndroidRuntime(494): at com.martijngijselaar.rooster.WebviewActivity.onResume(WebviewActivity.java:46) 11-25 12:33:34.697: E/AndroidRuntime(494): 在 android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1149) 11-25 12:33:34.697: E/AndroidRuntime(494): 在 android.app.Activity.performResume(Activity.java:3823) 11-25 12:33:34.697: E/AndroidRuntime(494): 在 android.app.ActivityThread .performResumeActivity(ActivityThread.java:3118) 11-25 12:33:34.697: E/AndroidRuntime(494): ... 12 更多martijngijselaar.rooster.WebviewActivity.onResume(WebviewActivity.java:46) 11-25 12:33:34.697: E/AndroidRuntime(494): 在 android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1149) 11-25 12: 33:34.697: E/AndroidRuntime(494): 在 android.app.Activity.performResume(Activity.java:3823) 11-25 12:33:34.697: E/AndroidRuntime(494): 在 android.app.ActivityThread.performResumeActivity (ActivityThread.java:3118) 11-25 12:33:34.697: E/AndroidRuntime(494): ... 12 更多martijngijselaar.rooster.WebviewActivity.onResume(WebviewActivity.java:46) 11-25 12:33:34.697: E/AndroidRuntime(494): 在 android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1149) 11-25 12: 33:34.697: E/AndroidRuntime(494): 在 android.app.Activity.performResume(Activity.java:3823) 11-25 12:33:34.697: E/AndroidRuntime(494): 在 android.app.ActivityThread.performResumeActivity (ActivityThread.java:3118) 11-25 12:33:34.697: E/AndroidRuntime(494): ... 12 更多在 android.app.ActivityThread.performResumeActivity(ActivityThread.java:3118) 11-25 12:33:34.697: E/AndroidRuntime(494): ... 12 更多在 android.app.ActivityThread.performResumeActivity(ActivityThread.java:3118) 11-25 12:33:34.697: E/AndroidRuntime(494): ... 12 更多

块引用

4

3 回答 3

5

你真的很亲近。您只需将加载 url 的代码移动到 onResume 中:

public void onResume(){
super.onResume();
 if ( isOnline() == true )
            myWebView.loadUrl(webLink);
}

onResume 在 onCreate 之后立即调用,因此在第一次加载和从暂停返回时都会调用它。

根据评论中的讨论进行更新:将 Web 视图移动到实例变量:

public class WebviewActivity extends MainActivity {

  private WebView myWebView;

  public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.main);

    myWebView = (WebView)findViewById(R.id.webview);
     //the rest of your onCreate method here)
    }
  }
于 2011-11-25T11:36:33.687 回答
1

基本上你只需要看看Activity Lifecycle流程。

只需保留初始化onCreate并检查 WIFI 并在 onResume 中启动对话框。

于 2011-11-25T11:31:03.400 回答
1

您在 onCreate 中创建所有东西是正确的。onResume 在您打开屏幕并且设备唤醒时执行,因此请考虑一下您应该执行的逻辑操作:刷新需要刷新的图形,如果蓝牙或 wifi 通信被关闭,则重新启动,等等

查看 Android Developers 中的活动生命周期架构,了解其工作原理。

于 2011-11-25T11:34:33.303 回答