在我的活动中,用户可以拍摄照片,然后将拍摄图像设置为gridview.And gridview中有一些图像(捕获图像)和2个编辑文本。现在我想将信息保存在2个编辑文本中共享onPause()中的首选项文件;并将共享首选项文件中的值返回到onResume中的edittext。
这是我的代码
public class MainActivity extends Activity {
private Button btPhoto, btSave;
private GridView gridview;
private File imgFile;
private String path;
private ArrayList<File> List_File;
private static final int imageCode = 100;
private Context context;
SharedPreferences preference;
SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btPhoto = (Button) findViewById(R.id.btPhoto);
btSave = (Button) findViewById(R.id.bt_save);
gridview = (GridView) findViewById(R.id.gridview);
path = "/TongFolder/";
context = this;
List_File = new ArrayList<File>();
File imgDir = new File(Environment.getExternalStorageDirectory()+path);
imgDir.mkdirs();
boolean check = imgDir.isDirectory();
if(check == true){
btPhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
capture();
}
});
}else{
System.out.println("not create");
}
}
public void capture(){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
imgFile = new File(Environment.getExternalStorageDirectory()+path,
"img_"+System.currentTimeMillis()+".jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imgFile));
startActivityForResult(intent, imageCode);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == imageCode && resultCode == RESULT_OK){
//add path to arrayList
List_File.add(imgFile);
for (File file : List_File) {
String showPath = file.getPath();
System.out.println(showPath);
}
gridview.setAdapter(new gridViewAdapter(context, List_File));
}
}
public void onPause(){
super.onPause();
//initialize
preference = getPreferences(Context.MODE_PRIVATE);
EditText edit_num = new EditText(getApplicationContext());
EditText edit_info = new EditText(getApplicationContext());
//count child of gridview
int size = gridview.getChildCount();
System.out.println("size_"+size);
if(size == 0){
}else{
ViewGroup child_grid = (ViewGroup) gridview.getChildAt(size);
//get editText in child_gridview
edit_num = (EditText) child_grid.findViewById(R.id.editText_num);
edit_info = (EditText) child_grid.findViewById(R.id.editText_info);
String num = edit_num.getText().toString();
String info = edit_info.getText().toString();
//save data to sharedpreference file
editor = preference.edit();
editor.putString("number_recipe_"+size, num);
editor.putString("info_recipe_"+size, info);
editor.commit();
System.out.println("data in position "+size+" is "+num+"and"+info);
}
}
public void onResume(){
super.onResume();
//initialize
EditText edit_num = new EditText(getApplicationContext());
EditText edit_info = new EditText(getApplicationContext());
//count child in gridview
int size = gridview.getChildCount();
System.out.println("onResume_child in gridview have "+size);
//for loop to put data in shared preference to edittext(2)
for(int i = 0; i<size; i++){
ViewGroup child_grid = (ViewGroup) gridview.getChildAt(i);
//findviewById edittext in childAt (i)
edit_num = (EditText) child_grid.findViewById(R.id.editText_num);
edit_info = (EditText) child_grid.findViewById(R.id.editText_info);
//get value from shared preference file and set text to editText
String number = preference.getString("number_recipe_"+i, null);
String information = preference.getString("info_recipe_"+i, null);
edit_num.setText(number);
edit_info.setText(information);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
这是我的logcat
02-19 21:11:51.136: E/AndroidRuntime(15891): FATAL EXCEPTION: main
02-19 21:11:51.136: E/AndroidRuntime(15891): java.lang.RuntimeException: Unable to pause activity {com.example.imagecapture/com.example.imagecapture.MainActivity}: java.lang.NullPointerException
02-19 21:11:51.136: E/AndroidRuntime(15891): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2902)
02-19 21:11:51.136: E/AndroidRuntime(15891): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2858)
02-19 21:11:51.136: E/AndroidRuntime(15891): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2836)
02-19 21:11:51.136: E/AndroidRuntime(15891): at android.app.ActivityThread.access$900(ActivityThread.java:140)
02-19 21:11:51.136: E/AndroidRuntime(15891): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1248)
02-19 21:11:51.136: E/AndroidRuntime(15891): at android.os.Handler.dispatchMessage(Handler.java:99)
02-19 21:11:51.136: E/AndroidRuntime(15891): at android.os.Looper.loop(Looper.java:137)
02-19 21:11:51.136: E/AndroidRuntime(15891): at android.app.ActivityThread.main(ActivityThread.java:4921)
02-19 21:11:51.136: E/AndroidRuntime(15891): at java.lang.reflect.Method.invokeNative(Native Method)
02-19 21:11:51.136: E/AndroidRuntime(15891): at java.lang.reflect.Method.invoke(Method.java:511)
02-19 21:11:51.136: E/AndroidRuntime(15891): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
02-19 21:11:51.136: E/AndroidRuntime(15891): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
02-19 21:11:51.136: E/AndroidRuntime(15891): at dalvik.system.NativeStart.main(Native Method)
02-19 21:11:51.136: E/AndroidRuntime(15891): Caused by: java.lang.NullPointerException
02-19 21:11:51.136: E/AndroidRuntime(15891): at com.example.imagecapture.MainActivity.onPause(MainActivity.java:123)
02-19 21:11:51.136: E/AndroidRuntime(15891): at android.app.Activity.performPause(Activity.java:5304)
02-19 21:11:51.136: E/AndroidRuntime(15891): at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1240)
02-19 21:11:51.136: E/AndroidRuntime(15891): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2889)
02-19 21:11:51.136: E/AndroidRuntime(15891): ... 12 more
谢谢
答案 0 :(得分:2)
我在代码中看到一个可能导致NullPointerException的错误。
在下面的代码中,getChildCount返回ViewGroup中的子项数量,但不返回最高子项索引。你必须使用 size - 1 来获得最后一个孩子!
<强>错误强>
int size = gridview.getChildCount();
ViewGroup child_grid = (ViewGroup) gridview.getChildAt(size);
<强>正确强>
int size = gridview.getChildCount();
ViewGroup child_grid = (ViewGroup) gridview.getChildAt(size - 1);