Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 通过其他活动更改布局_Android_Android Activity_Layout Inflater_Onactivityresult - Fatal编程技术网

Android 通过其他活动更改布局

Android 通过其他活动更改布局,android,android-activity,layout-inflater,onactivityresult,Android,Android Activity,Layout Inflater,Onactivityresult,有没有办法通过Gallery.class更改Main.class类的ImageView 我的主类onClick调用Gallery.class的方法setImage public void setImage(int currentView) { Log.d("fgdsf","dumaan dito"); this.currentView = currentView; Intent gallery = new Intent(Intent.ACTION_PICK,

有没有办法通过Gallery.class更改Main.class类的ImageView

我的主类onClick调用Gallery.class的方法setImage

public void setImage(int currentView) {

    Log.d("fgdsf","dumaan dito");
    this.currentView = currentView;
    Intent gallery = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(gallery, RESULT_LOAD_IMAGE);

}
这是onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
            && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        LayoutInflater inflater = (LayoutInflater) mActivity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.activity_main, null);

        if (currentView == 0) {
            ImageView imageView = (ImageView) v
                    .findViewById(R.id.imageView1);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        } 
        if(currentView==1)
        {
            ImageView imageView = (ImageView) v
                    .findViewById(R.id.imageView2);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        }

    }
}
我得到了这个错误

08-07 08:35:08.208:E/AndroidRuntime(15689):由以下原因引起:java.lang.NullPointerException 08-07 08:35:08.208:E/AndroidRuntime(15689):在android.app.Activity.startActivityForResult(Activity.java:3370)


我想你做不到。您只能控制当前活动及其组件的布局。由于该组件不在当前活动的视图中(在您的示例中是Gallery),访问它将引发NullPointerException。您可以做的是,将Gallery上选定图像的路径传递给Main类,然后检索图像路径并将其设置为ImageView。

我认为您最好的选择是在Gallery.class中设置一个界面,如

public class Gallery extends Activity {

    public interface MyClickListener {
        public void OnClick();
    }

    private MyClickListener listener;

然后在onClick中,设置onClick(listener.onClick())。在Main.class中,您必须导入“MyClickListener”,并定义要执行的任何操作。

用于捕获图像

这是从安卓4.0开始的

之前它返回图像uri路径,但现在它从gallery返回位图

所以要解决这个问题

File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/", "image" + new Date().getTime() + ".jpg");

Uri imgUri = Uri.fromFile(file);
String imagepath = file.getAbsolutePath();

final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imagepath);
startActivityForResult(intent, CAPTURE_IMAGE);


@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != Activity.RESULT_CANCELED) {
            if (requestCode == CAPTURE_IMAGE) {
                imgUser.setImageBitmap(BitmapFactory.decodeFile(imagepath);
            } else {
                super.onActivityResult(requestCode, resultCode, data);
            }
        }

    }
用于从Gallary拾取图像

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, ""), PICK_IMAGE);


public String getAbsolutePath(Uri uri) {
        String[] projection = { MediaColumns.DATA };
        @SuppressWarnings("deprecation")
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        if (cursor != null) {
            int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } else
            return null;
    }

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != Activity.RESULT_CANCELED) {
            if (requestCode == PICK_IMAGE) {

           imgUser.setImageBitmap(BitmapFactory.decodeFile(getAbsolutePath(data.getData()));
            }  else {
                super.onActivityResult(requestCode, resultCode, data);
            }
        }

    }