我有我的主要活动,其中有几个按钮
其中一个应该打开相机
为了使它更清晰我使用View.onClick()
,所以我将在主Activity上使用Button,其余的将由另一个其他类(Camactivity)管理。
在我的主要活动中:
Button btnrep = (Button)findViewById(R.id.button3);
btnrep.setOnClickListener(new Camactivity(this));
和我的
public class Camactivity extends Activity implements View.OnClickListener
private File imageFile;
private Context appContext;
public MainActivity_1(Context context)
{
appContext = context;
}
@Override
public void onClick(View view) {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);//use intent and pass in mediastore
//mediastore is a databases where image and video are stores and link
imageFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "BreedingGround.bmp");
/*link to a directory - pass in directory where you want to save the pictures and names of the file*/
Uri tempuri = Uri.fromFile(imageFile);//Convert imageFile to a Uri
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, tempuri);//location where u want the image file to be save after taking photo
intent.putExtra(android.provider.MediaStore.EXTRA_VIDEO_QUALITY, 1);//quality of out image, 1 means high quality image
startActivityForResult(intent, 0);//Request code 0 to identify who send the request
}
但是我在startActivityForResult(intent, 0);
FATAL EXCEPTION: main Process: com.example.mohit.softeng, PID: 22075
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.PackageManager android.content.Context.getPackageManager()' on a null object reference
at android.content.ContextWrapper.getPackageManager(ContextWrapper.java:97)
at com.example.mohit.softeng.MainActivity_1.onClick(MainActivity_1.java:60)
at android.view.View.performClick(View.java:5697)
at android.widget.TextView.performClick(TextView.java:10826)
at android.view.View$PerformClick.run(View.java:22526)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7225)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
答案 0 :(得分:0)
非常欢迎您定义View.OnClickListener
子类,但是
我根本没有理由看到Camactivity
,所以只需在View.OnClickListener
上实施MainActivity
即可。
此外,您可能希望startActivityForResult
MainActivity
中onActivityResult
的结果,而不是Camactivity
。
所以,有了这个建议
Button btnrep = (Button)findViewById(R.id.button3);
btnrep.setOnClickListener(this);
如果您的Activity已经实现了OnClickListener
,那么添加一个if语句来检查单击了哪个按钮。
@Override
public void onClick(View view) {
switch (v.getId()) {
case R.id.button3:
openCamera();
}
}
private void openCamera() {
// That other code in Camactivity
}
替代答案......仍未使用new
制作活动类。
Button btnrep = (Button)findViewById(R.id.button3);
btnrep.setOnClickListener(new View.onClickListener() {
@Override
public void onClick(View v) {
Intent cam = new Intent(MainActivity.this, Camactivity.class);
startActivityForResult(cam, 0); // If you need the result in MainAcivity, pass it back from camActivity
// else, just startActivity(cam);
}
});
然后,更新Camactivity
以在创建相机意图后立即启动它。
public class Camactivity extends Activity {
private File imageFile;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);//use intent and pass in mediastore
//mediastore is a databases where image and video are stores and link
imageFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "BreedingGround.bmp");
/*link to a directory - pass in directory where you want to save the pictures and names of the file*/
Uri tempuri = Uri.fromFile(imageFile);//Convert imageFile to a Uri
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, tempuri);//location where u want the image file to be save after taking photo
intent.putExtra(android.provider.MediaStore.EXTRA_VIDEO_QUALITY, 1);//quality of out image, 1 means high quality image
startActivityForResult(intent, 0);//Request code 0 to identify who send the request
}
@Override
public void onActivityResult (int requestCode, int resultCode, Intent data) {
if (requestCode == 0) {
// TODO: Handle camera intent result
}
}
}