简单的Android摄像头应用程序-旋转导致NullPointerException
我是Android开发的新手,当时正在玩相机。我只是想创建一个简单的应用程序,可以使用本机相机应用程序拍照,并返回该图像的文件路径 我的工作正常,但我犯了一个奇怪的错误。当我点击按钮启动相机时,如果我在相机应用程序中更改屏幕方向,并且在退出相机之前不切换回(当我被问及是否要重新拍摄时,按下“完成”按钮),则会引发NullPointerException 我在这里有点不知所措,不知该如何解决这个问题,所以任何信息都会有帮助的 以下是我目前掌握的代码:简单的Android摄像头应用程序-旋转导致NullPointerException,android,camera,nullpointerexception,android-orientation,Android,Camera,Nullpointerexception,Android Orientation,我是Android开发的新手,当时正在玩相机。我只是想创建一个简单的应用程序,可以使用本机相机应用程序拍照,并返回该图像的文件路径 我的工作正常,但我犯了一个奇怪的错误。当我点击按钮启动相机时,如果我在相机应用程序中更改屏幕方向,并且在退出相机之前不切换回(当我被问及是否要重新拍摄时,按下“完成”按钮),则会引发NullPointerException 我在这里有点不知所措,不知该如何解决这个问题,所以任何信息都会有帮助的 以下是我目前掌握的代码: package com.CameraTest;
package com.CameraTest;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class CameraTestActivity extends Activity
{
private static final int CAMERA_PIC_REQUEST = 1234;
protected Uri mCapturedImageURI;
protected TextView textview;
protected Button button;
/** Called when the activity is first created. */
@Override public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.button1);
textview = (TextView) findViewById(R.id.textView1);
if (savedInstanceState != null) {
textview.setText(savedInstanceState.getString("textview"));
}
button.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
String fileName = "temp.jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
mCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
startActivityForResult(intent, CAMERA_PIC_REQUEST);
}
});
}
@Override protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
outState.putString("textview", textview.getText().toString());
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_PIC_REQUEST && resultCode == -1)
{
String[] projection = { MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(this.mCapturedImageURI, projection, null, null, null);
int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String capturedImageFilePath = cursor.getString(column_index_data);
this.textview.setText("File Path:" + capturedImageFilePath);
}
}
}
您必须更改清单文件 在您的清单中,只需替换下面的代码
<activity android:name=".CameraTestActivity"
android:label="@string/app_name" android:configChanges="keyboardHidden|orientation">
用你的代码
<activity android:name=".CameraTestActivity"
android:label="@string/app_name">
是的,logcat错误会告诉您NullPointerException的确切行,并保存其他宝贵信息。