我正在传递并显示从一个活动到另一个活动的值,并使用这样的字符串创建文件名:
AU_20140312_160107_65.jpg
AU - 简单的前缀
20140312_160107 - yyyyMMdd_HHmmss
6 - event_id(从LoginActivity.java获取)
5 - operative_id(从LoginActivity.java获取)
请参阅下面的代码:
LoginActivity.java:
static SharedPreferences sharedprefs;
static String event_id, operative_id ;
sharedprefs = getApplicationContext().getSharedPreferences(
"com.example.app", Context.MODE_PRIVATE);
Intent intent = getIntent();
event_id = intent.getStringExtra("event_id");
sharedprefs.edit().putString("EVENT_ID", event_id).commit();
operative_id = intent.getStringExtra("operative_id");
sharedprefs.edit().putString("OPERATIVE_ID", operative_id).commit();
CameraLauncherActivity.java:
static File getOutputMediaFile() {
String event_ID = LoginActivity.sharedprefs.getString("EVENT_ID", null);
Log.d("shared >>> eventID : ", event_ID);
String operative_ID = LoginActivity.sharedprefs.getString("OPERATIVE_ID", null);
Log.d("shared >>> operativeID : ", operative_ID);
timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date());
// file name
mediaFile = new File(LoginActivity.mediaStorageDir.getPath() + File.separator
+ "AU_" + timeStamp + "_" + event_ID + operative_ID + ".jpg");
return mediaFile;
}
首次启动时,日志:
shared >>> eventID :(29917): 12
shared >>> operativeID :(29917): 4
就像你上面看到的那样,一切正常,并且使用日志获得eventID和operativeID
但是一旦我关闭它并重新启动我的应用程序,然后获取 NPE 和日志:
FATAL EXCEPTION: main
java.lang.NullPointerException: println needs a message
at android.util.Log.println_native(Native Method)
at android.util.Log.d(Log.java:155)
app.mac.Camera.CameraLauncherActivity.getOutputMediaFile(CameraLauncherActivity.java:386)
at app.mac.Camera.CameraLauncherActivity$2.onPictureTaken(CameraLauncherActivity.java:362)
at android.hardware.Camera$EventHandler.handleMessage(Camera.java:789)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1036)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:803)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
不是将值直接分配给变量event_id,而是将操作数存储在数据库或首选项中。
而不是像这样直接分配: event_id = intent.getStringExtra(" event_id"); operative_id = intent.getStringExtra(" operative_id");
检查intent.getStringExtra(" event_id")和intent.getStringExtra(" operative_id")是否为null。
如果从存储的值中获取null。 如果不为null则为变量赋值并使用新值覆盖存储的值以供下次使用。
答案 1 :(得分:0)
//Initialization
SharedPreferences _mypref = getApplicationContext().getSharedPreferences("mypref", 0);
//store data in shared preferences
Editor editor = _mypref.edit();
//Storing boolean - true/false;
editor.putBoolean("key_name", true);
//Storing string
editor.putString("key_name", "string value");
//Storing integer
editor.putInt("key_name", "int value");
//Storing float
editor.putFloat("key_name", "float value");
//Storing long
editor.putLong("key_name", "long value");
//commit changes
editor.commit();
// retrive data
//getting String
_mypref .getString("key_name", null);
//getting Integer
_mypref .getInt("key_name", null);
//getting Float
_mypref .getFloat("key_name", null);
//getting Long
_mypref .getLong("key_name", null);
//getting boolean
_mypref.getBoolean("key_name", null);
//remove Data
editor.remove("name");
editor.commit();
//Clear all Data
editor.clear();
editor.commit();