我在我的应用程序中有这个自定义相机活动,它运行得很好,但是当我在相机活动中按下主页按钮,然后返回应用程序时,它崩溃了,并且logcat说有一个空指针异常在表面视图创建.在onResume上调用此设置方法:
public void setup()
{
preview = new CameraPreview(this);
((FrameLayout) findViewById(R.id.camera_preview)).addView(preview);
head=(ImageView)findViewById(R.id.head);
head.setX((float)(Shared.screenWidth/5*1.8));
head.setY((float)(Shared.screenHeight*0.03));
LayoutParams params = (LayoutParams) head.getLayoutParams();
params.width = (int)((Shared.screenWidth/5*3.2)-(head.getX()));
params.height=(int)((Shared.screenHeight/3.8)-(head.getY()));
head.setLayoutParams(params);
body=(ImageView)findViewById(R.id.body);
body.setX((float)(Shared.screenWidth/7));
body.setY((float)(Shared.screenHeight/3.8));
LayoutParams params2 = (LayoutParams) body.getLayoutParams();
params2.width = (int)((Shared.screenWidth/7*6)-(body.getX()));
params2.height=(int)((Shared.screenHeight)-(body.getY()));
body.setLayoutParams(params2);
go=(Button)findViewById(R.id.go);
again=(Button)findViewById(R.id.again);
stop=(ImageButton)findViewById(R.id.stop);
stop.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
Intent i =new Intent(CamActivity.this,Main.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
});
btn = (ImageButton) findViewById(R.id.takePic);
btn.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);
btn.setVisibility(View.INVISIBLE);
btn.setEnabled(false);
go.setVisibility(View.VISIBLE);
go.setEnabled(true);
head.setVisibility(View.INVISIBLE);
body.setVisibility(View.INVISIBLE);
go.setOnClickListener( …Run Code Online (Sandbox Code Playgroud)