2012-11-09 152 views
0

我試圖讓照片就拍攝之後,但onActivityResult不叫:照片拍攝完成後,OnActivityResult不叫

public class questionListView extends LinearLayout { 

protected static final Integer PICTURE_RESULT = 1; 
private Context ctx; 

public Uri path; 

public questionListView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    ctx = context; 
    init(); 
} 

public questionListView(Context context) { 
    super(context); 
    ctx = context; 
    init(); 
} 

private void init() { 

    LayoutInflater factory = LayoutInflater.from(getContext()); 
    View myView = factory.inflate(R.layout.view1, null); 

    LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); 
    myView.setLayoutParams(params); 

    addView(myView); 

    Button btaddphoto = (Button) myView.findViewById(R.id.btAddPhoto); 

    btaddphoto.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 

      String _path = Environment.getDataDirectory() + File.separator + "pic1.png"; 
      File file = new File(_path); 
      Uri outputFileUri = Uri.fromFile(file); 

      Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); 
      intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); 

      ((Activity) ctx).startActivityForResult(intent, PICTURE_RESULT); 

     } 
    }); 
} 

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

    Bitmap mBitmap; 
    String _path = Environment.getDataDirectory() + File.separator + "pic1.png"; 
    mBitmap = BitmapFactory.decodeFile(_path); 
    if (mBitmap == null) { 
     // bitmap still null 
    } else { 

     // kleine bitmap maken 
     // deze in een imageview toevoegen aan layout lvPhotos 

    } 
} 

}

我行

得到一個錯誤
super.OnActivityResult(requestCode, resultCode, data); 

錯誤消息:

The method onActivityResult(int, int, Intent) is undefined for the type LinearLayout 

如果我離開了,onActivityResult函數不會被調用。

我在做什麼錯?

RG, 埃裏克

+1

'onActivityResult'應該在你的活動中! –

回答

2

要調用從LinearLayoutonActivityResult()onActivityResult()方法特定於活動

欲瞭解更多詳情,請參閱官方Android developers site對待這個問題。

在附註上,最好從XML創建佈局而不是從Java代碼創建佈局。不僅代碼更快,而且性能也更快。