-1
所以我正在製作這個應用程序,其中我將有一個包含文本和其他按鈕的項目列表。帶按鈕的Android ListView
我得到這個錯誤在我的自定義適配器類:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
整個定義適配器類如下:
package com.android.ict.seneca.androidpocketguide;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
public class CustomListAdapter extends ArrayAdapter<RowItem> {
private Context context;
public CustomListAdapter(Context context,
int resourceId,
List<RowItem> items) {
super(context, resourceId, items);
this.context = context;
}
private class ViewHolder {
TextView txtName;
TextView txtDate;
TextView txtLocation;
Button button;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
RowItem rowItem = getItem(position);
LayoutInflater mInflater =
(LayoutInflater) context.getSystemService(
Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.txtName = (TextView) convertView.findViewById(R.id.nam);
holder.txtDate = (TextView) convertView.findViewById(R.id.dat);
holder.txtLocation = (TextView) convertView.findViewById(R.id.loc);
holder.button = (Button)convertView.findViewById(R.id.deleteBtn);
convertView.setTag(holder);
}
else
holder = (ViewHolder) convertView.getTag();
holder.txtName.setText(rowItem.getNameId());
holder.txtDate.setText(rowItem.getLocation());
holder.txtLocation.setText(rowItem.getDate());
holder.button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Toast.makeText(context, "Delete button pressed: ",Toast.LENGTH_LONG).show();
// Your code that you want to execute on this button click
}
});
return convertView;
}
}
我有2點的佈局,1爲ListView itelf然後另一個只是按鈕。
ListView控件的項目如下(activity_websites):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/locationList"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
按鈕XMLFILE如下(delete.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/deleteBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFFFFFFF"
android:text="Delete"
android:onClick="onClickDelete"/>
</LinearLayout>
而且我有種感覺,我在做什麼的權利現在通常是錯誤的,以獲得我想要的應用程序的視圖。我可以得到這些幫助嗎?