不断得到这个错误不知道为什么每件事情都是完美的我认为。如果有任何帮助
04-10 22:54:25.753 28129-28129/com.ahmed.raja.wrongclick E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.ahmed.raja.wrongclick, PID: 28129
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ahmed.raja.wrongclick/com.ahmed.raja.wrongclick.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.ahmed.raja.wrongclick.MainActivity.onCreate(MainActivity.java:81)
at android.app.Activity.performCreate(Activity.java:5264)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
这是我的MainActivity我认为有一个问题oncreate
方法只是无法找出哪个东西正在崩溃我的应用程序。我已经仔细检查了我的XML文件
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
/**
* Created by DELL on 4/10/2015.
*/
public class MainActivity extends Activity implements SurfaceHolder.Callback {
private final static String LOG_TAG = "DevicePolicyAdmin";
DevicePolicyManager dpm;
ComponentName DevicePolicyAdmin;
private CheckBox truitonAdminEnabledCheckbox;
protected static final int REQUEST_ENABLE = 1;
Button btntake;
Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean previewing = false;
LayoutInflater controlInflater = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
DevicePolicyAdmin = new ComponentName(this,
Dprcv.class);
btntake =(Button)findViewById(R.id.takepicture);
truitonAdminEnabledCheckbox = (CheckBox) findViewById(R.id.checkBox);
//start
// setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
controlInflater = LayoutInflater.from(getBaseContext());
View viewControl = controlInflater.inflate(R.layout.control, null);
ViewGroup.LayoutParams layoutParamsControl
= new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT);
this.addContentView(viewControl, layoutParamsControl);
btntake.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
camera.takePicture(myShutterCallback,
myPictureCallback_RAW, myPictureCallback_JPG);
}
});
//end
}
@Override
protected void onResume() {
super.onResume();
if (isMyDevicePolicyReceiverActive()) {
truitonAdminEnabledCheckbox.setChecked(true);
} else {
truitonAdminEnabledCheckbox.setChecked(false);
}
truitonAdminEnabledCheckbox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
Intent intent = new Intent(
DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(
DevicePolicyManager.EXTRA_DEVICE_ADMIN,
DevicePolicyAdmin);
intent.putExtra(
DevicePolicyManager.EXTRA_ADD_EXPLANATION,
getString(R.string.wow));
startActivityForResult(intent, REQUEST_ENABLE);
} else {
dpm
.removeActiveAdmin(DevicePolicyAdmin);
}
}
});
}
private boolean isMyDevicePolicyReceiverActive() {
return dpm
.isAdminActive(DevicePolicyAdmin);
}
Camera.ShutterCallback myShutterCallback = new Camera.ShutterCallback(){
@Override
public void onShutter() {
// TODO Auto-generated method stub
}};
Camera.PictureCallback myPictureCallback_RAW = new Camera.PictureCallback(){
@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
// TODO Auto-generated method stub
}};
Camera.PictureCallback myPictureCallback_JPG = new Camera.PictureCallback(){
@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
// TODO Auto-generated method stub
/*Bitmap bitmapPicture
= BitmapFactory.decodeByteArray(arg0, 0, arg0.length); */
Uri uriTarget = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new ContentValues());
OutputStream imageFileOS;
try {
imageFileOS = getContentResolver().openOutputStream(uriTarget);
imageFileOS.write(arg0);
imageFileOS.flush();
imageFileOS.close();
Toast.makeText(MainActivity.this,
"waH kiAa selfie hAi :D ",
//+ uriTarget.toString(),
Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
camera.startPreview();
}};
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
if(previewing){
camera.stopPreview();
previewing = false;
}
if (camera != null){
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
previewing = true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
// camera = Camera.open();
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
Log.d("No of cameras", Camera.getNumberOfCameras() + "");
for (int camNo = 0; camNo < Camera.getNumberOfCameras(); camNo++) {
Camera.CameraInfo camInfo = new Camera.CameraInfo();
Camera.getCameraInfo(camNo, camInfo);
if (camInfo.facing==(Camera.CameraInfo.CAMERA_FACING_FRONT)) {
camera = Camera.open(camNo);
}
}
if (camera == null) {
// no front-facing camera, use the first back-facing camera instead.
// you may instead wish to inform the user of an error here...
camera = Camera.open();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
camera.stopPreview();
camera.release();
camera = null;
previewing = false;
}
}
答案 0 :(得分:0)
在为Button btnTake设置点击监听器时,在调用以下行之前似乎没有初始化您的摄像头实例:
camera.takePicture(myShutterCallback,
myPictureCallback_RAW, myPictureCallback_JPG);
因此你得到了NullPointerException。
您需要在使用前初始化相机实例。我看到你在surfaceCreated()方法中这样做,但是在onCreate()之后调用,因此Camera实例解析为null。
在初始化设置按钮的单击侦听器之前,在onCreate()方法的某处具有以下代码。
try {
c = Camera.open(); // attempt to get a Camera instance
}
catch (Exception e){
// Camera is not available (in use or does not exist)
}