0

我的标题是不言自明的。这是我的代码:

 public class MainActivity extends Activity {

    private WebView browse;
    private TextView t;
    private String address="http://www.google.com";
    private int refresh, clearcache, time, refreshTime, cacheTime;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SharedPreferences gPrefs = PreferenceManager
                .getDefaultSharedPreferences(getBaseContext());
        address = gPrefs.getString("Address", "http://www.google.com");
        refresh = gPrefs.getInt("Refresh", 1);
        clearcache = gPrefs.getInt("ClearCache", 1);
        time = gPrefs.getInt("Time", 0);

        switch (time) {
        case 0:
            refreshTime = refresh * 60 * 60 * 1000;
            break;
        case 1:
            refreshTime = refresh * 60 * 1000;
            break;
        case 2:
            refreshTime = refresh * 1000;
            break;
        }

        switch (time) {
        case 0:
            cacheTime = clearcache * 60 * 60 * 1000;
            break;
        case 1:
            cacheTime = clearcache * 60 * 1000;
            break;
        case 2:
            cacheTime = clearcache * 1000;
            break;
        }

        browse = (WebView) findViewById(R.id.webView1);
        t = (TextView) findViewById(R.id.t);

        browse.getSettings().setJavaScriptEnabled(true);
        browse.getSettings().setPluginsEnabled(true);
        browse.getSettings().setLoadWithOverviewMode(true);
        browse.getSettings().setUseWideViewPort(true);
        browse.getSettings().setBuiltInZoomControls(true);
        browse.getSettings().setSupportZoom(true);
        browse.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        browse.setScrollbarFadingEnabled(true);
        browse.getSettings().setLoadsImagesAutomatically(true);
        browse.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        browse.getSettings().setAppCacheMaxSize(1024 * 1024 * 8);

        browse.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                // TODO Auto-generated method stub
                super.onPageStarted(view, url, favicon);
                t.setVisibility(TextView.VISIBLE);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                // do your stuff here
                super.onPageFinished(view, url);
                t.setVisibility(TextView.GONE);
            }
        });

        try {
            browse.loadUrl(address);
        } catch (Exception e) {
            e.printStackTrace();
        }

        refreshRRryan();
        cacheRRryan();
    }

    final Handler refreshhandler = new Handler();
    final Timer refreshtimer = new Timer();
    final Runnable refreshdoA = new Runnable() {
        @Override
        public void run() {
            browse.reload();
        }
    };

    public void refreshRRryan() {
        TimerTask refreshtask = new TimerTask() {
            @Override
            public void run() {
                refreshhandler.post(refreshdoA);
            }
        };
        refreshtimer.scheduleAtFixedRate(refreshtask, refreshTime, refreshTime);
    }

    final Handler cachehandler = new Handler();
    final Timer cachetimer = new Timer();
    final Runnable cachedoA = new Runnable() {
        @Override
        public void run() {
            browse.clearCache(true);
            getApplicationContext().deleteDatabase("webview.db");
            getApplicationContext().deleteDatabase("webviewCache.db");
        }
    };

    public void cacheRRryan() {
        TimerTask cachetask = new TimerTask() {
            @Override
            public void run() {
                cachehandler.post(cachedoA);
            }
        };
        cachetimer.scheduleAtFixedRate(cachetask, cacheTime, cacheTime);
    }

    @Override
    public File getCacheDir() {
        // NOTE: this method is used in Android 2.1

        return getApplicationContext().getCacheDir();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
        super.onCreateOptionsMenu(menu);
        MenuInflater blowUp = getMenuInflater();
        blowUp.inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
        case R.id.aboutUs:
            Intent i = new Intent("ABOUT");
            startActivity(i);
            break;
        case R.id.preferences:
            Intent p = new Intent("PREFS");
            startActivityForResult(p, 101);
            break;
        }
        return false;
    }

}

这是logcat:

08-17 11:08:05.997: E/AndroidRuntime(1197): FATAL EXCEPTION: main
08-17 11:08:05.997: E/AndroidRuntime(1197): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fit.world.browser/com.fit.world.browser.MainActivity}: java.lang.ClassCastException: java.lang.String
08-17 11:08:05.997: E/AndroidRuntime(1197):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-17 11:08:05.997: E/AndroidRuntime(1197):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-17 11:08:05.997: E/AndroidRuntime(1197):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-17 11:08:05.997: E/AndroidRuntime(1197):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-17 11:08:05.997: E/AndroidRuntime(1197):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-17 11:08:05.997: E/AndroidRuntime(1197):     at android.os.Looper.loop(Looper.java:123)
08-17 11:08:05.997: E/AndroidRuntime(1197):     at android.app.ActivityThread.main(ActivityThread.java:3683)
08-17 11:08:05.997: E/AndroidRuntime(1197):     at java.lang.reflect.Method.invokeNative(Native Method)
08-17 11:08:05.997: E/AndroidRuntime(1197):     at java.lang.reflect.Method.invoke(Method.java:507)
08-17 11:08:05.997: E/AndroidRuntime(1197):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-17 11:08:05.997: E/AndroidRuntime(1197):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-17 11:08:05.997: E/AndroidRuntime(1197):     at dalvik.system.NativeStart.main(Native Method)
08-17 11:08:05.997: E/AndroidRuntime(1197): Caused by: java.lang.ClassCastException: java.lang.String
08-17 11:08:05.997: E/AndroidRuntime(1197):     at android.app.ContextImpl$SharedPreferencesImpl.getInt(ContextImpl.java:2817)
08-17 11:08:05.997: E/AndroidRuntime(1197):     at com.fit.world.browser.MainActivity.onCreate(MainActivity.java:37)
08-17 11:08:05.997: E/AndroidRuntime(1197):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-17 11:08:05.997: E/AndroidRuntime(1197):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-17 11:08:05.997: E/AndroidRuntime(1197):     ... 11 more

而且我很确定我的清单很好,因为活动第一次运行。

请帮帮我。

4

2 回答 2

3
refresh = gPrefs.getInt("Refresh", 1);
clearcache = gPrefs.getInt("ClearCache", 1);
time = gPrefs.getInt("Time", 0);

应用程序第一次运行时,它使用您在上面使用的默认值 (1,1,0)。

但是第二次,它肯定会使用您从用户输入中保存的数据。

您必须将其保存为字符串并尝试将其检索为 int ...

于 2012-08-17T06:43:08.503 回答
2

错误很明显:java.lang.String cannot be cast to java.lang.Integer 您正在尝试将 String 对象转换为 Integer。那是做不到的。它们不是同一类型。

该错误会告诉您发生在文件的哪一行,因此您可以更正它。 MainActivity.java:37

于 2012-08-17T06:39:47.780 回答