onActivityResult不是TabHost子活动中的triger

时间:2015-09-21 11:30:55

标签: android android-tabhost onactivityresult

使用startActivityForResult时,Tab主机和选项卡中的活动有问题,但onActivityResult 不会消失。

A类是主要的tabhost

A.java

    setContentView(R.layout.a);
    mLocalActivityManager = new LocalActivityManager(this, false);
    mLocalActivityManager.dispatchCreate(savedInstanceState);

    TabHost tabHost = (TabHost) findViewById(R.id.tabhost);
    tabHost.setup(mLocalActivityManager);

    TabHost.TabSpec tabSpec1 = tabHost.newTabSpec("tab1")
            .setIndicator("tab1")
            .setContent(new Intent(this, B.class));

B类是作为标签主持人的孩子的活动

B.java

public class B extends AppCompatActivity {
private ImageView image;
private int SELECT_IMAGE = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_b);
    image = (ImageView) findViewById(R.id.image);
    image.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AlertDialog.Builder builder =
                    new AlertDialog.Builder(B.this);
            builder.setMessage("Select Picture?");
            builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    Intent intent = new Intent();
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_IMAGE);

                }
            });
            builder.setNegativeButton("Cancle", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            builder.show();
        }
    });
}

}


protected  void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == SELECT_IMAGE) {
        if (resultCode == B.RESULT_OK) {
            if (data != null) {
                try {
                    Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), data.getData());
                    image.setImageBitmap(bitmap);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

我希望B.java中的ImageView 图像在从图库中选择后更改其图像。但是当我选择时,它不会改变任何东西。

感谢您的帮助!

0 个答案:

没有答案