2011-12-30 62 views
0

我正在接触列表项目和我需要做的最后一件事是显示图像为我的每个联系人。
在从下面我想通过使用路径至极,以显示我的形象代码保存在我的edittext1: ,但我得到这条线上空指针异常:java空指针异常,当试图获取图像的路径

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 
+1

请发布完整的例外,包括堆栈跟踪。 – 2011-12-30 18:47:03

+0

您确定EditText中有文字吗? – 2011-12-30 18:47:24

+0

听起来像'mpic'为空。你确定'R.id.edittext1'是Activity的布局中的一个视图的ID吗? – 2011-12-30 18:49:09

回答

0

最有可能的可能性是,MPIC为null,因为没有视图可以用t发现帽子ID。

第二种可能性是调用mpic上的.getText()方法的结果为null,因此对结果调用toString()方法失败。

在尝试调用它们的方法之前,您应该添加一个显式检查来验证对象是否为空,或者用try/catch包围尝试,并在检测到问题时执行一些有用的信息。

您还可以使用android日志记录工具将对象的表示形式打印到日志中,并手动检查哪些内容为空 - 尽管这样做不会帮助您解决将来可能出现的问题在最终用户的设备上,但不在您的开发/测试设备上。