problem in setonClickListener in ListView on android
in a list view i set a image view , 3 image buttons and two text views. i have to set setoncliklistener
for each image button. i did in the following way but i get NullPointerException
. in my xml i give android:onClick="OnClick"
for each image button. this is my code: please help me. i post code,xml,logcat.
public class AndroidList extends ListActivity{
ImageButton ib1,ib2,ib3;
public class MyThumbnaildapter extends ArrayAdapter<String> implements OnClickListener{
public MyThumbnaildapter(Context context, int textViewResourceId,String[] objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// View view = super.getView(position, convertView, parent);
View row = convertView;
if(row==null){
LayoutInflater inflater=getLayoutInflater();
row=inflater.inflate(R.layout.row, parent, false);
}
ib1=(ImageButton)row.findViewById(R.id.imageButton1); // line no 53 .. NullPointerException
ib2=(ImageButton)row.findVie开发者_开发知识库wById(R.id.imageButton2);
ib3=(ImageButton)row.findViewById(R.id.imageButton3);
ib1.setOnClickListener(this);
ib2.setOnClickListener(this);
ib3.setOnClickListener(this);
row.setTag(position);
TextView textfilePath = (TextView)row.findViewById(R.id.FilePath);
textfilePath.setText(_videosId[position]);
ImageView imageThumbnail = (ImageView)row.findViewById(R.id.Thumbnail);
Bitmap bmThumbnail;
int ids;
ids=Integer.parseInt(_videosId[position]);
bmThumbnail= MediaStore.Video.Thumbnails.getThumbnail(getContentResolver(),ids, MediaStore.Video.Thumbnails.MICRO_KIND, null);
// bmThumbnail = ThumbnailUtils.createVideoThumbnail(_videosId[position], Thumbnails.MICRO_KIND);
imageThumbnail.setImageBitmap(bmThumbnail);
return row;
}
@Override
public void onClick(View v) {
Integer position = (Integer)v.getTag(); // here i get position
switch(v.getId()){
case R.id.imageButton1:
Toast.makeText(getBaseContext(),"play "+position, Toast.LENGTH_SHORT).show(); // position print as null
break;
case R.id.ImageButton2:
Toast.makeText(getBaseContext(),"play2 "+position, Toast.LENGTH_SHORT).show(); // position print as null
break;
case R.id.ImageButton3:
Toast.makeText(getBaseContext(),"play3 "+position, Toast.LENGTH_SHORT).show(); // position print as null
break;
}
}
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
_contentUri = MEDIA_EXTERNAL_CONTENT_URI;
initVideosId();
setListAdapter(new MyThumbnaildapter(AndroidList.this, R.layout.row, _videosId));
}
protected void onListItemClick(ListView l, View v, int position, long id) {
Log.e("video", "called");
super.onListItemClick(l, v, position, id);
Toast.makeText(getBaseContext(),""+position, Toast.LENGTH_SHORT).show();
}
private void initVideosId() {
...........
}
}
my xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="100dp" android:descendantFocusability="blocksDescendants">
<image view>....</image view>
<LinearLayout android:orientation="vertical" android:descendantFocusability="blocksDescendants"
<ImageButton android:id="@+id/imageButton1 ... ></ImageButton>
<ImageButton android:id="@+id/imageButton2 ... ></ImageButton>
<ImageButton android:id="@+id/imageButton3 ... ></ImageButton>
</LinearLayout>
</RelativeLayout>
Log cat:
06-10 11:43:37.563: ERROR/AndroidRuntime(2504): java.lang.NullPointerException
06-10 11:43:37.563: ERROR/AndroidRuntime(2504): at com.exercise.AndroidList.AndroidList$MyThumbnaildapter.getView(AndroidList.java:53)
06-10 11:43:37.563: ERROR/AndroidRuntime(2504): at android.widget.AbsListView.obtainView(AbsListView.java:1274)
06-10 11:43:37.563: ERROR/AndroidRuntime(2504): at android.widget.ListView.makeAndAddView(ListView.java:1668)
06-10 11:43:37.563: ERROR/AndroidRuntime(2504): at android.widget.ListView.fillDown(ListView.java:637)
06-10 11:43:37.563: ERROR/AndroidRuntime(2504): at android.widget.ListView.fillFromTop(ListView.java:694)
............... please help me.
Change the order.
View row = convertView;
if(row == null){
LayoutInflater inflater=getLayoutInflater();
row = inflater.inflate(R.layout.row, parent, false);
}
ib1=(ImageButton)ROW.findViewById(R.id.imageButton1); // line no 53 .. NullPointerException
ib2=(ImageButton)row.findViewById(R.id.imageButton2);
ib3=(ImageButton)row.findViewById(R.id.imageButton3);
ib1.setOnClickListener(this);
ib2.setOnClickListener(this);
ib3.setOnClickListener(this);
row.setTag(position);
First you get the row. After that you are trying to inflate something from nowhere. what if row is null in the first step?
P.S. Do not forget ROW.findViewById(..
I think its getting null because layout is not set yet...
if(row == null)
{....}
move this section to just below of:
View row = convertView;
Hope it helps.
精彩评论