我正在开发一个应用程序,用户可以在其中添加商店部门,并为那些部门添加部门所在城市的功能。

当我尝试为一个部门添加城市时,我不断收到NumberFormatException:

Caused by: java.lang.NumberFormatException: Invalid long: "null"
            at java.lang.Long.invalidLong(Long.java:124)
            at java.lang.Long.parseLong(Long.java:345)
            at java.lang.Long.parseLong(Long.java:321)
            at org.hello.addgroups.vestiging_list.onCreate(vestiging_list.java:45)


在此之后,应用程序崩溃,但是当我重新启动应用程序时,值将添加到列表中。

这些是我的课程:
Add_vestiging.java:

public class Add_vestiging extends Activity implements View.OnClickListener{

EditText et,hiddenet;
Button add_bt, cancel_btn;
SQLController dbcon;
Intent i;
Long groupd;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add_new_vestiging);
    et = (EditText)findViewById(R.id.vestigingName);
    add_bt = (Button) findViewById(R.id.addBtn);
    cancel_btn = (Button) findViewById(R.id.cancelBtn);

    dbcon = new SQLController(this);
    try {
        dbcon.open();
    } catch(SQLException e) {
        e.printStackTrace();
    }
    i = getIntent();
    String id = i.getStringExtra("groupID");
    groupd = Long.parseLong(id);
    add_bt.setOnClickListener(this);
    cancel_btn.setOnClickListener(this);
}

@Override
public void onClick(View v) {
        switch(v.getId()) {
            case R.id.addBtn:
                String name = et.getText().toString();
                try {
                    dbcon.insertVestiging(groupd, name);
                }catch(SQLiteException e) {
                    e.printStackTrace();
                }
                Intent main = new Intent(Add_vestiging.this, vestiging_list.class);
                startActivity(main);
                break;
            case R.id.cancelBtn:
                super.finish();
                break;

        }
}
}


vestiging_list.java:

public class vestiging_list extends Activity {

        ListView lv;
        SQLController dbcon;
        TextView title;
        Button addves;
        Long long_id;
        String groupid;
        Intent add_ves;
        String groupname;
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.vestiging_list);
                dbcon = new SQLController(this);
                title = (TextView) findViewById(R.id.vestitel);


                try {
                        dbcon.open();
                } catch (SQLException e) {
                        e.printStackTrace();
                }
                add_ves = getIntent();
                groupid = add_ves.getStringExtra("groupID");
                groupname = add_ves.getStringExtra("groupName");
                title.setText(groupname);
                long_id = Long.parseLong(groupid);

                addves = (Button)findViewById(R.id.addves_bt_id);
                lv = (ListView)findViewById(R.id.vestigingList_id);

                addves.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                                add_ves = new Intent(getApplicationContext(), Add_vestiging.class);
                                add_ves.putExtra("groupID", groupid);
                                startActivity(add_ves);
                        }
                });

                Cursor cursor = dbcon.readVestigingen(long_id);
                String[] from = new String[] { GroupDatabaseHelper.V_ID, GroupDatabaseHelper.VESNAME};
                int[] to = new int [] { R.id.vesID, R.id.vesName};

                SimpleCursorAdapter adapter = new SimpleCursorAdapter(vestiging_list.this, R.layout.vestiging_single_row_item, cursor, from, to,1);
                adapter.notifyDataSetChanged();
                lv.setAdapter(adapter);

                     }
}


关于如何删除NumberFormatException的任何想法?
任何帮助是极大的赞赏。

提前致谢。

最佳答案

关于如何删除NumberFormatException的任何想法?


异常是由第45行的vestiging_list中的Long.parseLong引起的。原因是groupid,在您的情况下是null。由于您没有填写Intent对象,因此您曾经用来启动vestiging_list

例如。

case R.id.addBtn:
      String name = et.getText().toString();
      try {
         dbcon.insertVestiging(groupd, name);
       }catch(SQLiteException e) {
          e.printStackTrace();
       }
    Intent main = new Intent(Add_vestiging.this, vestiging_list.class);
    main.putExtra("groupID", String.valueOf(groupd));
    startActivity(main);
    break;


假设groupd很长。

关于java - Android中的NumberFormatException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32482264/

10-09 01:47