ListView 的 setOnClickListener 不起作用 [英] setOnClickListener of a ListView not working
问题描述
所以我试图为我的 ListView
设置一个 setOnClickListener
但是当我尝试时它由于某种原因导致我的程序崩溃......我对编程还很陌生,所以在进行故障排除时,我真的无法做任何事情,可悲的是 :( 我的代码在下面,所以任何关于可能出错的想法都会非常有帮助:
So I'm trying to set up a setOnClickListener
for my ListView
but it's causing a crash in my program for some reason when I try... I'm quite new to programming so when it comes to troubleshooting I can't really do anything sadly :( my code is below so any ideas on what could be wrong would be extremely helpful:
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);
}
});
//... ... ...
}
}
有什么想法吗?我卡住了:(
Any thoughts? I'm stuck :(
从 ddms 发布错误日志文件:
Posting error log file from 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
,它最初是我编码的,但它不起作用,因为 eclipse 告诉我我的代码有问题.
I saw that it said I probably want setOnItemClickListener
which is originally what I had coded it as, but that it wouldn't work since eclipse told me my code was wrong with it.
推荐答案
你需要使用 setOnItemClickListener()
然后new android.widget.AdapterView.OnItemClickListener()
不是 new View.OnItemClickListener()
这篇关于ListView 的 setOnClickListener 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!