我一直在尝试寻找有关创建 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();
}
}
它使应用程序崩溃,一直在努力找出原因,我一直陷入死胡同,现在看看是否有人可以帮助我解决这个问题
日志猫
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)