The Gentleman The Gentleman - 7 days ago 7
Android Question

Emulator crash "Invalid int"

I have become stuck in my code and I can not procced.The emulator crashes and the stack points to line 47. Please help a beginner out!

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);

}

}


}


The Stack error:


FATAL EXCEPTION: main java.lang.NumberFormatException: Invalid int:
"t" at java.lang.Integer.invalidInt(Integer.java:138)

Answer

Changing:

    @Override
    public long getItemId(int position) {
        String item = getItem(position);
        return (long) mIdMap.get(Integer.parseInt(item)) <-- Crashing 

to: @Override public long getItemId(int position) { return -1l; Made it work.