我在我的列表视图上获得了 NPE ..所以当我单击按钮时,它会显示我在另一个表单上设置的数据..
这是我的自定义适配器代码:
public class AdapterListPemesananMenu extends BaseAdapter {
public static ArrayList<MenuInputClass> searchArrayList;
private LayoutInflater mInflater;
public AdapterListPemesananMenu(Context context, ArrayList<MenuInputClass> results) {
searchArrayList = results;
mInflater = LayoutInflater.from(context);
}
public AdapterListPemesananMenu(OnClickListener onClickListener,
ArrayList<MenuInputClass> searchResults) {
// TODO Auto-generated constructor stub
}
public int getCount() {
return searchArrayList.size(); *********************the NPE is here
}
public Object getItem(int position) {
return searchArrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_pemesananmenu_row, null);
holder = new ViewHolder();
holder.txtName = (TextView) convertView.findViewById(R.id.txtNamaMasuk);
holder.txtCityState = (TextView) convertView
.findViewById(R.id.txtJumlahMasuk);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtName.setText(searchArrayList.get(position).getNama_menu());
holder.txtCityState.setText(searchArrayList.get(position)
.getJumlah_menu());
return convertView;
}
static class ViewHolder {
TextView txtName;
TextView txtCityState;
}
}
这是我的活动代码:
public class SingleListView_Activity extends Activity{
Button order;
public static ListView list_pemesanan;
public static String nama_menu;
TextView namamenu;
public static String jumlah_menu;
EditText jumlahmenu;
//public static ArrayList<MenuInputClass> searchResults = GetSearchResults();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_list_view);
ListView list_single_list_view=(ListView)findViewById(R.id.list_single);
list_pemesanan = (ListView)findViewById(R.id.list);
namamenu=(TextView)findViewById(R.id.nama_menu2);
jumlahmenu = (EditText)findViewById(R.id.txtJumlahOrder);
order=(Button)findViewById(R.id.btn_order);
//when this button on click its set the data to my another form
order.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ArrayList<MenuInputClass> searchResults = GetSearchResults();
ListPemesanan_Activity.list_pemesanan.setAdapter(new AdapterListPemesananMenu(this,searchResults));
}
});
}
private ArrayList<MenuInputClass> GetSearchResults(){
//this is the data that i set
ArrayList<MenuInputClass> results = new ArrayList<MenuInputClass>();
MenuInputClass sr = new MenuInputClass();
sr.setJumlah_menu("asdas");
sr.setNama_menu("2");
results.add(sr);
return results;
}
}
日志猫:
10-23 04:33:19.759: E/AndroidRuntime(14459): FATAL EXCEPTION: main
10-23 04:33:19.759: E/AndroidRuntime(14459): java.lang.NullPointerException
10-23 04:33:19.759: E/AndroidRuntime(14459): at list_pemesananmenu_view.AdapterListPemesananMenu.getCount(AdapterListPemesananMenu.java:35)
10-23 04:33:19.759: E/AndroidRuntime(14459): at android.widget.ListView.setAdapter(ListView.java:479)
10-23 04:33:19.759: E/AndroidRuntime(14459): at list_pemesananmenu_view.SingleListView_Activity$1.onClick(SingleListView_Activity.java:123)
10-23 04:33:19.759: E/AndroidRuntime(14459): at android.view.View.performClick(View.java:2538)
10-23 04:33:19.759: E/AndroidRuntime(14459): at android.view.View$PerformClick.run(View.java:9152)
10-23 04:33:19.759: E/AndroidRuntime(14459): at android.os.Handler.handleCallback(Handler.java:587)
10-23 04:33:19.759: E/AndroidRuntime(14459): at android.os.Handler.dispatchMessage(Handler.java:92)
10-23 04:33:19.759: E/AndroidRuntime(14459): at android.os.Looper.loop(Looper.java:130)
10-23 04:33:19.759: E/AndroidRuntime(14459): at android.app.ActivityThread.main(ActivityThread.java:3693)
10-23 04:33:19.759: E/AndroidRuntime(14459): at java.lang.reflect.Method.invokeNative(Native Method)
10-23 04:33:19.759: E/AndroidRuntime(14459): at java.lang.reflect.Method.invoke(Method.java:507)
10-23 04:33:19.759: E/AndroidRuntime(14459): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
10-23 04:33:19.759: E/AndroidRuntime(14459): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
10-23 04:33:19.759: E/AndroidRuntime(14459): at dalvik.system.NativeStart.main(Native Method)
10-23 04:33:19.769: E/(195): Dumpstate > /data/log/dumpstate_app_error
10-23 04:33:20.059: E/AudioPolicyManager(129): stopOutput stream = 1 , mHardwareOutput = 1, output = 1
任何帮助将不胜感激...谢谢!