NumberFormatException:invalid long:""

时间:2014-12-23 09:40:49

标签: java android

我在运行时收到NumberFormatException:invalid long“”。我发布了相关代码。我在下面的编码中提到了第40行错误。

堆栈跟踪:

12-23 04:30:11.660: E/AndroidRuntime(3629): FATAL EXCEPTION: main
12-23 04:30:11.660: E/AndroidRuntime(3629): Process: com.android.timesheet, PID: 3629
12-23 04:30:11.660: E/AndroidRuntime(3629): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.timesheet/com.android.timesheet.ModifyMember}: java.lang.NumberFormatException: Invalid long: ""
12-23 04:30:11.660: E/AndroidRuntime(3629):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
12-23 04:30:11.660: E/AndroidRuntime(3629):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
12-23 04:30:11.660: E/AndroidRuntime(3629):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
12-23 04:30:11.660: E/AndroidRuntime(3629):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
12-23 04:30:11.660: E/AndroidRuntime(3629):     at android.os.Handler.dispatchMessage(Handler.java:102)
12-23 04:30:11.660: E/AndroidRuntime(3629):     at android.os.Looper.loop(Looper.java:136)
12-23 04:30:11.660: E/AndroidRuntime(3629):     at android.app.ActivityThread.main(ActivityThread.java:5017)
12-23 04:30:11.660: E/AndroidRuntime(3629):     at java.lang.reflect.Method.invokeNative(Native Method)
12-23 04:30:11.660: E/AndroidRuntime(3629):     at java.lang.reflect.Method.invoke(Method.java:515)
12-23 04:30:11.660: E/AndroidRuntime(3629):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-23 04:30:11.660: E/AndroidRuntime(3629):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-23 04:30:11.660: E/AndroidRuntime(3629):     at dalvik.system.NativeStart.main(Native Method)
12-23 04:30:11.660: E/AndroidRuntime(3629): Caused by: java.lang.NumberFormatException: Invalid long: ""
12-23 04:30:11.660: E/AndroidRuntime(3629):     at java.lang.Long.invalidLong(Long.java:124)
12-23 04:30:11.660: E/AndroidRuntime(3629):     at java.lang.Long.parseLong(Long.java:345)
12-23 04:30:11.660: E/AndroidRuntime(3629):     at java.lang.Long.parseLong(Long.java:318)
12-23 04:30:11.660: E/AndroidRuntime(3629):     at com.android.timesheet.ModifyMember.onCreate(ModifyMember.java:40)
12-23 04:30:11.660: E/AndroidRuntime(3629):     at android.app.Activity.performCreate(Activity.java:5231)
12-23 04:30:11.660: E/AndroidRuntime(3629):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-23 04:30:11.660: E/AndroidRuntime(3629):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
12-23 04:30:11.660: E/AndroidRuntime(3629):     ... 11 more

ModifyMember.java:

public class ModifyMember extends Activity implements OnClickListener {

    EditText et;
    Button edit_bt, delete_bt;

    long member_id;

    PersonDatabaseHelper dbcon;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.modify_member);

        dbcon = new PersonDatabaseHelper(getApplicationContext());  
    //  dbcon.open();    

        et = (EditText) findViewById(R.id.edit_mem_id);
        edit_bt = (Button) findViewById(R.id.update_bt_id);
        delete_bt = (Button) findViewById(R.id.delete_bt_id);

        Intent i = getIntent();
        String memberID = i.getStringExtra("memberID");
        String memberName = i.getStringExtra("memberName");

        member_id=Long.parseLong(memberID);  --->40th error line 


        et.setText(memberName);

        edit_bt.setOnClickListener(this);
        delete_bt.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.update_bt_id:
            String memName_upd = et.getText().toString();
            dbcon.updateData(member_id, memName_upd);
            this.returnHome();
            break;

        case R.id.delete_bt_id:

            dbcon.deleteData(member_id);
            this.returnHome();
            break;
        }
    }

    public void returnHome() {

        Intent home_intent = new Intent(getApplicationContext(),
                NewProjects.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        startActivity(home_intent);
    }

}

我不知道如何解决这个错误。任何人都可以帮助我。谢谢。

3 个答案:

答案 0 :(得分:3)

你的logcat清楚地说了

  

引起:java.lang.NumberFormatException:无效长:“”12-23   04:30:11.660:E / AndroidRuntime(3629):

您的memberID为空字符串,而您尝试转换为Long

解决方案:

检查memberID是否为empty

if(!TextUtils.isEmpty(memberID))
{
//Do your job
}

答案 1 :(得分:2)

Caused by: java.lang.NumberFormatException: Invalid long: ""
memberID could be empty or data in it do not have number
  

"抛出以表明应用程序已尝试转换a   字符串到其中一个数字类型,但字符串没有   适当的格式。"

在尝试转换之前检查字符串是否为空(例如:检查bundle是否包含密钥)

答案 2 :(得分:0)

memberId是一个空字符串,不是有效的长字。