ListView的setOnClickListener不起作用

所以即时通讯设法build立一个setOnClickListener为我的ListView,但它导致在我的程序崩溃因为某种原因,当我尝试..即时通讯相当新的编程,所以当涉及到故障排除我真的不能做任何事情悲伤:(我的代码是所以任何关于可能出错的想法都是非常有帮助的

public class HomePageActivity extends Activity { //ListView that holds the items ListView lstTest; //Array Adapter that holds the ArrayList and displays the items on the ListView AlertsAdapter arrayAdapter; //List that hosts the items ArrayList<Alerts> alrts = null; /** Called when the activity is first created. */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.homepage); //Initialize the ListView lstTest = (ListView)findViewById(R.id.lstText); //Initialize the ArrayList alrts = new ArrayList<Alerts>(); //Initialize the array adapter arrayAdapter = new AlertsAdapter(HomePageActivity.this, R.layout.listitems,alrts); //Set the above adapter as the adapter of choice for our list lstTest.setAdapter(arrayAdapter); lstTest.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent intent = new Intent( HomePageActivity.this, PromotionActivity.class ); startActivity(intent); } }); //... ... ... } } 

有什么想法吗? 我坚持:(

编辑:从ddms发布错误日志文件

 08-04 04:14:26.351: ERROR/AndroidRuntime(8882): FATAL EXCEPTION: main 08-04 04:14:26.351: ERROR/AndroidRuntime(8882): java.lang.RuntimeException: Unable to start activity ComponentInfo{hypenyte.bdutcher.promopost/hypenyte.bdutcher.promopost.HomePageActivity}: java.lang.RuntimeException: Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead 08-04 04:14:26.351: ERROR/AndroidRuntime(8882): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 08-04 04:14:26.351: ERROR/AndroidRuntime(8882): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 08-04 04:14:26.351: ERROR/AndroidRuntime(8882): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 08-04 04:14:26.351: ERROR/AndroidRuntime(8882): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 08-04 04:14:26.351: ERROR/AndroidRuntime(8882): at android.os.Handler.dispatchMessage(Handler.java:99) 08-04 04:14:26.351: ERROR/AndroidRuntime(8882): at android.os.Looper.loop(Looper.java:123) 08-04 04:14:26.351: ERROR/AndroidRuntime(8882): at android.app.ActivityThread.main(ActivityThread.java:4627) 08-04 04:14:26.351: ERROR/AndroidRuntime(8882): at java.lang.reflect.Method.invokeNative(Native Method) 08-04 04:14:26.351: ERROR/AndroidRuntime(8882): at java.lang.reflect.Method.invoke(Method.java:521) 08-04 04:14:26.351: ERROR/AndroidRuntime(8882): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 08-04 04:14:26.351: ERROR/AndroidRuntime(8882): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 08-04 04:14:26.351: ERROR/AndroidRuntime(8882): at dalvik.system.NativeStart.main(Native Method) 08-04 04:14:26.351: ERROR/AndroidRuntime(8882): Caused by: java.lang.RuntimeException: Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead 08-04 04:14:26.351: ERROR/AndroidRuntime(8882): at android.widget.AdapterView.setOnClickListener(AdapterView.java:750) 08-04 04:14:26.351: ERROR/AndroidRuntime(8882): at hypenyte.bdutcher.promopost.HomePageActivity.onCreate(HomePageActivity.java:58) 08-04 04:14:26.351: ERROR/AndroidRuntime(8882): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 08-04 04:14:26.351: ERROR/AndroidRuntime(8882): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 08-04 04:14:26.351: ERROR/AndroidRuntime(8882): ... 11 more 

我看到它说我可能想setOnItemClickListener这是最初是我编码为,但它不会工作,因为日食告诉我,我的代码是错的

Solutions Collecting From Web of "ListView的setOnClickListener不起作用"

你需要使用setOnItemClickListener()

然后new android.widget.AdapterView.OnItemClickListener()

不是new View.OnItemClickListener()

不要使用setOnClickListener ,请尝试使用setOnItemClickListener

如果使用Butterknife,请检查您是否没有适用于AdapterView后代(微调,ListView等)的OnClick注释。

 @OnClick(R.id.spinner_id) 

调用ButterKnife.inject(this, view); 会抛出exception

Caused by: java.lang.RuntimeException: Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead

首先在你使用你的列表视图的主要活动中设置值:

 new android.widget.AdapterView.OnItemClickListener() 

代替你的listview的适配器:

  new AdapterView.OnItemClickListener() 

所以最后它看起来像:

 this.listView.setItemsCanFocus(false); this.listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() { @Override public void onItemClick(android.widget.AdapterView<?> parent, View view, int position, long id) { } 

当您使用任何自定义视图来填充listview中的数据。

然后在你父主布局{在里面列表视图}的主要Activity文件中设置这个选项:

  android:descendantFocusability="blocksDescendants" 

如果你在列表视图中使用任何button/文本视图,请在该视图/button上设置此属性:

  android:focusable="false" android:focusableInTouchMode="false" 

对于你的listview中的滚动选项 ,只需在activity内添加这一行:

  listView=(ListView)findViewById(R.id.container); listView.setNestedScrollingEnabled(true); 

根据我的理解,我给出的答案,我希望这将有所帮助,如果有人发现一些更正或有帮助,他们是受欢迎的。