質問

In my activity is user can take a photo and then set a capture image to gridview.And i gridview in have some image(capture image), and 2 edit text.Now i want to save information in 2 edit text with shared preference file in onPause(); and return value from shared preference file to edittext in onResume.

this is my code

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;
}

}

and this is my 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

Thank you

役に立ちましたか?

解決

I see one possible mistake in your code that could cause the NullPointerException.

In the code below the getChildCount returns the amount of childs in the ViewGroup but not the highest child index. You must use size - 1 to get the last child!

Wrong

int size = gridview.getChildCount();
ViewGroup child_grid = (ViewGroup) gridview.getChildAt(size);

Correct

int size = gridview.getChildCount();
ViewGroup child_grid = (ViewGroup) gridview.getChildAt(size - 1);

他のヒント

Although this isnt how i resolved this error. Yes i faced this error several times and in the end learned that

"its main cause is that some component of particular activity has null value"

not necessarily what compiler is pointing at. So just check for null pointer values

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top