我有一个活动,其中有一个打开相机应用程序的按钮然后我会尝试裁剪。但我的活动甚至没有开始。这可能与按钮有关,崩溃发生在setonclicklistener之后。你能帮忙吗?
这是我的MainActivityCrop.java:
public class MainActivityCrop extends Activity {
//keep track of camera capture intent
final int CAMERA_CAPTURE = 1;
//captured picture uri
private Uri picUri;
//keep track of cropping intent
final int PIC_CROP = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("onResume: ", " BB");
setContentView(R.layout.activity_main_activity_crop);
Log.d("onResume: ", " CC");
//retrieve a reference to the UI button
Button captureBtn = (Button)findViewById(R.id.capture_btn);
//handle button clicks
Log.d("onResume: ", " DD");
captureBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d("onResume: ", " EE");
try {
Log.d("onResume: ", " FF");
//use standard intent to capture an image
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//we will handle the returned data in onActivityResult
startActivityForResult(captureIntent, CAMERA_CAPTURE);
}
catch(ActivityNotFoundException anfe){
//display an error message
String errorMessage = "Whoops - your device doesn't support capturing images!";
Toast toast = Toast.makeText(null, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
//user is returning from capturing an image using the camera
if(requestCode == CAMERA_CAPTURE){
//get the Uri for the captured image
picUri = data.getData();
//carry out the crop operation
performCrop();
}
//user is returning from cropping the image
else if(requestCode == PIC_CROP){
//get the returned data
Bundle extras = data.getExtras();
//get the cropped bitmap
Bitmap thePic = extras.getParcelable("data");
//retrieve a reference to the ImageView
ImageView picView = (ImageView)findViewById(R.id.picture);
//display the returned cropped image
picView.setImageBitmap(thePic);
}
}
}
private void performCrop(){
try {
//call the standard crop action intent (the user device may not support it)
Intent cropIntent = new Intent("com.android.camera.action.CROP");
//indicate image type and Uri
cropIntent.setDataAndType(picUri, "image/*");
//set crop properties
cropIntent.putExtra("crop", "true");
//indicate aspect of desired crop
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
//indicate output X and Y
cropIntent.putExtra("outputX", 256);
cropIntent.putExtra("outputY", 256);
//retrieve data on return
cropIntent.putExtra("return-data", true);
//start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, PIC_CROP);
}
catch(ActivityNotFoundException anfe){
//display an error message
String errorMessage = "Whoops - your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
}
这是我的fragment_main_activity_crop.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/capture_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/capture" />
<ImageView
android:id="@+id/picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/picture"
android:layout_margin="5dp"
/>
</LinearLayout>
这些是logcat脚本:
06-03 16:44:26.710: E/AndroidRuntime(5036): FATAL EXCEPTION: main
06-03 16:44:26.710: E/AndroidRuntime(5036): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.croptry/com.example.croptry.MainActivityCrop}: java.lang.NullPointerException
06-03 16:44:26.710: E/AndroidRuntime(5036): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1978)
06-03 16:44:26.710: E/AndroidRuntime(5036): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2003)
06-03 16:44:26.710: E/AndroidRuntime(5036): at android.app.ActivityThread.access$600(ActivityThread.java:123)
06-03 16:44:26.710: E/AndroidRuntime(5036): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1169)
06-03 16:44:26.710: E/AndroidRuntime(5036): at android.os.Handler.dispatchMessage(Handler.java:99)
06-03 16:44:26.710: E/AndroidRuntime(5036): at android.os.Looper.loop(Looper.java:137)
06-03 16:44:26.710: E/AndroidRuntime(5036): at android.app.ActivityThread.main(ActivityThread.java:4446)
06-03 16:44:26.710: E/AndroidRuntime(5036): at java.lang.reflect.Method.invokeNative(Native Method)
06-03 16:44:26.710: E/AndroidRuntime(5036): at java.lang.reflect.Method.invoke(Method.java:511)
06-03 16:44:26.710: E/AndroidRuntime(5036): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-03 16:44:26.710: E/AndroidRuntime(5036): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-03 16:44:26.710: E/AndroidRuntime(5036): at dalvik.system.NativeStart.main(Native Method)
06-03 16:44:26.710: E/AndroidRuntime(5036): Caused by: java.lang.NullPointerException
06-03 16:44:26.710: E/AndroidRuntime(5036): at com.example.croptry.MainActivityCrop.onCreate(MainActivityCrop.java:37)
06-03 16:44:26.710: E/AndroidRuntime(5036): at android.app.Activity.performCreate(Activity.java:4465)
06-03 16:44:26.710: E/AndroidRuntime(5036): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-03 16:44:26.710: E/AndroidRuntime(5036): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1942)
答案 0 :(得分:0)
如果
发生异常,我看不到错误captureBtn.setOnClickListener...
尝试清理项目并重建它,看看之后会发生什么。有时它会有所帮助。
修改强>
您使用:
setContentView(R.layout.activity_main_activity_crop);
,您的按钮位于
fragment_main_activity_crop.xml
这是错误的原因
答案 1 :(得分:0)
这是因为你的相机。尝试从模拟器打开相机,你会发现你无法访问它。
我认为它会对你有所帮助。