确定你所有超级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;
}
}
目录下载
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}
非常感谢任何和所有帮助。
在这一点上,我觉得自己是一个真正需要完成这个项目的疯狂女人,所以我的生活可以向前发展。如果有人(任何人)对此有答案,我真的会感激它。
答案 0 :(得分:1)
您在活动中有一个名为timer的私有计时器,但由于您的活动中有另一个名为timer的计时器,因此似乎没有初始化。在webview下定义的私有计时器计时器为null,因此在调用onPause时会得到nullpointexception。
从onCreate()中删除类,您可能需要将Timer定义为singlethreadscheduledexecutor。
timer = Executors.newSingleThreadScheduledExecutor();
timer.scheduleAtFixedRate(new Runnable() {
您的代码包含建议的答案:
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);
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;
}
}
答案 1 :(得分:0)
您可以尝试在活动中拦截onBackPressed,然后执行timer.cancel()。像
这样的东西@Override
public void onBackPressed() {
timer.cancel();
}
答案 2 :(得分:0)
onStop()是放置timer.cancel()的更可靠的位置(如果你想覆盖几乎每种情况,例如最后here所述的情况,那么这两个地方)
onPause()在每种情况下都不会被调用,当onResume()很可能发生时使用它,例如当你继续进行另一个活动然后返回时。请注意lifecycle outline中的缩进。按下后退按钮时可能无法调用。
编辑1:这是一个预编辑答案,因此可能无法解决强制关闭问题。
编辑2:
@Override
public void onStop(){
super.onStop();
try {
timer.cancel();
} catch (Exception ex) {
Log.e("MY APP ERROR", ex.getMessage());
}
}
我总是在这些生命周期方法中将调用置于超级优先级。我不确定它是否有所作为。您可能也想设置try / catch语句,特别是如果您选择在onStop()和onPause()中包含timer.cancel()。
编辑3: 关于准备承认失败。我对ScheduledExecutorService的怪癖并不熟悉,既然你已经说过你愿意改写,那么我在应用程序中完成了同样的事情。 Handler是一个特定于Android的实现,所以也许这就是为什么我没有遇到同样的麻烦。
Handler timer;
Runnable runner;
@Override
onCreate(...) {
...
//EDIT 4: (reading time from preferences)
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
final int c = Integer.parseInt(SP.getString("appRefreshRate","20"));
...
runner = new Runnable() {
@Override
public void run() {
timer.postDelayed(runner, c * 1000);
webview.reload();
}
};
timer = new Handler();
timer.postDelayed(runner, c * 1000);
...
}
@Override
public void onPause(){
super.onPause();
try {
timer.removeCallbacks(runner);
} catch (Exception ex) {
Log.e("MY APP ERROR", ex.getMessage());
}
}
@Override
public void onStop(){
super.onStop();
try {
timer.removeCallbacks(runner);
} catch (Exception ex) {
Log.e("MY APP ERROR", ex.getMessage());
}
}