我有一个 imageview 和一个带有 4 个 edittext 字段的按钮上传头像(当我将条目插入数据库时,这一切正常。然后,当我点击上传头像时,它会转到内置画廊,我可以在其中选择要显示的图像imageview。但是,当我单击内置图库中的图像时,它不会在 imageview 上显示图像。相反,它给了我一个错误。
这是我的 Main.java:
public class Main extends Activity {
DBAdapter db = new DBAdapter (this);
private ImageView img;
private String selectedImagePath;
private static final int SELECT_IMAGE=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ImageView img = (ImageView) findViewById(R.id.imageViewavatar);
Button upload = (Button) findViewById(R.id.buttonuimage);
upload.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent imageintent = new Intent();
imageintent.setType("image/*");
imageintent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(imageintent, "Select Picture"), SELECT_IMAGE);
}
});
Button save = (Button) findViewById(R.id.savebutton);
save.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
boolean success = true;
try
{
db.open();
EditText editTextfirstname = (EditText) findViewById(R.id.editText1);
EditText editTextlastname = (EditText) findViewById(R.id.editText2);
EditText editTextcity = (EditText) findViewById(R.id.editText3);
EditText editTextstate = (EditText) findViewById(R.id.editText4);
String firstnameValue = editTextfirstname.getText().toString();
String lastnameValue = editTextlastname.getText().toString();
String cityValue = editTextcity.getText().toString();
String stateValue = editTextstate.getText().toString();
String imageValue = selectedImagePath.toString();
db.insertContact(firstnameValue, lastnameValue, cityValue, stateValue, imageValue);
}
catch (Exception e){
success = false;
}finally{
if(success){
Toast toast = Toast.makeText(Main.this, "Successfully added contact", 5000);
toast.show();
MediaPlayer mp = MediaPlayer.create(Main.this, R.raw.beep);
mp.start();
Intent intnt = new Intent(Main.this, Main.class);
startActivity(intnt);
}
}
}
private void show_mesg(String msg) {
Toast.makeText(getApplicationContext(),msg,Toast.LENGTH_SHORT).show();
}
});
}
public void added (View v){
Intent i = new Intent(Main.this, Second.class);
startActivity(i);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_IMAGE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
Bitmap bm = BitmapFactory.decodeFile(selectedImagePath);
img.setImageBitmap(bm);
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
这是 LogCat 上显示的内容:
08-31 09:52:21.674: E/AndroidRuntime(459): FATAL EXCEPTION: main
08-31 09:52:21.674: E/AndroidRuntime(459): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { dat=content://media/external/images /media/2 }} to activity {com.dtan.testcontacts/com.dtan.testcontacts.Main}: java.lang.NullPointerException
08-31 09:52:21.674: E/AndroidRuntime(459): at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
08-31 09:52:21.674: E/AndroidRuntime(459): at android.app.ActivityThread.handleSendResult(ActivityThread.java:2574)
08-31 09:52:21.674: E/AndroidRuntime(459): at android.app.ActivityThread.access$2000(ActivityThread.java:117)
08-31 09:52:21.674: E/AndroidRuntime(459): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:961)
08-31 09:52:21.674: E/AndroidRuntime(459): at android.os.Handler.dispatchMessage(Handler.java:99)
08-31 09:52:21.674: E/AndroidRuntime(459): at android.os.Looper.loop(Looper.java:123)
08-31 09:52:21.674: E/AndroidRuntime(459): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-31 09:52:21.674: E/AndroidRuntime(459): at java.lang.reflect.Method.invokeNative(Native Method)
08-31 09:52:21.674: E/AndroidRuntime(459): at java.lang.reflect.Method.invoke(Method.java:507)
08-31 09:52:21.674: E/AndroidRuntime(459): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-31 09:52:21.674: E/AndroidRuntime(459): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-31 09:52:21.674: E/AndroidRuntime(459): at dalvik.system.NativeStart.main(Native Method)
08-31 09:52:21.674: E/AndroidRuntime(459): Caused by: java.lang.NullPointerException
08-31 09:52:21.674: E/AndroidRuntime(459): at com.dtan.testcontacts.Main.onActivityResult(Main.java:113)
08-31 09:52:21.674: E/AndroidRuntime(459): at android.app.Activity.dispatchActivityResult(Activity.java:3908)
08-31 09:52:21.674: E/AndroidRuntime(459): at android.app.ActivityThread.deliverResults(ActivityThread.java:2528)
08-31 09:52:21.674: E/AndroidRuntime(459): ... 11 more