这里涉及2个课程................ 来自另一个类(SQLitedatabase)的方法:
public String QuantReturn(){
Testing x = new Testing();
String code = x.QRcodeReturn();
String quantity=null;
String selectQuery = "SELECT quantity FROM meds_db WHERE name = " + code;
Cursor c = db.rawQuery(selectQuery, null);
if(null != c && c.moveToFirst()){
quantity = c.getString(c.getColumnIndex(KEY_QUANTITY));
}
return quantity;
}
和被叫时崩溃的类........................................ ..........
public class Testing extends Activity {
public static final String SCAN_RESULT = "MyPreferencesFile";
public String QRcodeReturn(){
SharedPreferences codeHack = getSharedPreferences(SCAN_RESULT,0);
String QRcode = codeHack.getString("entry", "unregistered");
return QRcode;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_testing);
ToolDB db = new ToolDB(this);
String c1 = db.QuantReturn();
TextView t3 = (TextView) findViewById(R.id.slot4);
t3.setText(c1);
}
因崩溃而发生的错误的Logcat:
03-19 13:59:08.517: E/AndroidRuntime(10579): FATAL EXCEPTION: main
03-19 13:59:08.517: E/AndroidRuntime(10579): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.SurgicalMate/com.example.SurgicalMate.Testing}: java.lang.NullPointerException
03-19 13:59:08.517: E/AndroidRuntime(10579): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2351)
03-19 13:59:08.517: E/AndroidRuntime(10579): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
03-19 13:59:08.517: E/AndroidRuntime(10579): at android.app.ActivityThread.access$600(ActivityThread.java:151)
03-19 13:59:08.517: E/AndroidRuntime(10579): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1331)
03-19 13:59:08.517: E/AndroidRuntime(10579): at android.os.Handler.dispatchMessage(Handler.java:99)
03-19 13:59:08.517: E/AndroidRuntime(10579): at android.os.Looper.loop(Looper.java:155)
03-19 13:59:08.517: E/AndroidRuntime(10579): at android.app.ActivityThread.main(ActivityThread.java:5454)
03-19 13:59:08.517: E/AndroidRuntime(10579): at java.lang.reflect.Method.invokeNative(Native Method)
03-19 13:59:08.517: E/AndroidRuntime(10579): at java.lang.reflect.Method.invoke(Method.java:511)
03-19 13:59:08.517: E/AndroidRuntime(10579): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
03-19 13:59:08.517: E/AndroidRuntime(10579): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
03-19 13:59:08.517: E/AndroidRuntime(10579): at dalvik.system.NativeStart.main(Native Method)
03-19 13:59:08.517: E/AndroidRuntime(10579): Caused by: java.lang.NullPointerException
03-19 13:59:08.517: E/AndroidRuntime(10579): at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:153)
03-19 13:59:08.517: E/AndroidRuntime(10579): at com.example.SurgicalMate.Testing.QRcodeReturn(Testing.java:23)
03-19 13:59:08.517: E/AndroidRuntime(10579): at com.example.SurgicalMate.ToolDB.QuantReturn(ToolDB.java:76)
03-19 13:59:08.517: E/AndroidRuntime(10579): at com.example.SurgicalMate.Testing.onCreate(Testing.java:38)
03-19 13:59:08.517: E/AndroidRuntime(10579): at android.app.Activity.performCreate(Activity.java:5066)
03-19 13:59:08.517: E/AndroidRuntime(10579): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101)
03-19 13:59:08.517: E/AndroidRuntime(10579): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
03-19 13:59:08.517: E/AndroidRuntime(10579): ... 11 more
提前谢谢
答案 0 :(得分:0)
您必须在调用onCreate
后访问共享首选项。否则上下文将为null。
答案 1 :(得分:0)
尝试这种方式:
public String QRcodeReturn(){
SharedPreferences codeHack = getSharedPreferences(SCAN_RESULT,0);
if (codeHack != null) {
String QRcode = codeHack.getString("entry", "unregistered");
return QRcode;
} else {
return "";
}
}
因为我看到的是, codeHack 为空,您尝试调用null对象的 getString 。
答案 2 :(得分:0)
你的getSharedPreferences()方法返回null - 因此,codeHack解析为null,对此String的进一步操作抛出空指针异常。最有可能的是,您的首选项文件尚未正确设置。