gpt4 book ai didi

java - Android 中的 NumberFormatException

转载 作者:行者123 更新时间:2023-12-02 04:23:27 24 4
gpt4 key购买 nike

我正在制作一个应用程序,用户可以在其中添加商店部门,并且可以为这些部门添加部门所在的城市。

当我尝试为部门添加城市时,我不断收到 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)

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

这些是我的类(class):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 有什么想法吗?非常感谢任何帮助。

提前致谢。

最佳答案

Any ideas on how to remove the NumberFormatException?

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

例如

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是一个long。

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com