我试图避免使用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
答案 0 :(得分:2)
它写在你的stacktrace中
无法添加窗口 - 令牌android.os.BinderProxy@40517fb0无效;是你的活动正在运行
启动执行重新加载的线程
然后按下
您的活动已完成
线程返回并尝试绘制您的活动
哎呀它已经完成了
答案 1 :(得分:1)
查看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
也会继续运行并重新加载页面。要解决此问题,您可以停止ScheduledExecutorService
中的onPause
。
调用scheduleAtFixedFate()
会返回ScheduledFuture
个对象。存储此对象,然后使用cancel(true)
方法在其上调用onPause()
。
答案 2 :(得分:0)
您的活动在到达该行代码时已停止。请参阅Android论坛中此“错误”报告中的交换:http://code.google.com/p/android/issues/detail?id=3953