0

imageview と、4 つの edittext フィールドを持つボタン アップロード アバターがあります (データベースにエントリを挿入しても問題ありません。次に、アップロード アバターをクリックすると、組み込みのギャラリーに移動し、表示する画像を選択できます。しかし、組み込みのギャラリーで画像をクリックすると、画像が画像ビューに表示されず、エラーが発生します。

そして、ここに私の Main.java があります:

   public class Main extends Activity {
   DBAdapter db = new DBAdapter (this);
   private ImageView img;
  private String selectedImagePath;
   private static final int SELECT_IMAGE=0;

   @Override
   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final ImageView img = (ImageView) findViewById(R.id.imageViewavatar);
    Button upload = (Button) findViewById(R.id.buttonuimage);
    upload.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            Intent imageintent = new Intent();
            imageintent.setType("image/*");
            imageintent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(imageintent, "Select Picture"), SELECT_IMAGE);


        }
    });


Button save = (Button) findViewById(R.id.savebutton);
save.setOnClickListener(new OnClickListener() {

    public void onClick(View arg0) {
        boolean success = true;
        try
        {
        db.open();
            EditText editTextfirstname = (EditText) findViewById(R.id.editText1);
            EditText editTextlastname = (EditText) findViewById(R.id.editText2);
            EditText editTextcity = (EditText) findViewById(R.id.editText3);
            EditText editTextstate = (EditText) findViewById(R.id.editText4);


            String firstnameValue = editTextfirstname.getText().toString();
            String lastnameValue = editTextlastname.getText().toString();
            String cityValue = editTextcity.getText().toString();
            String stateValue = editTextstate.getText().toString();
            String imageValue = selectedImagePath.toString();

            db.insertContact(firstnameValue, lastnameValue, cityValue, stateValue, imageValue);
            }
        catch (Exception e){
            success = false;    
        }finally{
            if(success){
            Toast toast = Toast.makeText(Main.this, "Successfully added contact", 5000);
            toast.show();
            MediaPlayer mp = MediaPlayer.create(Main.this, R.raw.beep);
            mp.start();
            Intent intnt = new Intent(Main.this, Main.class);
            startActivity(intnt);



                }

            }
        }
    private void show_mesg(String msg) {
        Toast.makeText(getApplicationContext(),msg,Toast.LENGTH_SHORT).show();
    }
    });
    }
 public void added (View v){
Intent i = new Intent(Main.this, Second.class);
startActivity(i);

 }

 public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_IMAGE) {
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
            Bitmap bm = BitmapFactory.decodeFile(selectedImagePath);
            img.setImageBitmap(bm);


        }
    }
 }

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
 }

これはLogCatに表示されているものです:

08-31 09:52:21.674: E/AndroidRuntime(459): FATAL EXCEPTION: main
08-31 09:52:21.674: E/AndroidRuntime(459): java.lang.RuntimeException: Failure delivering      result ResultInfo{who=null, request=0, result=-1, data=Intent { dat=content://media/external/images  /media/2 }} to activity {com.dtan.testcontacts/com.dtan.testcontacts.Main}:  java.lang.NullPointerException
08-31 09:52:21.674: E/AndroidRuntime(459):  at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
08-31 09:52:21.674: E/AndroidRuntime(459):  at android.app.ActivityThread.handleSendResult(ActivityThread.java:2574)
 08-31 09:52:21.674: E/AndroidRuntime(459):     at android.app.ActivityThread.access$2000(ActivityThread.java:117)
08-31 09:52:21.674: E/AndroidRuntime(459):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:961)
08-31 09:52:21.674: E/AndroidRuntime(459):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-31 09:52:21.674: E/AndroidRuntime(459):  at android.os.Looper.loop(Looper.java:123)
08-31 09:52:21.674: E/AndroidRuntime(459):  at android.app.ActivityThread.main(ActivityThread.java:3683)
08-31 09:52:21.674: E/AndroidRuntime(459):  at java.lang.reflect.Method.invokeNative(Native Method)
08-31 09:52:21.674: E/AndroidRuntime(459):  at java.lang.reflect.Method.invoke(Method.java:507)
08-31 09:52:21.674: E/AndroidRuntime(459):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-31 09:52:21.674: E/AndroidRuntime(459):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-31 09:52:21.674: E/AndroidRuntime(459):  at dalvik.system.NativeStart.main(Native Method)
08-31 09:52:21.674: E/AndroidRuntime(459): Caused by: java.lang.NullPointerException
08-31 09:52:21.674: E/AndroidRuntime(459):  at com.dtan.testcontacts.Main.onActivityResult(Main.java:113)
08-31 09:52:21.674: E/AndroidRuntime(459):  at android.app.Activity.dispatchActivityResult(Activity.java:3908)
08-31 09:52:21.674: E/AndroidRuntime(459):  at android.app.ActivityThread.deliverResults(ActivityThread.java:2528)
08-31 09:52:21.674: E/AndroidRuntime(459):  ... 11 more
4

1 に答える 1

0

わかった!!。については、以下の編集済みコードを参照してください。

私は交換しました:

 public void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (resultCode == RESULT_OK) {
    if (requestCode == SELECT_IMAGE) {
        Uri selectedImageUri = data.getData();
        selectedImagePath = getPath(selectedImageUri);
        Bitmap bm = BitmapFactory.decodeFile(selectedImagePath);
        img.setImageBitmap(bm);

と:

 public void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (resultCode == RESULT_OK) {
    if (requestCode == SELECT_IMAGE) {
        Uri selectedImageUri = data.getData();
        selectedImagePath = getPath(selectedImageUri);
        File f = new File (selectedImagePath);
            Bitmap bm = BitmapFactory.decodeFile(f.getAbsolutePath());
            img.setImageBitmap(bm);
于 2012-08-31T10:27:30.153 に答える