Reputation: 1
I am assigning action to the item of list view in the below code .But when I am clicking Item of list view application is "unfortunately stopping". I am providing the code below.
package com.lara;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class FamilyActivity extends Activity {
private ListView lv;
private String names[]={"sum","hari","mom","dad"};
private ArrayAdapter<String> s1;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.family1);
lv=(ListView)findViewById(R.id.listView1);
s1=new ArrayAdapter<String>(getApplication(), R.layout.text, names);
lv.setAdapter(s1);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplication(), 555, Toast.LENGTH_LONG).show();
}
});
}
Upvotes: 0
Views: 101
Reputation: 6925
Application is stopping because below are the arguments you need to pass in order to show a Toast.
public static Toast makeText (Context context, CharSequence text, int duration)
but you are passing 555
which is Integer
not CharSequence
Update your code like this
s1=new ArrayAdapter<String>(getApplicationContext(), R.layout.text, names);
and
Toast.makeText(getApplicationContext(), ""+555, Toast.LENGTH_LONG).show();
Upvotes: 0
Reputation: 1709
Firstly what is in that R.layout.text
layout?? Run the program with android.R.layout.simple_list_item1
(something like that) and see if it crashes. Secondly since you are extending class Activity where there is getApplication I think you can also use this
keyword. Also the docs for the Toast.makeText method show that the second argument is a string so using @Raghunandan 's method might just do the trick. One last point is please always provide the logcat post,I didnt know how to use it and would bother everyone here on S/O with simple meagre stuff but once u learn to use it debugging will be easy,make it a point to learn how to use
Upvotes: 0
Reputation: 133560
Try the below
s1=new ArrayAdapter<String>(FamilyActivity.this,android.R.layout.simple_list_item_1, names);
and change Toast to
Toast.makeText(getApplicationContext(),""+ 555, Toast.LENGTH_LONG).show();
or
Toast.makeText(getApplicationContext(), String.valueOf(555), Toast.LENGTH_LONG).show();
555 is an int value
public static Toast makeText (Context context, int resId, int duration)
looks for a resource with the id provided if not found you get resource not found exception
What you need
public static Toast makeText (Context context, CharSequence text, int duration)
So use a string
Upvotes: 3
Reputation: 8939
Try Like this
Toast.makeText(FamilyActivity.this, ""+500, Toast.LENGTH_LONG).show();
Upvotes: 1