我的 Android 应用程序抛出 RuntimeException

发布于 2024-12-17 21:06:38 字数 3924 浏览 3 评论 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/Android运行时(494):位于 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/Android运行时(494):位于 android.app.ActivityThread.main(ActivityThread.java:4627) 11-25 12:33:34.697: E/AndroidRuntime(494): 在 java.lang.reflect.Method.invokeNative(本机方法) 11-25 12:33:34.697: E/AndroidRuntime(494): 在 java.lang.reflect.Method.invoke(Method.java:521) 11-25 12:33:34.697: E/Android运行时(494):位于 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): 在 dalvik.system.NativeStart.main(本机方法)11-25 12:33:34.697: E/AndroidRuntime(494):导致:java.lang.NullPointerException 11-25 12:33:34.697: E/AndroidRuntime(494): 在 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 更多

块引用

Android app gives me an error now:

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();
    }
}

And this is the logcat:

11-25 12:33:34.697: E/AndroidRuntime(494): FATAL EXCEPTION: main 11-25
12:33:34.697: E/AndroidRuntime(494): java.lang.RuntimeException:
Unable to resume activity
{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): at
android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3143)
11-25 12:33:34.697: E/AndroidRuntime(494): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2684)
11-25 12:33:34.697: E/AndroidRuntime(494): at
android.app.ActivityThread.access$2300(ActivityThread.java:125) 11-25
12:33:34.697: E/AndroidRuntime(494): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-25 12:33:34.697: E/AndroidRuntime(494): at
android.os.Handler.dispatchMessage(Handler.java:99) 11-25
12:33:34.697: E/AndroidRuntime(494): at
android.os.Looper.loop(Looper.java:123) 11-25 12:33:34.697:
E/AndroidRuntime(494): at
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): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 11-25
12:33:34.697: E/AndroidRuntime(494): at
dalvik.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): at
android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1149)
11-25 12:33:34.697: E/AndroidRuntime(494): at
android.app.Activity.performResume(Activity.java:3823) 11-25
12:33:34.697: E/AndroidRuntime(494): at
android.app.ActivityThread.performResumeActivity(ActivityThread.java:3118)
11-25 12:33:34.697: E/AndroidRuntime(494): ... 12 more

Blockquote

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

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

发布评论

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

评论(3

兔姬 2024-12-24 21:06:38

你们真的很亲近。您只需将加载 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)
    }
  }

You are really close. You just need to move the code that loads the url into onResume:

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

onResume is called right after onCreate so it will be called in both the case where it's the first load and when coming back from the pause.

Update based on discussion in the comments:
To move the web view to an instance variable:

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)
    }
  }
眼趣 2024-12-24 21:06:38

基本上,您只需要查看 Activity Lifecycle 流程。

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

Basically you just need to look at the Activity Lifecycle flow.

Just leave the initializations in onCreate and check for the WIFI and launch dialog in onResume.

友欢 2024-12-24 21:06:38

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

查看架构Android 开发人员中的活动生命周期以了解其工作原理。

You are doing right creating all the stuff in the onCreate. onResume is executed when you turn on screen and device wakes up, so think about the logical stuff you should do then: refresh graphics which need to be refreshed, restart bluetooth or wifi communications if they were shut down, etc

Have a look at the schema of Activities lifecicles in Android Developers to learn how this works.

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