我正在制作一个联系人列表项目,我需要做的最后一件事是为每个联系人显示图像 在下面的代码中,我想通过使用保存在edittext1中的路径来显示我的图像: 但我在这一行得到了NUll指针异常:
String username = mpic.getText().toString();
private void ShowImage()
{
//string username created from edit text field to a string
EditText mpic =(EditText)findViewById(R.id.edittext1);
String username = mpic.getText().toString();
//bitmap will decode the string to a image (bitmap)
Bitmap myBitmap = BitmapFactory.decodeFile(username);
//Image view used to set the bitmap
ImageView myImage = (ImageView) findViewById(R.id.image);
//setting the image to the image view
myImage.setImageBitmap(myBitmap);
}
全班是:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ShowImage();
showDatabaseContent();
lv1 = getListView();
lv1.setTextFilterEnabled(true);
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
cursor = (Cursor) a.getItemAtPosition(position);
itemId = cursor.getString(6);
openOptionsMenu();
}
});
lv1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id){
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
//selected item index from ListView
public void showDialogItemId(long itemId){
Toast.makeText(this, "Menu item selected index is" + Long.toString(itemId), Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.modifyitem:
if(null != itemId){
Bundle contactToModify = new Bundle();
contactToModify.putString("cFirstName", cursor.getString(0));
contactToModify.putString("cMobilePhone", cursor.getString(5));
contactToModify.putString("cEmail", cursor.getString(2));
contactToModify.putString("curl", cursor.getString(3));
contactToModify.putString("cAdress", cursor.getString(4));
contactToModify.putString ("cphoto", cursor.getString(1));
contactToModify.putString("mod_type", "modifyPerson");
Intent intent = new Intent(this, ContactDetails.class);
intent.setClass(this, ContactDetails.class);
intent.putExtras(contactToModify);
startActivityForResult(intent, CONTACT_MODIFIED);
}else{
Toast.makeText(this, "Select Contact to modify", Toast.LENGTH_LONG).show();
}
break;
case R.id.additem:
Intent i = new Intent(this, ContactDetails.class);
Bundle bun = new Bundle();
bun.putString("mod_type", "addPerson");
i.setClass(this, ContactDetails.class);
i.putExtras(bun);
startActivityForResult(i, CONTACT_ADDED);
break;
case R.id.removeitem:
if(null != itemId){
removeContact(itemId);
showDatabaseContent();
}
else{
Toast.makeText(this, "Select Contact to delete", Toast.LENGTH_LONG).show();
}
break;
case R.id.search:
Intent j =new Intent(this,Search.class);
j.setClass(this, Search.class);
startActivity(j);
break;
}
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
// See which child activity is calling us back.
switch (resultCode) {
case CONTACT_ADDED:
// This is the standard resultCode that is sent back if the
// activity crashed or didn't doesn't supply an explicit result.
if (resultCode == RESULT_FIRST_USER){
Bundle bundle = new Bundle();
bundle = intent.getBundleExtra("contactData");
addContact(bundle);
showDatabaseContent();
}
else{
Toast.makeText(this, "CANCEL CONTACT BUTTON PRESSED", Toast.LENGTH_LONG).show();
}
break;
case CONTACT_MODIFIED:
if (resultCode == 2){
Bundle bundle = new Bundle();
bundle = intent.getBundleExtra("contactData");
modifyContact(bundle);
showDatabaseContent();
}
else{
Toast.makeText(this, "MODIFY CONTACT FAILED", Toast.LENGTH_LONG).show();
}
break;
default:
break;
}
}
//method removes item from database
private void removeContact(String itemId){
db = contacts.getWritableDatabase();
db.delete(DbConstants.TABLE_NAME, "_ID=" + itemId, null);
}
private void addContact(Bundle bundle) {
// Insert a new record into the Events data source.
// You would do something similar for delete and update.
db = contacts.getWritableDatabase();
ContentValues vals = new ContentValues();
vals.put(DbConstants.NAME, bundle.getString("contactFirstName"));
vals.put(DbConstants.PHONE, bundle.getString("contactMobilePhone"));
vals.put(DbConstants.EMAIL, bundle.getString("contactEmail"));
vals.put(DbConstants.URL_STRING,bundle.getString("contactUrl"));
vals.put(DbConstants.ADRESS,bundle.getString("contactadress"));
vals.put(DbConstants.PHOTO,bundle.getString("contactphoto"));
db.insertOrThrow(DbConstants.TABLE_NAME, null, vals);
}
//method should modify existing Contact
private void modifyContact(Bundle bundle){
db = contacts.getWritableDatabase();
ContentValues vals = new ContentValues();
vals.put(DbConstants.NAME, bundle.getString("contactFirstName"));
vals.put(DbConstants.PHONE, bundle.getString("contactMobilePhone"));
vals.put(DbConstants.EMAIL, bundle.getString("contactEmail"));
vals.put(DbConstants.URL_STRING,bundle.getString("contactUrl"));
vals.put(DbConstants.ADRESS,bundle.getString("contactadress"));
vals.put(DbConstants.PHOTO,bundle.getString("contactphoto"));
db.update(DbConstants.TABLE_NAME, vals, _ID+"="+itemId, null);
}
private Cursor getContacts() {
db = contacts.getReadableDatabase();
cursor = db.query(DbConstants.TABLE_NAME, FROM, null, null, null,
null, null);
startManagingCursor(cursor);
return cursor;
}
public void showDatabaseContent(){
contacts = new DbCreate(this);
try {
cursor = getContacts();
showContacts(cursor);
} finally {
contacts.close();
db.close();
}
}
private void showContacts(Cursor cursor) {
//set up data binding
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item, cursor, FROM, TO);
setListAdapter(adapter);
}
private void ShowImage()
{
//string username created from edit text field to a string
EditText mpic =(EditText)findViewById(R.id.edittext1);
String username = mpic.getText().toString();
//bitmap will decode the string to a image (bitmap)
Bitmap myBitmap = BitmapFactory.decodeFile(username);
//Image view used to set the bitmap
ImageView myImage = (ImageView) findViewById(R.id.image);
//setting the image to the image view
myImage.setImageBitmap(myBitmap);
}
整个错误:
12-30 20:33:23.292: E/AndroidRuntime(4297): FATAL EXCEPTION: main
12-30 20:33:23.292: E/AndroidRuntime(4297): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.example.dbcontactconsole/org.example.dbcontactconsole.DbContactConsole}: java.lang.NullPointerException
12-30 20:33:23.292: E/AndroidRuntime(4297): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
12-30 20:33:23.292: E/AndroidRuntime(4297): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
12-30 20:33:23.292: E/AndroidRuntime(4297): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
12-30 20:33:23.292: E/AndroidRuntime(4297): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
12-30 20:33:23.292: E/AndroidRuntime(4297): at android.os.Handler.dispatchMessage(Handler.java:99)
12-30 20:33:23.292: E/AndroidRuntime(4297): at android.os.Looper.loop(Looper.java:123)
12-30 20:33:23.292: E/AndroidRuntime(4297): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-30 20:33:23.292: E/AndroidRuntime(4297): at java.lang.reflect.Method.invokeNative(Native Method)
12-30 20:33:23.292: E/AndroidRuntime(4297): at java.lang.reflect.Method.invoke(Method.java:521)
12-30 20:33:23.292: E/AndroidRuntime(4297): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-30 20:33:23.292: E/AndroidRuntime(4297): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-30 20:33:23.292: E/AndroidRuntime(4297): at dalvik.system.NativeStart.main(Native Method)
12-30 20:33:23.292: E/AndroidRuntime(4297): Caused by: java.lang.NullPointerException
12-30 20:33:23.292: E/AndroidRuntime(4297): at org.example.dbcontactconsole.DbContactConsole.ShowImage(DbContactConsole.java:234)
12-30 20:33:23.292: E/AndroidRuntime(4297): at org.example.dbcontactconsole.DbContactConsole.onCreate(DbContactConsole.java:46)
12-30 20:33:23.292: E/AndroidRuntime(4297): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-30 20:33:23.292: E/AndroidRuntime(4297): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
12-30 20:33:23.292: E/AndroidRuntime(4297): ... 11 more
答案 0 :(得分:0)
最有可能的是mpic为null,因为该id无法找到任何视图。
第二种可能性是在mpic上调用.getText()方法的结果为null,因此在结果上调用toString()方法失败。
在尝试调用方法之前,应该添加一个显式检查以验证对象是否为null,或者使用try / catch包围尝试,并在检测到问题时执行一些信息。
您还可以使用android日志记录工具将对象的表示形式打印到日志中,并手动检查哪些是空的 - 尽管这不会帮助您解决可能仅由于某些情况发生的未来问题最终用户的设备,但不在您的开发/测试设备上。