退出时的后退按钮创建强制关闭错误



我试图避免使用onDestroy,并希望尽可能简单,但当我退出程序时,我会遇到强制关闭错误。不知道为什么。这是应用程序主要部分的代码。有什么建议吗?

主要应用程序代码

   public class Quotes extends Activity implements OnClickListener {
   ProgressDialog dialog;
   private WebView webview;
   @Override
   public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          WebView adsview = (WebView) findViewById(R.id.ads);
          adsview.getSettings().setJavaScriptEnabled(true);
          adsview.loadUrl("http://www.dgdevelco.com/quotes/androidad.html");
          SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
          String q = SP.getString("appViewType","http://www.dgdevelco.com/quotes/quotesandroidtxt.html");
          String c = SP.getString("appRefreshRate","20");
          webview = (WebView) findViewById(R.id.scroll);
          webview.getSettings().setJavaScriptEnabled(true);
          webview.setWebViewClient(new QuotesWebView(this));
          webview.loadUrl(q);
          ScheduledExecutorService timer = Executors.newSingleThreadScheduledExecutor();
          timer.scheduleAtFixedRate(new Runnable() {
 @Override
 public void run() {
          webview.reload();
          }
 }, 10, Long.parseLong(c),TimeUnit.SECONDS);
 findViewById(R.id.refresh).setOnClickListener(this);

 }
 @Override
 public void onPause(){
          super.onPause();
 }
 @Override
 public void onResume(){
          super.onResume();
 }

 public void onClick(View v){
          switch(v.getId()){
          case R.id.refresh:
               webview.reload();
               break;
          }
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
          MenuInflater inflater = getMenuInflater();
          inflater.inflate(R.menu.menu, menu);
          MenuItem about = menu.getItem(0);
          about.setIntent(new Intent(this, About.class));
          MenuItem preferences = menu.getItem(1);
          preferences.setIntent(new Intent(this, Preferences.class));
          return true;
 }
 }   

LogCat

 07-04 13:34:55.011: INFO/DevicePushListener(1415): Connection State Changed: NetworkInfo: type: WIFI[], state: CONNECTED/CONNECTED, reason: (unspecified), extra: (none), roaming: false, failover: false, isAvailable: true
 07-04 13:34:55.011: WARN/WindowManager(1314): Attempted to add application window with unknown token HistoryRecord{40c40f50 com.dge.quotes/.Quotes}.  Aborting.
 07-04 13:34:55.034: DEBUG/AndroidRuntime(24137): Shutting down VM
 07-04 13:34:55.034: WARN/dalvikvm(24137): threadid=1: thread exiting with uncaught exception (group=0x40018560)
 07-04 13:34:55.058: ERROR/AndroidRuntime(24137): FATAL EXCEPTION: main
 07-04 13:34:55.058: ERROR/AndroidRuntime(24137): android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@40517fb0 is not valid; is your activity running?
 07-04 13:34:55.058: ERROR/AndroidRuntime(24137):     at android.view.ViewRoot.setView(ViewRoot.java:527)
 07-04 13:34:55.058: ERROR/AndroidRuntime(24137):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
 07-04 13:34:55.058: ERROR/AndroidRuntime(24137):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
 07-04 13:34:55.058: ERROR/AndroidRuntime(24137):     at android.view.Window$LocalWindowManager.addView(Window.java:424)
 07-04 13:34:55.058: ERROR/AndroidRuntime(24137):     at android.app.Dialog.show(Dialog.java:241)
 07-04 13:34:55.058: ERROR/AndroidRuntime(24137):     at android.app.ProgressDialog.show(ProgressDialog.java:107)
 07-04 13:34:55.058: ERROR/AndroidRuntime(24137):     at android.app.ProgressDialog.show(ProgressDialog.java:90)
 07-04 13:34:55.058: ERROR/AndroidRuntime(24137):     at com.dge.quotes.QuotesWebView.onPageStarted(QuotesWebView.java:22)
 07-04 13:34:55.058: ERROR/AndroidRuntime(24137):     at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:271)
 07-04 13:34:55.058: ERROR/AndroidRuntime(24137):     at android.os.Handler.dispatchMessage(Handler.java:99)
 07-04 13:34:55.058: ERROR/AndroidRuntime(24137):     at android.os.Looper.loop(Looper.java:123)
 07-04 13:34:55.058: ERROR/AndroidRuntime(24137):     at android.app.ActivityThread.main(ActivityThread.java:3806)
 07-04 13:34:55.058: ERROR/AndroidRuntime(24137):     at java.lang.reflect.Method.invokeNative(Native Method)
 07-04 13:34:55.058: ERROR/AndroidRuntime(24137):     at java.lang.reflect.Method.invoke(Method.java:507)
 07-04 13:34:55.058: ERROR/AndroidRuntime(24137):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
 07-04 13:34:55.058: ERROR/AndroidRuntime(24137):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
 07-04 13:34:55.058: ERROR/AndroidRuntime(24137):     at dalvik.system.NativeStart.main(Native Method)
 07-04 13:34:55.089: WARN/ActivityManager(1314):   Force finishing activity com.dge.quotes/.Quotes

它是在您的堆栈中编写的

无法添加窗口--令牌android.os.BinderProxy@40517fb0无效;你的活动正在运行吗

您启动一个线程来重新加载

然后按下

您的活动已完成

线程返回并尝试利用您的活动

Oops its have completed

看看ScheduledExecutorService文档页面上提供的示例代码:

import static java.util.concurrent.TimeUnit.*;
 class BeeperControl {
   private final ScheduledExecutorService scheduler =
     Executors.newScheduledThreadPool(1);
   public void beepForAnHour() {
     final Runnable beeper = new Runnable() {
       public void run() { System.out.println("beep"); 
     };
     final ScheduledFuture beeperHandle =
       scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
     scheduler.schedule(new Runnable() {
       public void run() { beeperHandle.cancel(true); }
     }, 60 * 60, SECONDS);
   }
 }}

(http://developer.android.com/reference/java/util/concurrent/ScheduledExecutorService.html)

正如其他人所说,问题是ScheduledExecutorService即使在用户关闭该活动之后也会继续运行和重新加载页面。要解决此问题,可以在onPause中停止ScheduledExecutorService

调用scheduleAtFixedFate()返回一个ScheduledFuture对象。存储此对象,然后在onPause()方法中对其调用cancel(true)

当活动到达代码行时,它已经停止。请参阅Android论坛中这份"bug"报告中的交流:http://code.google.com/p/android/issues/detail?id=3953

相关内容

  • 没有找到相关文章

最新更新