我已經陷入我的代碼中,我無法過程。模擬器崩潰,堆棧指向第47行。請幫助初學者!模擬器崩潰「Invalid int」
public class MainActivity3 extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Button obutton = (Button) findViewById(R.id.obutton);
obutton.setOnClickListener(this);
final Button btn = (Button) findViewById(R.id.button);
final EditText edt = (EditText) findViewById(R.id.editText);
final ListView listview = (ListView) findViewById(R.id.Lista);
final ArrayList<String> list = new ArrayList<>();
final StableArrayAdapter adapter = new StableArrayAdapter(this, android.R.layout.simple_list_item_1, list);
listview.setAdapter(adapter);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (edt.getText().toString() != null) {
adapter.addItem(edt.getText().toString());
}
}
});
}
private class StableArrayAdapter extends ArrayAdapter<String> {
ArrayList<String> data = new ArrayList<>();
private List mIdMap;
public StableArrayAdapter(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
this.data = (ArrayList) objects;
}
@Override
public long getItemId(int position) {
String item = getItem(position);
return (long) mIdMap.get(Integer.parseInt(item)) <-- Crashing
}
public void addItem(String item) {
data.add(item);
notifyDataSetChanged();
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.obutton:
Intent obama = new Intent(MainActivity3.this, MainActivity.class);
startActivity(obama);
case R.id.button:
Intent test = new Intent(MainActivity3.this, Main6Activity.class);
startActivityForResult(test, 1);
}
}
}
堆棧錯誤:
FATAL EXCEPTION: main java.lang.NumberFormatException: Invalid int: "t" at java.lang.Integer.invalidInt(Integer.java:138)
謝謝你的快速回復Chirag!不過,我不能簡單地編碼「get(item)」,甚至不能獲得(String new(item))。任何建議?提前致謝。 –