2011-11-25 90 views
0

Android应用给了我现在的错误:我的Android应用程序抛出RuntimeException

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的:

12月11日至25日:33:34.697:E/AndroidRuntime(494 ):FATAL EXCEPTION:main 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):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 androi d.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):在 com.android.in ternal.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):引起: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):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.per formResumeActivity(ActivityThread.java:3118) 十二月11日至25日:33:34.697:E/AndroidRuntime(494):... 12多个

块引用

+0

PLZ补充说,你有你的的onResume方法试过的代码,你就加requestfeature() ??如果是的话,删除它,它已经添加到你的onCreate方法,或者只是,重写方法onStart();并在其上添加行请求功能,并从其他方法中删除(onCreate,onResume) – Houcine

回答

5

你真的很接近。你只需要移动时加载的网址进入的onResume代码:的onCreate之后

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

的onResume被称为所以它会在两种情况下被调用,其中它的第一负载和未来从暂停回来的时候。基于在评论中讨论

更新: 要移动的网络视图实例变量:

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) 
    } 
    } 
+0

非常感谢! – MartijnG

+0

我还有一个小问题,我如何获得** myWebView.loadUrl(weblink); **在onResume中可用?它说:myWebView无法解析... – MartijnG

+0

将mWebView声明为类实例变量而不是本地变量(将该声明移到构造函数方法之外并在类声明之后放置它)。 – Chris

1

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

只需在onCreate中保留初始值,然后检查onResume中的WIFI和启动对话框。

1

你正在做正确的onCreate所有的东西。当您打开屏幕和设备唤醒时,执行onResume,因此请思考您应该做的逻辑操作:刷新需要刷新的图形,关闭时重新启动蓝牙或WiFi通信等。

有一个查看Android开发人员活动生命周期的模式,了解其工作原理。

相关问题