按下后退按钮时计时器不会关闭。按下按钮时强制关闭错误
好的,各位超级 java/android 专家。我知道你看到这个会笑,但我真的需要帮助。我是一个超级新手,已经阅读了所有 Google 开发文档(以及更多文档),但就是无法掌握这个概念。通过查看工作代码可以学到更多东西,而不是参考我已经读过但显然不理解的东西,所以如果有人可以帮助我,那就太好了。
主要活动
import java.util.Timer;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
public class Quotes extends Activity implements OnClickListener {
ProgressDialog dialog;
private WebView webview;
private Timer timer;
@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(){
timer.cancel();
super.onPause();
}
@Override
public void onResume(){
webview.reload();
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;
}
}
CatLog
07-05 17:28:51.001: DEBUG/AndroidRuntime(11446): Shutting down VM
07-05 17:28:51.001: WARN/dalvikvm(11446): threadid=1: thread exiting with uncaught exception (group=0x40018560)
07-05 17:28:51.017: ERROR/AndroidRuntime(11446): FATAL EXCEPTION: main
07-05 17:28:51.017: ERROR/AndroidRuntime(11446): java.lang.RuntimeException: Unable to pause activity {com.dge.quotes/com.dge.quotes.Quotes}: java.lang.NullPointerException
07-05 17:28:51.017: ERROR/AndroidRuntime(11446): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2477)
07-05 17:28:51.017: ERROR/AndroidRuntime(11446): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2424)
07-05 17:28:51.017: ERROR/AndroidRuntime(11446): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2404)
07-05 17:28:51.017: ERROR/AndroidRuntime(11446): at android.app.ActivityThread.access$1700(ActivityThread.java:124)
07-05 17:28:51.017: ERROR/AndroidRuntime(11446): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:979)
07-05 17:28:51.017: ERROR/AndroidRuntime(11446): at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 17:28:51.017: ERROR/AndroidRuntime(11446): at android.os.Looper.loop(Looper.java:123)
07-05 17:28:51.017: ERROR/AndroidRuntime(11446): at android.app.ActivityThread.main(ActivityThread.java:3806)
07-05 17:28:51.017: ERROR/AndroidRuntime(11446): at java.lang.reflect.Method.invokeNative(Native Method)
07-05 17:28:51.017: ERROR/AndroidRuntime(11446): at java.lang.reflect.Method.invoke(Method.java:507)
07-05 17:28:51.017: ERROR/AndroidRuntime(11446): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-05 17:28:51.017: ERROR/AndroidRuntime(11446): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-05 17:28:51.017: ERROR/AndroidRuntime(11446): at dalvik.system.NativeStart.main(Native Method)
07-05 17:28:51.017: ERROR/AndroidRuntime(11446): Caused by: java.lang.NullPointerException
07-05 17:28:51.017: ERROR/AndroidRuntime(11446): at com.dge.quotes.Quotes.onPause(Quotes.java:72)
07-05 17:28:51.017: ERROR/AndroidRuntime(11446): at android.app.Activity.performPause(Activity.java:3901)
07-05 17:28:51.017: ERROR/AndroidRuntime(11446): at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1191)
07-05 17:28:51.017: ERROR/AndroidRuntime(11446): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2459)
07-05 17:28:51.017: ERROR/AndroidRuntime(11446): ... 12 more
07-05 17:28:51.509: WARN/ActivityManager(1319): Activity pause timeout for HistoryRecord{40ba7e20 com.dge.quotes/.Quotes}
任何和所有帮助将不胜感激。
在这一点上,我感觉自己像一个完全疯狂的女人,她真的需要完成这个项目,这样我的生活才能继续前进。如果有人(任何人)对此有答案,我将非常感激。
OK all you super java/android guru's. I know your going to look at this and laugh, but I really need the help. Am a super newbie and have read every Google development doc (and many more) and just can't grasp the concept. Can learn a LOT more by seeing working code rather than a referral to something I already read and obviously didn't understand, so if someone can help me out, that would be wonderful.
Main Activity
import java.util.Timer;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
public class Quotes extends Activity implements OnClickListener {
ProgressDialog dialog;
private WebView webview;
private Timer timer;
@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(){
timer.cancel();
super.onPause();
}
@Override
public void onResume(){
webview.reload();
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;
}
}
CatLog
07-05 17:28:51.001: DEBUG/AndroidRuntime(11446): Shutting down VM
07-05 17:28:51.001: WARN/dalvikvm(11446): threadid=1: thread exiting with uncaught exception (group=0x40018560)
07-05 17:28:51.017: ERROR/AndroidRuntime(11446): FATAL EXCEPTION: main
07-05 17:28:51.017: ERROR/AndroidRuntime(11446): java.lang.RuntimeException: Unable to pause activity {com.dge.quotes/com.dge.quotes.Quotes}: java.lang.NullPointerException
07-05 17:28:51.017: ERROR/AndroidRuntime(11446): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2477)
07-05 17:28:51.017: ERROR/AndroidRuntime(11446): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2424)
07-05 17:28:51.017: ERROR/AndroidRuntime(11446): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2404)
07-05 17:28:51.017: ERROR/AndroidRuntime(11446): at android.app.ActivityThread.access$1700(ActivityThread.java:124)
07-05 17:28:51.017: ERROR/AndroidRuntime(11446): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:979)
07-05 17:28:51.017: ERROR/AndroidRuntime(11446): at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 17:28:51.017: ERROR/AndroidRuntime(11446): at android.os.Looper.loop(Looper.java:123)
07-05 17:28:51.017: ERROR/AndroidRuntime(11446): at android.app.ActivityThread.main(ActivityThread.java:3806)
07-05 17:28:51.017: ERROR/AndroidRuntime(11446): at java.lang.reflect.Method.invokeNative(Native Method)
07-05 17:28:51.017: ERROR/AndroidRuntime(11446): at java.lang.reflect.Method.invoke(Method.java:507)
07-05 17:28:51.017: ERROR/AndroidRuntime(11446): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-05 17:28:51.017: ERROR/AndroidRuntime(11446): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-05 17:28:51.017: ERROR/AndroidRuntime(11446): at dalvik.system.NativeStart.main(Native Method)
07-05 17:28:51.017: ERROR/AndroidRuntime(11446): Caused by: java.lang.NullPointerException
07-05 17:28:51.017: ERROR/AndroidRuntime(11446): at com.dge.quotes.Quotes.onPause(Quotes.java:72)
07-05 17:28:51.017: ERROR/AndroidRuntime(11446): at android.app.Activity.performPause(Activity.java:3901)
07-05 17:28:51.017: ERROR/AndroidRuntime(11446): at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1191)
07-05 17:28:51.017: ERROR/AndroidRuntime(11446): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2459)
07-05 17:28:51.017: ERROR/AndroidRuntime(11446): ... 12 more
07-05 17:28:51.509: WARN/ActivityManager(1319): Activity pause timeout for HistoryRecord{40ba7e20 com.dge.quotes/.Quotes}
Any and all help would be greatly appreciated.
At this point I feel like a completely crazed woman who really needs to get this project done so my life can move forward. If anyone (ANYONE) has an answer to this, I would REALLY Appreciate it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您在活动中有一个名为计时器的私有计时器,但是它似乎尚未初始化,因为您的活动中有另一个名为计时器的计时器。您在 webview 下定义的私有计时器计时器为 null,因此当调用 onPause 时您会收到 nullpointException。
从 onCreate() 中删除该类,您可能需要将 Timer 定义为单线程调度执行器。
您的代码以及建议的答案:
You have a private Timer called timer in the activity, however that seems not to have been initialized since you have another timer called timer within your activity. Your private Timer timer defined under the webview is null therefore you get the nullpointexception when onPause is called.
Remove the class from within the onCreate(), you might need to define Timer as a singlethreadscheduledexecutor.
Your code with suggested answer:
您可以尝试在活动中拦截 onBackPressed,然后执行 timer.cancel()。像这样的东西
You could try intercepting onBackPressed in your activity and then do a timer.cancel(). Something like
onStop() 是放置 timer.cancel() 的更可靠位置(或者如果您想涵盖几乎所有情况,例如末尾所述的情况,则可以同时放置这两个位置 此处)。
onPause() 并不是在所有情况下都会被调用,它在 onResume() 很可能发生时使用,例如当您继续执行另一个活动然后返回时。请注意生命周期大纲中的缩进。按下后退按钮时可能不会调用它。
编辑 1:这是预编辑答案,因此可能不涵盖强制关闭问题。
编辑 2:
我始终在这些生命周期方法中首先调用 super 。我不确定这是否有什么不同。您可能还想设置 try/catch 语句,特别是如果您选择在 onStop() 和 onPause() 中都包含timer.cancel()。
编辑3:
大约准备认输了。我不太熟悉 ScheduledExecutorService 的怪癖,既然您已经表示愿意重写,那么我就是如何在我的应用程序中完成同样的事情的。 Handler 是 Android 特定的实现,所以也许这就是为什么我没有遇到与您相同的麻烦。
onStop() is the more reliable location to put timer.cancel() (or both places if you want to cover nearly every situation such as circumstances stated at the end here).
onPause() is not called in every situation, it is used when onResume() is reasonably likely to happen, such as when you proceed to another activity and then return. Note the indentation in the lifecycle outline. It may not be called when the back button is pressed.
EDIT 1: this is a pre-edit answer, so may not cover the force-close issue.
EDIT 2:
I have always put the call to super first in these lifecycle methods. I'm not sure if it makes a difference. You'll likely want to set the try/catch statement as well, especially if you choose to include timer.cancel() in both onStop() AND onPause().
EDIT 3:
About ready to concede defeat. I'm not that familiar with the quirks of ScheduledExecutorService, and since you've stated you're willing to rewrite, here's how I've accomplished the same things in my apps. Handler is an Android-specific implementation, so perhaps thats why I haven't had the same trouble you are having.