在Android上的ListView中的setonClickListener中的问题

时间:2011-06-10 06:35:30

标签: android android-layout android-listview android-linearlayout

在列表视图中,我设置了图像视图,3个图像按钮和两个文本视图。我必须为每个图像按钮设置setoncliklistener。我按照以下方式做了,但我得到了NullPointerException。在我的xml中,我为每个图像按钮提供android:onClick="OnClick"。这是我的代码:请帮帮我。我发布代码,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.findViewById(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() {
        ........... 
    }

  }

我的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)

............... 请帮帮我。

2 个答案:

答案 0 :(得分:2)

更改订单。


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

首先你得到了这一行。在那之后,你试图从无处膨胀一些东西。如果第一步中的行为空,该怎么办?

P.S。不要忘记 ROW .findViewById(..

答案 1 :(得分:0)

我认为它因为布局尚未设置而变为空...

if(row == null)
{....}

将此部分移至下方:

View row = convertView;  

希望它有所帮助。