1

我使用选项卡活动作为主要活动,其中有 4 个选项卡。一个选项卡是具有三个按钮的活动组。每个按钮都是一个活动。我在其中一项活动中使用相机。我知道它的工作原理,因为我已在存储卡中签入图像已保存,但它没有调用 Onactivityresult() 方法。

这是为了显示 ContentView

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.enter_expenses, null);
    this.setContentView(viewToLoad);            

此代码用于拍摄图像

             dbimgguid = UUID.randomUUID();
             imagename =dbimgguid.toString();
             Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
             photo = new File(Environment.getExternalStorageDirectory(), "Expenses"+imagename+".jpg");
             intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(photo));
             imageurl = Uri.fromFile(photo); 
             startActivityForResult(intent, CAMERA_RECEIPTREQUEST); 

这是活动结果

     @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
     super.onActivityResult(requestCode, resultCode, data);  
     System.out.println(requestCode);
     System.out.println(resultCode);
     switch(requestCode)
     {
     case CAMERA_RECEIPTREQUEST:  
         if(resultCode== Activity.RESULT_OK)
         {
         //Toast.makeText(this, "Receipt Image Saved", Toast.LENGTH_SHORT).show();
         BitmapFactory.Options options = new BitmapFactory.Options();
         options.inSampleSize = 8;
         //ImageView jpgView = (ImageView)findViewById(R.id.imageView1);
         Bitmap receipt = BitmapFactory.decodeFile(photo.toString(),options);  
         String image = "Expenses"+imagename+".jpg"; 
         Intent imagepass = new Intent(ExpensesActivity.this,ReviewReceiptImage.class);
         imagepass.putExtra("receipt", receipt);
         imagepass.putExtra("imagename", image);
         startActivityForResult(imagepass, CAMERA_CONFIRMRECEIPT); 
         Toast.makeText(this, "Receipt Image Saved", Toast.LENGTH_SHORT).show();
         }

     case CAMERA_CONFIRMRECEIPT:

         Toast.makeText(this, "Receipt Image Saved", Toast.LENGTH_SHORT).show();
         if(resultCode == CAMERA_CONFIRMRECEIPT)
         {
             take_receipt.setEnabled(false);
             take_receipt.setVisibility(View.GONE);
             show_receipt.setEnabled(true);
             show_receipt.setVisibility(View.VISIBLE);
         } 
         break;
     }
 }

谢谢你们的帮助

4

1 回答 1

2

我已经解决了我的问题。请在下面查看我的答案,

我终于想通/记得这个问题是因为Android将只允许嵌套的子活动层......即子活动不能嵌套子活动。要解决这个问题:

从您的子活动调用getParent().startActivityForResult()您的父(活动组)将能够处理 onActivityResult。所以我创建了一个ActivityGroup的子类并处理了这个onActivityResult。如果需要,您可以将该结果重新路由回子活动。只需通过 获取当前活动getLocalActivityManager().getCurrentActivity()。我的子活动继承自自定义活动,因此我handleActivityResult(requestCode, resultCode, data)在该子类中添加了一个供 ActivityGroup 调用的。

用确切的代码回答上述问题

ActivityGroup->SubActivity->onActivityResult->ActivityGroup(onActivityResult)->subActivity(onActivityResult)

这是 ActivityGroup 类,您必须在其中为所有子活动定义 onActivityResult,

@Override
public void onActivityResult(int requestCode,int resultCode, Intent data)
{

         switch(requestCode)
            {

            case CAMERA_RECEIPTREQUEST:   
            if(resultCode == Activity.RESULT_OK)
            {
            ExpensesActivity activity = (ExpensesActivity) getLocalActivityManager().getCurrentActivity();
            activity.onActivityResult(requestCode, resultCode, data);
            }
            break;

            case CAMERA_CONFIRMRECEIPT:
            ExpensesActivity activity1 = (ExpensesActivity) getLocalActivityManager().getCurrentActivity();
            activity1.onActivityResult(requestCode, resultCode, data);   
            }

        }

在子活动中,您必须将视图设置如下

    View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.enter_time, null);
    this.setContentView(viewToLoad);

然后在子Activity onActivityResulty()

    @Override
    public void onActivityResult(int requestCode,int resultCode, Intent data)
    {
     //Some Code
    }
于 2012-05-28T09:20:15.423 回答