好的,你们都是超级 java/android 大师。我知道你会看到这个并大笑,但我真的需要帮助。我是一个超级新手,已经阅读了所有谷歌开发文档(以及更多),但无法掌握这个概念。可以通过查看工作代码而不是推荐我已经阅读但显然不理解的东西来学习更多,所以如果有人可以帮助我,那就太好了。
主要活动
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}
任何和所有的帮助将不胜感激。
在这一点上,我觉得自己像一个完全疯狂的女人,她真的需要完成这个项目,这样我的生活才能继续前进。如果有人(任何人)对此有答案,我会非常感激。