[英]Android textviews and buttons clickable in ListView
我用另一個用json http數組填充的xml布局填充了listview。 在列表視圖中,我有3個文本視圖,我想要點擊,但當我添加onCreate時onClick方法應用程序強制關閉。 我該怎么辦,能有人給我舉個例子嗎?
<ListView
android:id="@+id/custom_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/meni"
android:layout_marginTop="10dp"
android:dividerHeight="15.0sp"
/>
在list_row_layout中:
<TextView
android:id="@+id/acceptTV"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="5dp"
android:background="@drawable/pressed"
android:gravity="fill"
android:text="Accept"
android:focusable="false"
android:textColor="#0D98BA"
android:textSize="17sp" />
Activity類
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
feedListView= (ListView) findViewById(R.id.custom_list);
String url = "....";
new DownloadFilesTask().execute(url);
progressbar = (ProgressBar)findViewById(R.id.progressBar);
accept = (TextView) findViewById(R.id.acceptTV);
accept.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
logcat的
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{...MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
Caused by: java.lang.NullPointerException
以及更多
用戶解決問題3057145如果使用了適配器,則只能在適配器類中調用textview。 你不能在主要活動中調用textview。
並且您只能在適配器類中執行onclick事件。
holder.accept =(TextView)convertView.findViewById(R.id.acceptTV);
holder.accept.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
holder.accept.setText("dafsafag");
}
});
它接縫ComponentInfo錯誤。 此錯誤將顯示在某些ID不存在於XML但您嘗試在java文件中使用。 你可以檢查所有元素名稱是否完美調用..嘗試逐步調試....
如果每件事情都很完美,請嘗試將列表視圖ID作為列表並嘗試.............
如果使用了適配器,則只能在適配器類中調用textview。 你不能在主要活動中調用textview。
並且您只能在適配器類中執行onclick事件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.