確定你所有的超級java/android guru的。我知道你會看到這個笑,但我真的需要幫助。我是一個超級新手,並且已經閱讀了所有Google開發文檔(還有更多),並且無法理解這個概念。可以通過查看工作代碼而不是轉介我已經閱讀並顯然不理解的東西來學習更多,所以如果有人能幫助我,那將是美好的。按下後退按鈕時,定時器不關閉。 Forceclose錯誤按鈕按
主要活動
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}
任何和所有幫助將不勝感激。
在這一點上,我覺得自己像一個完全瘋狂的女人,真的需要完成這個項目,所以我的生活可以前進。如果任何人(任何人)對此有回答,我會真正讚賞它。
你好嗎按下按鈕有什麼錯誤? – Estel
對不起力量在onPause關閉,並假設它曾經做過,onResume也會有問題。 – Chris
請添加一個堆棧跟蹤(其實我希望它會在每次定時器被觸發時崩潰) – mibollma