Java 可点击列表视图

Java 可点击列表视图,java,android,listview,nullpointerexception,Java,Android,Listview,Nullpointerexception,我现在正在寻找一个解决方案,在列表视图中点击项目几天 首先,我遇到了这样一个问题: developer.android.com/resources/articles/touch mode.html 并发现它没有“正常”的onListItemClick()行为 然后我遇到了这个代码: 调试时,我注意到参数View v是TextView而不是“正常”视图,当然: TextView text = (TextView) v.findViewById(R.id.listitem_text); 返回nu

我现在正在寻找一个解决方案,在列表视图中点击项目几天

首先,我遇到了这样一个问题: developer.android.com/resources/articles/touch mode.html 并发现它没有“正常”的onListItemClick()行为

然后我遇到了这个代码:

调试时,我注意到参数View vTextView而不是“正常”视图,当然:

TextView text = (TextView) v.findViewById(R.id.listitem_text);
返回null,我得到一个NullPointerException

你知道为什么吗?我怎样才能解决这个问题


提前感谢!:)

列表项应该可以直接点击。您可以通过查看
ApiDemos
项目代码来检查列表的编码方式。它应该出现在本地计算机上,因为它是SDK的一部分。我在
\platforms\android-2.0.1\samples\ApiDemos
上找到了它,您如何创建
ClickableListAdapter的实例

创建列表适配器时,必须传递一个资源id
viewId
,这应该是一个
布局
,稍后会膨胀

public ClickableListAdapter(Context context, int viewid, List objects) {  

        // Cache the LayoutInflate to avoid asking for a new one each time.  
        mInflater = LayoutInflater.from(context);  
        mDataObjects = objects;  
        mViewId = viewid;
下面,代码膨胀传递给构造函数的xml布局,并调用
createHolder

view = mInflater.inflate(mViewId, null);  
// call the user's implementation  
holder = createHolder(view); 
因此,请确保在实例化
ClickableListAdapter
时,传递的是
布局,而不是
id

编辑 您必须使用以下链接创建xml布局:

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout  
  xmlns:android="http://schemas.android.com/apk/res/android"  
  android:layout_width="fill_parent"  
  android:layout_height="wrap_content"  
  android:orientation="horizontal"  
  android:gravity="center_vertical"  
  >  

<TextView android:text="Text" android:id="@+id/listitem_text"  
            android:layout_weight="1"   
            android:layout_width="fill_parent"   
            android:layout_height="wrap_content"  
            ></TextView>  
<ImageView android:id="@+id/listitem_icon"  
            android:src="@drawable/globe2_32x32"  
            android:layout_width="wrap_content"   
            android:layout_height="wrap_content"  
            android:maxWidth="32px"  
            android:maxHeight="32px"  
            >  
</ImageView>  
</LinearLayout>

就像我以前写的那样。。。此处:在触摸模式下,列表视图不能由它们自己单击。。。我仍然没有解决办法。。。Log.d(“测试…”,“v.getClass():”+v.getClass().toString());显示:TextView Log.d(“测试…”,“v.getParent().getClass():”+v.getClass().toString());什么也看不出来。。。我被困在这个问题上:(@Beasly:你也可以查看列表在这里是如何编码的-对不起,你读了我刚才写的东西了吗?我以前见过这个…我在这里发布之前一直在寻找解决方案…当然我也在浏览API@Beasly:很抱歉给您带来任何困惑。我想说的是,我没有做任何额外的事情来让列表项在我的项目中可点击。这很奇怪,你有这样的问题,所以我怀疑smth是以错误的方式编码的。如果这不是你的情况,再次抱歉。没问题…我愿意接受任何建议让它工作…我恢复的代码在链接上实际上我想我正在使用:adapter=new MyClickableChannelListAdapter(这个,android.R.layout.simple_list_item_1,channelList);setListAdapter(adapter);不是吗?或者我必须做些别的事情才能得到我自己的(XML)布局吗?哦,天哪……我发现了这个问题……太微不足道了……在这里创建适配器的列表是错误的:dapter=new myclickablechannellistapter(这个,R.layout.mylistrow,channelList);setListAdapter(适配器);我的意思是我使用了错误的xml。谢谢你的帮助…我在几个小时内搜索了这样一个小东西…好的,我还有一件事与此相关…当我选择项目1时,让我们假设我再次向下和向上滚动项目2被选中…我正在调查它,但如果有人知道这个问题,我感谢任何建议:)我还没有详细查看实现,但是检查传递给
getItemId
position
的不同值(只是一个想法,否则,您应该发布一个新问题)-祝您好运。
<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout  
  xmlns:android="http://schemas.android.com/apk/res/android"  
  android:layout_width="fill_parent"  
  android:layout_height="wrap_content"  
  android:orientation="horizontal"  
  android:gravity="center_vertical"  
  >  

<TextView android:text="Text" android:id="@+id/listitem_text"  
            android:layout_weight="1"   
            android:layout_width="fill_parent"   
            android:layout_height="wrap_content"  
            ></TextView>  
<ImageView android:id="@+id/listitem_icon"  
            android:src="@drawable/globe2_32x32"  
            android:layout_width="wrap_content"   
            android:layout_height="wrap_content"  
            android:maxWidth="32px"  
            android:maxHeight="32px"  
            >  
</ImageView>  
</LinearLayout>
adapter = new MyClickableChannelListAdapter(this, R.layout.mylistrow, channelList); 
setListAdapter(adapter);