Search code examples
androidlistviewforceclose

Getting position from ListView and force closes


I'm not sure why it keeps force closing as soon as I click on one of the items from the listview. I get the position and pass it on to other activity but as soon as I try and display it or put it in an editText, it shuts down. It's probably something simple but I am lost.

First Activity

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id)
        {
            itemPosition = position;

            String itemValue = (String) listView.getItemAtPosition(position);

            Intent myIntent = new Intent(view.getContext(), EnterInfoActivity.class);
            myIntent.putExtra("position",itemPosition);
            startActivity(myIntent);
        }
    });

Second Activity:

protected void onCreate(Bundle savedInstantState){
    super.onCreate(savedInstantState);
    setContentView(R.layout.enterinfo_layout);

    int value;

    Bundle extras = getIntent().getExtras();
    value = extras.getInt("position");

    Toast.makeText(getApplicationContext(), value, Toast.LENGTH_LONG);
}

Solution

  • You cannot set an int value as toast text. Android assumes that an int value is a reference to a string in the strings.xml, which of course cannot be found. If you want to show the toast like that, then you need to convert the int to a String

    Toast.makeText(getApplicationContext(),String.valueOf(value), Toast.LENGTH_LONG);