我一直在努力寻找有关创建Helper类的教程或示例。我一直在努力的是数据库助手的教程。我想要做的不是使用重复的函数或语句等...我试图创建一个帮助类来包含他重复的代码但由于某种原因我不能让它工作。下面是一个快速的例子我/我想做什么。
助手类
public class Helper extends Activity {
Context ctx;
static SharedPreferences settings;
public static SharedPreferences.Editor edit;
public static final String PREF = "PrefTest";
static boolean c;
static int B;
static String A, a, b, C, D, E, F;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
settings = getSharedPreferences(PREF,MODE_PRIVATE);
edit = settings.edit();
}
public static String userId() {
a = settings.getString("Phone", "");
return a;
}
public static String pwd(){
b = settings.getString("pwd", "");
return b;
}
public static boolean lock(){
c = settings.getBoolean("pwd", false);
return c;
}
public static String version(Context ctx) {
A = ctx.getResources().getString(R.string.version);
return A;
}
public static void saveUser(String user){
if(user == null || user == ""){
edit.putString("Phone", "User N/A");
edit.commit();
}else{
edit.putString("Phone", user);
edit.commit();
}
}
public static void savePwd(String D){
if(D == null || D ==""){
edit.putString("pwd",D);
edit.commit();
}else{
edit.putString("pwd",Error.validate(D + userId()));
edit.commit();
}
}
public static void saveLock(boolean lock) {
if (lock){
edit.putBoolean("Locked", true);
edit.commit();
}else{
edit.putBoolean("Locked", false);
edit.commit();
}
}
public static int passwordCheck() {
if (pwd() == null || pwd() == "") {
B = 0;
} else {
B = 1;
}
return B;
}
public static int phoneCheck() {
if (userId() == null || userId() == "") {
B = 0;
} else {
B = 1;
}
return B;
}
public static int lockCheck() {
if (lock()) {
B = 1;
} else {
B = 0;
}
return B;
}
}
现在我打电话的时候:
int ab = Helper.phoneCheck();
if(ab == 0){
try{
Helper.saveUser("TestUser");
}catch(Exception e){
e.printStackTrace();
}
}
它崩溃了应用程序,一直在努力弄清楚为什么我会继续陷入死胡同,现在看看是否有人可以帮助我解决这个问题
logcat的
05-30 01:25:11.067: D/dalvikvm(1411): GC_EXTERNAL_ALLOC freed 43K, 53% free 2551K/5379K, external 716K/1038K, paused 125ms
05-30 01:25:11.237: D/AndroidRuntime(1411): Shutting down VM
05-30 01:25:11.237: W/dalvikvm(1411): threadid=1: thread exiting with uncaught exception (group=0x40015560)
05-30 01:25:11.277: E/AndroidRuntime(1411): FATAL EXCEPTION: main
05-30 01:25:11.277: E/AndroidRuntime(1411): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.prefapp/com.test.prefapp.StartUp}: java.lang.NullPointerException
05-30 01:25:11.277: E/AndroidRuntime(1411): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-30 01:25:11.277: E/AndroidRuntime(1411): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-30 01:25:11.277: E/AndroidRuntime(1411): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-30 01:25:11.277: E/AndroidRuntime(1411): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-30 01:25:11.277: E/AndroidRuntime(1411): at android.os.Handler.dispatchMessage(Handler.java:99)
05-30 01:25:11.277: E/AndroidRuntime(1411): at android.os.Looper.loop(Looper.java:123)
05-30 01:25:11.277: E/AndroidRuntime(1411): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-30 01:25:11.277: E/AndroidRuntime(1411): at java.lang.reflect.Method.invokeNative(Native Method)
05-30 01:25:11.277: E/AndroidRuntime(1411): at java.lang.reflect.Method.invoke(Method.java:507)
05-30 01:25:11.277: E/AndroidRuntime(1411): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-30 01:25:11.277: E/AndroidRuntime(1411): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-30 01:25:11.277: E/AndroidRuntime(1411): at dalvik.system.NativeStart.main(Native Method)
05-30 01:25:11.277: E/AndroidRuntime(1411): Caused by: java.lang.NullPointerException
05-30 01:25:11.277: E/AndroidRuntime(1411): at com.test.prefapp.Helper.userId(Helper.java:51)
05-30 01:25:11.277: E/AndroidRuntime(1411): at com.test.prefapp.Helper.phoneCheck(Helper.java:194)
05-30 01:25:11.277: E/AndroidRuntime(1411): at com.test.prefapp.StartUp.onCreate(StartUp.java:44)
05-30 01:25:11.277: E/AndroidRuntime(1411): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-30 01:25:11.277: E/AndroidRuntime(1411): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
答案 0 :(得分:1)
看起来你有两个活动:Helper和Startup。 而且你似乎从Startup.onCreate调用了Helper.phoneCheck。 在Helper.onCreate()中,您正在初始化设置:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
settings = getSharedPreferences(PREF,MODE_PRIVATE);
edit = settings.edit();
}
我怀疑当android框架调用Startup.onCreate时,Helper活动尚未启动。因此,它的Helper.onCreate没有被调用,设置也没有被初始化。
如果没有看到其余代码,我可以猜出一个可能的解决方案。如何更改Startup以便它从Helper而不是Activity扩展,如此?
public class Startup extends Helper {
public void onCreate() {
super.onCreate(); // This will initialize Helper.settings
...
int ab = Helper.phoneCheck();
}
}
通过这种方式,您可以保证Helper.settings将在您使用它时进行初始化。此外,其他活动也可以扩展Helper并重用其方法。