Android应用中的NumberFormatException

时间:2014-01-29 11:38:33

标签: java android sqlite

我正在尝试制作一个简单的联系人列表应用。我尝试编辑我的联系人通过单击电话列表到活动结果,编辑后我按下保存按钮它通过在此行抛出“id = null”给我空指针异常

long rowId=Long.parseLong(DBHandler.Key_ID) 

在活动结果(...)功能中。在调试过程中,当我在列表中单击联系人进行编辑时,它会获取id,但是当我保存编辑部分时,它会将id赋予null。请帮帮我。

这是源代码:

public class WebpreneurActivity extends ListActivity {

    private static final int CONTACT_CREATE = 0;
    private static final int CONTACT_EDIT = 1;
    public static  long id1;
    //select the second one, Android view menu
    private static final int INSERT_ID = Menu.FIRST;
    private static final int DELETE_ID = Menu.FIRST + 1;

    private DBHandler dbHelper;
    private Cursor c;
    ImageButton imageButton;
    public static long  rowId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d("database1" ,"0");
        Log.d("Your Location4", "ok4:");
        super.onCreate(savedInstanceState);
        Log.d("database1" ,"1");
        setContentView(R.layout.activity_webpreneur);
        Log.d("database1" ,"2");
        dbHelper = new DBHandler(this);
        Log.d("database1" ,"3");
        dbHelper.open();
        fillData();
        //dbHelper.open();

        //addListenerOnButton();
        //dbHelper.addContact();

        imageButton = (ImageButton) findViewById(R.id.imageButton1);
        Log.d("database1" ,"button");
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Log.d("database1" ,"b4");
                Intent i = new Intent(getApplicationContext(), ContactEdit.class);
                startActivityForResult(i, CONTACT_CREATE);

                Log.d("database1" ,"button3");
                //fillData();           
            }       
         });

         //dbHelper.close();
     }

     @SuppressWarnings("deprecation")
     public void fillData() {
         Log.d("Your Location4", "ok6:");
         c = dbHelper.fetchAllRows();
         Log.d("Your Location4", "ok8:");
         //startManagingCursor(c);
         Log.d("Your Location4", "ok2:");
         NoIdCursorWrapper nc = new NoIdCursorWrapper(c, DBHandler.Key_ID);
         ListAdapter adapter = new SimpleCursorAdapter(this, R.layout.contact_row, nc, new String[] { DBHandler.Key_Name, DBHandler.Key_Phone }, new int[] { R.id.name, R.id.phonenumber });
         setListAdapter(adapter);
     }

     protected void onListItemClick(ListView l, View v, int position,   long id) {
         super.onListItemClick(l, v, position, id);

         Intent i = new Intent(this, ContactEdit.class);
         i.putExtra(DBHandler.Key_ID, c.getLong(c.getColumnIndex(DBHandler.Key_ID)));
         i.putExtra(DBHandler.Key_Name, c.getString(c.getColumnIndex(DBHandler.Key_Name)));
         i.putExtra(DBHandler.Key_Address, c.getString(c.getColumnIndex(DBHandler.Key_Address)));
         i.putExtra(DBHandler.Key_Phone, c.getString(c.getColumnIndex(DBHandler.Key_Phone)));
         i.putExtra(DBHandler.Key_Website, c.getString(c.getColumnIndex(DBHandler.Key_Home)));
         startActivityForResult(i, CONTACT_EDIT);
     }

     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
         super.onActivityResult(requestCode, resultCode, data);

         if (resultCode == RESULT_OK) {
             String name = data.getStringExtra(DBHandler.Key_Name);
             String address = data.getStringExtra(DBHandler.Key_Address);
             String mobile = data.getStringExtra(DBHandler.Key_Phone);
             String home = data.getStringExtra(DBHandler.Key_Home);

             //String id = data.getStringExtra(DBHandler.Key_ID);
             switch (requestCode) {
                 case CONTACT_CREATE:
                     Log.d("Your Location4", "jj:");
                     dbHelper.createRow(name, address, mobile, home);
                     Log.d("Your Location4" , "ok90:");
                     fillData();
                     break;

                 case CONTACT_EDIT:
                     //String id = data.getStringExtra(DBHandler.Key_ID);
                     long rowId=Long.parseLong(DBHandler.Key_ID); ////Giving null pointer exception at this point
                     Log.d("Your Location4" , "ok9b:");
                     if (rowId != (Long)null){
                         dbHelper.updateRow(rowId, name, address, mobile, home);
                     }
                     fillData();
                     break;
              }
          }
      }
  }

这是我的log cat /// [注意:正确格式化logcat]

01-29 16:12:13.617: E/AndroidRuntime(26320): FATAL EXCEPTION: main
        01-29 16:12:13.617: E/AndroidRuntime(26320): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity {com.webpreneur_contactlist/com.webpreneur_contactlist.WebpreneurActivity}: java.lang.NumberFormatException: ID
        01-29 16:12:13.617: E/AndroidRuntime(26320):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2553)
        01-29 16:12:13.617: E/AndroidRuntime(26320):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:2595)
        01-29 16:12:13.617: E/AndroidRuntime(26320):    at android.app.ActivityThread.access$2000(ActivityThread.java:121)
        01-29 16:12:13.617: E/AndroidRuntime(26320):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:973)
        01-29 16:12:13.617: E/AndroidRuntime(26320):    at android.os.Handler.dispatchMessage(Handler.java:99)
        01-29 16:12:13.617: E/AndroidRuntime(26320):    at android.os.Looper.loop(Looper.java:138)
        01-29 16:12:13.617: E/AndroidRuntime(26320):    at android.app.ActivityThread.main(ActivityThread.java:3701)
        01-29 16:12:13.617: E/AndroidRuntime(26320):    at java.lang.reflect.Method.invokeNative(Native Method)
        01-29 16:12:13.617: E/AndroidRuntime(26320):    at java.lang.reflect.Method.invoke(Method.java:507)
        01-29 16:12:13.617: E/AndroidRuntime(26320):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
        01-29 16:12:13.617: E/AndroidRuntime(26320):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
        01-29 16:12:13.617: E/AndroidRuntime(26320):    at dalvik.system.NativeStart.main(Native Method)
        01-29 16:12:13.617: E/AndroidRuntime(26320): Caused by: java.lang.NumberFormatException: ID
        01-29 16:12:13.617: E/AndroidRuntime(26320):    at java.lang.Long.parse(Long.java:353)
        01-29 16:12:13.617: E/AndroidRuntime(26320):    at java.lang.Long.parseLong(Long.java:344)
        01-29 16:12:13.617: E/AndroidRuntime(26320):    at java.lang.Long.parseLong(Long.java:311)
        01-29 16:12:13.617: E/AndroidRuntime(26320):    at com.webpreneur_contactlist.WebpreneurActivity.onActivityResult(WebpreneurActivity.java:129)
        01-29 16:12:13.617: E/AndroidRuntime(26320):    at android.app.Activity.dispatchActivityResult(Activity.java:3908)
        01-29 16:12:13.617: E/AndroidRuntime(26320):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2549)      

2 个答案:

答案 0 :(得分:3)

更改为:

long rowId=data.getLongExtra(DBHandler.Key_ID,0);

之后:

 if (rowId != 0){
                         dbHelper.updateRow(rowId, name, address, mobile, home);
                     }

答案 1 :(得分:0)

用try-catch包裹并处理异常

long rowId;
try {
    rowId=Long.parseLong(DBHandler.Key_ID);
} catch (NumberFormatException ex) {
    //Handle exception!
    //f.ex. rowId = -1;
}