【问题标题】:problem in setonClickListener in ListView on androidandroid上ListView中setonClickListener的问题
【发布时间】:2011-06-10 06:35:29
【问题描述】:

在列表视图中,我设置了一个图像视图、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>

原木猫:

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)

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

【问题讨论】:

  • 我建议你做更多的调试。也许打破了界限。所以事先抓住Integer buttonId = R.id.imageButton1;,并记录它的价值。然后获取视图View buttonView = findViewById(buttonId); 并记录其类。然后将其转换为 ImageButton ImageButton button = (ImageButton) buttonView;。然后将其分配给变量ib1 = button;。希望您能在某个地方找到问题
  • 这段代码让我哭了......真的,这段代码看起来就像你从随机来源复制别人的代码并将其粘贴到一个文件中:) ... ImageButton ib1,ib2,ib3;一流的??它们应该是 getView 中的本地变量 ...或者更好地查看 developer.android.com/resources/samples/ApiDemos/src/com/… ... 通过在 getView 中调用 findViewById(R.id.imageButton1) 您从活动中调用 findViewById 而不是来自列表项跨度>

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


【解决方案1】:

更改顺序。


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

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

附:不要忘记 ROW.findViewById(..

【讨论】:

  • 我更改了我在回答中给出的代码,我在相同的位置得到了相同的错误。请帮帮我。
  • 你没有改变你的代码... ib1=(ImageButton) row .findViewById(R.id.imageButton1);
  • @Pepi 我确定他没有更改代码...错误在这里ib1.setOnClickListener(this) coz ib1 为空,您的答案很好(他为活动而不是列表项调用 findViewById )
  • 请...仔细阅读...正如我所说:ib1=(ImageButton)row.findViewById(R.id.imageButton1); ib2=(ImageButton)row.findViewById(R.id.imageButton2); ib3=(ImageButton)row.findViewById(R.id.imageButton3);你忘了 ROW.findviewById 再看我的例子..
  • 它的作品谢谢,我按照你说的做了改变。但我打印位置它给我null。请帮我。谢谢
【解决方案2】:

我认为它会为空,因为尚未设置布局......

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

将此部分移至以下:

View row = convertView;  

希望对你有帮助。

【讨论】:

  • 我的代码中有“if(row==null) below of view row = convertView”;
  • 它可以运行,但请在我的代码中检查“public void onclick(view v){...}”。如果在 toast 中打印位置值。它显示为空。请....
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-31
  • 1970-01-01
相关资源
最近更新 更多