2011-12-21 78 views
0

我開始爲Android編程,直到我嘗試將SharedPreferences的讀/寫代碼(在每個活動中)分隔到一個單獨的類時,我沒有遇到任何嚴重問題,請說CONFIGMANAGER。獨立類上的Android和SharedPreferences

這裏是我CONFIGMANAGER類:

package com.application.testing; 

import android.app.Activity; 
import android.content.Context; 
import android.content.SharedPreferences; 

public class ConfigManager extends Activity { 

    public boolean boolPref; 
    public String stringPref; 

    private SharedPreferences prefs; 

    public ConfigManager() { 
     prefs = getSharedPreferences(
       getResources().getString(R.string.preferences), 
       Context.MODE_PRIVATE); 
     loadCfg(); 
    } 

    public void saveCfg() { 

     SharedPreferences.Editor editor = prefs.edit(); 

     editor.putBoolean("boolPreference", boolPref); 
     editor.putString("stringPreference", stringPref); 

     editor.commit(); 

    } 

    public void loadCfg() { 

     this.boolPref = prefs.getBoolean("boolPreference", false); 
     this.stringPref = prefs.getString("stringPreference", ""); 

    } 

} 

現在,當我嘗試實例化這個類來讀/寫的喜好,我收到錯誤和應用程序退出。

讓我們說一個閃屏,如果用戶決定不顯示,我保留它的首選項,並在應用程序的開始,如果它是真或假,讀取顯示閃屏或移動到下一個活動。

這將是閃屏類代碼:

package com.application.testing; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 

public class SplashScreen extends Activity { 

    public ConfigManager cfg; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 

     cfg = new ConfigManager(); 

     if (!(cfg.boolPref)) { 
      Intent i = new Intent("com.application.testing.NEXTACTIVITY"); 
      startActivity(i); 
     } else { 

      setContentView(R.layout.splash); 

      Thread timer = new Thread() { 
       public void run() { 

        try { 
         sleep(3000); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } finally { 
         Intent i = new Intent("com.application.testing.NEXTACTIVITY"); 
         startActivity(i); 
        } 

       } 
      }; 

      timer.start(); 
     } 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     cfg.saveCfg(); // not neccesary, no preference changes during splashscreen execution 
     finish(); 
    } 

} 

最後,這是logcat的輸出:

12-21 23:46:48.294: D/AndroidRuntime(1381): Shutting down VM 
12-21 23:46:48.344: W/dalvikvm(1381): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 
12-21 23:46:48.375: E/AndroidRuntime(1381): FATAL EXCEPTION: main 
12-21 23:46:48.375: E/AndroidRuntime(1381): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.application.testing/com.application.testing.SplashScreen}: java.lang.NullPointerException 
12-21 23:46:48.375: E/AndroidRuntime(1381):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
12-21 23:46:48.375: E/AndroidRuntime(1381):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
12-21 23:46:48.375: E/AndroidRuntime(1381):  at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
12-21 23:46:48.375: E/AndroidRuntime(1381):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
12-21 23:46:48.375: E/AndroidRuntime(1381):  at android.os.Handler.dispatchMessage(Handler.java:99) 
12-21 23:46:48.375: E/AndroidRuntime(1381):  at android.os.Looper.loop(Looper.java:123) 
12-21 23:46:48.375: E/AndroidRuntime(1381):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
12-21 23:46:48.375: E/AndroidRuntime(1381):  at java.lang.reflect.Method.invokeNative(Native Method) 
12-21 23:46:48.375: E/AndroidRuntime(1381):  at java.lang.reflect.Method.invoke(Method.java:521) 
12-21 23:46:48.375: E/AndroidRuntime(1381):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
12-21 23:46:48.375: E/AndroidRuntime(1381):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
12-21 23:46:48.375: E/AndroidRuntime(1381):  at dalvik.system.NativeStart.main(Native Method) 
12-21 23:46:48.375: E/AndroidRuntime(1381): Caused by: java.lang.NullPointerException 
12-21 23:46:48.375: E/AndroidRuntime(1381):  at android.content.ContextWrapper.getResources(ContextWrapper.java:80) 
12-21 23:46:48.375: E/AndroidRuntime(1381):  at com.application.testing.ConfigManager.<init>(ConfigManager.java:22) 
12-21 23:46:48.375: E/AndroidRuntime(1381):  at com.application.testing.SplashScreen.onCreate(SplashScreen.java:22) 
12-21 23:46:48.375: E/AndroidRuntime(1381):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
12-21 23:46:48.375: E/AndroidRuntime(1381):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
12-21 23:46:48.375: E/AndroidRuntime(1381):  ... 11 more 

有誰知道我的錯誤是什麼?

提前很多感謝的,因爲我要瘋了:)

+0

歡迎StackOverflow的:相反,改變它的構造採取Context

public ConfigManager(Context context) { prefs = context.getSharedPreferences( context.getResources().getString(R.string.preferences), Context.MODE_PRIVATE); loadCfg(); } 

然後,您可以通過ActivityConfigManagerSplashScreenonCreate。標題中沒有必要標記問題。一旦您收到答案或更多答案,問題左側的「圖標/框」將變爲綠色,並且一旦您接受答案,字體將變爲黃色。所以每個人都可以看到問題狀態。 :) – 2011-12-22 00:57:09

回答

3

ConfigManager不應擴大Activity

cfg = new ConfigManager(this); 
+0

對!就是這樣!我只想指出一件事,將'context.'添加到'getSharedPreferences()' – jila 2011-12-22 00:47:33