我在我的非活动课上获得了NPE ..在此活动中我设置了
按钮(setonclicklistener)设置我的自定义适配器..我不知道如何解决它,因为我
在编程方面不够好我很困惑,我必须更改代码..
这里是我的自定义AdapterListMenu类:
public class AdapterListMenu extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
Context context;
public AdapterListMenu(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public AdapterListMenu(Context context) {
this.context=context;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_menurow, null);
final TextView nama_menu = (TextView)vi.findViewById(R.id.nama_menu); // title
TextView keterangan_menu = (TextView)vi.findViewById(R.id.keterangan_menu); // artist name
TextView harga_menu = (TextView)vi.findViewById(R.id.harga_menu); // duration
ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
final EditText listmenu_jumlahmenu = (EditText)vi.findViewById(R.id.txtListMenu_Jumlah);
final Button listmenu_submit=(Button)vi.findViewById(R.id.btn_ListMenu_Submit);
listmenu_submit.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
String nama_menus = nama_menu.getText().toString();
String jumlah_menus = listmenu_jumlahmenu.getText().toString();
MenuInputClass sr = new MenuInputClass();
sr.setNama_menu(nama_menus);
sr.setJumlah_menu(jumlah_menus);
Appetizer_Activity.results.add(sr);
ListPemesanan_Activity.list_pemesanan.setAdapter(new AdapterListPemesananMenu(getApplicationContext(), Appetizer_Activity.results));
--->i think the problem is here
}
private Context getApplicationContext() {
// TODO Auto-generated method stub
return null;
}
});
HashMap<String, String> listmenudisplay = new HashMap<String, String>();
listmenudisplay = data.get(position);
// Setting all values in listview
nama_menu.setText(listmenudisplay.get(ListAppetizer_Activity.parser_nama_menu));
keterangan_menu.setText(listmenudisplay.get(ListAppetizer_Activity.parser_keterangan_menu));
harga_menu.setText(listmenudisplay.get(ListAppetizer_Activity.parser_harga_menu));
imageLoader.DisplayImage(listmenudisplay.get(ListAppetizer_Activity.parser_gambar), thumb_image);
return vi;
}
适配器列表pemesanan菜单类:
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();
}
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;
}
}
列出pemesanan菜单xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.76" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="14dp"
android:text="No Meja :" />
<EditText
android:id="@+id/txtNoMeja"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:layout_toRightOf="@+id/textView2"
android:ems="10" >
<requestFocus />
</EditText>
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_below="@+id/txtNoMeja"
android:layout_centerHorizontal="true"
android:choiceMode="multipleChoice"
android:textColor="#ffff00"
>
</ListView>
<Button
android:id="@+id/btn_submit_list_pemesanan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="Submit" />
</RelativeLayout>
</LinearLayout>
列出pemesanan菜单行布局
<?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" >
<TextView
android:id="@+id/txtNamaMasuk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="TextView"
android:textColor="@color/textbody" />
<TextView
android:id="@+id/txtJumlahMasuk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="38dp"
android:text="TextView"
android:textColor="@color/textbody" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/txtJumlahMasuk"
android:text="Jumlah :"
android:textColor="@color/textbody" />
</RelativeLayout>
这是logcat:
10-31 03:44:23.111: E/AndroidRuntime(13386): FATAL EXCEPTION: main
10-31 03:44:23.111: E/AndroidRuntime(13386): java.lang.NullPointerException
10-31 03:44:23.111: E/AndroidRuntime(13386): at android.view.LayoutInflater.from(LayoutInflater.java:171)
10-31 03:44:23.111: E/AndroidRuntime(13386): at xml_parser_and_adapter.AdapterListPemesananMenu.<init>(AdapterListPemesananMenu.java:25)
10-31 03:44:23.111: E/AndroidRuntime(13386): at xml_parser_and_adapter.AdapterListMenu$4.onClick(AdapterListMenu.java:158)
10-31 03:44:23.111: E/AndroidRuntime(13386): at android.view.View.performClick(View.java:2538)
10-31 03:44:23.111: E/AndroidRuntime(13386): at android.view.View$PerformClick.run(View.java:9152)
10-31 03:44:23.111: E/AndroidRuntime(13386): at android.os.Handler.handleCallback(Handler.java:587)
10-31 03:44:23.111: E/AndroidRuntime(13386): at android.os.Handler.dispatchMessage(Handler.java:92)
10-31 03:44:23.111: E/AndroidRuntime(13386): at android.os.Looper.loop(Looper.java:130)
10-31 03:44:23.111: E/AndroidRuntime(13386): at android.app.ActivityThread.main(ActivityThread.java:3693)
10-31 03:44:23.111: E/AndroidRuntime(13386): at java.lang.reflect.Method.invokeNative(Native Method)
10-31 03:44:23.111: E/AndroidRuntime(13386): at java.lang.reflect.Method.invoke(Method.java:507)
10-31 03:44:23.111: E/AndroidRuntime(13386): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
10-31 03:44:23.111: E/AndroidRuntime(13386): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
我该怎么办?
谢谢......用以下代码替换我的代码:
ListPemesanan_Activity.list_pemesanan.setAdapter(new AdapterListPemesananMenu(activity, Appetizer_Activity.results));
11-01 16:56:26.108: E/AndroidRuntime(7758): FATAL EXCEPTION: main
11-01 16:56:26.108: E/AndroidRuntime(7758): java.lang.NullPointerException
11-01 16:56:26.108: E/AndroidRuntime(7758): at android.view.ViewConfiguration.get(ViewConfiguration.java:228)
11-01 16:56:26.108: E/AndroidRuntime(7758): at android.view.View.<init>(View.java:1888)
11-01 16:56:26.108: E/AndroidRuntime(7758): at android.view.View.<init>(View.java:1931)
11-01 16:56:26.108: E/AndroidRuntime(7758): at android.view.ViewGroup.<init>(ViewGroup.java:292)
11-01 16:56:26.108: E/AndroidRuntime(7758): at android.widget.AdapterView.<init>(AdapterView.java:228)
11-01 16:56:26.108: E/AndroidRuntime(7758): at android.widget.AbsListView.<init>(AbsListView.java:686)
11-01 16:56:26.108: E/AndroidRuntime(7758): at android.widget.ListView.<init>(ListView.java:188)
11-01 16:56:26.108: E/AndroidRuntime(7758): at android.widget.ListView.<init>(ListView.java:184)
11-01 16:56:26.108: E/AndroidRuntime(7758): at android.widget.ListView.<init>(ListView.java:180)
11-01 16:56:26.108: E/AndroidRuntime(7758): at xml_parser_and_adapter.AdapterListMenu$4.onClick(AdapterListMenu.java:152)
11-01 16:56:26.108: E/AndroidRuntime(7758): at android.view.View.performClick(View.java:2538)
11-01 16:56:26.108: E/AndroidRuntime(7758): at android.view.View$PerformClick.run(View.java:9152)
11-01 16:56:26.108: E/AndroidRuntime(7758): at android.os.Handler.handleCallback(Handler.java:587)
11-01 16:56:26.108: E/AndroidRuntime(7758): at android.os.Handler.dispatchMessage(Handler.java:92)
11-01 16:56:26.108: E/AndroidRuntime(7758): at android.os.Looper.loop(Looper.java:130)
11-01 16:56:26.108: E/AndroidRuntime(7758): at android.app.ActivityThread.main(ActivityThread.java:3693)
11-01 16:56:26.108: E/AndroidRuntime(7758): at java.lang.reflect.Method.invokeNative(Native Method)
11-01 16:56:26.108: E/AndroidRuntime(7758): at java.lang.reflect.Method.invoke(Method.java:507)
答案 0 :(得分:0)
试试这个
ListPemesanan_Activity.list_pemesanan.setAdapter(new AdapterListPemesananMenu(activity, Appetizer_Activity.results));
--->i think the problem is here
而不是
ListPemesanan_Activity.list_pemesanan.setAdapter(new AdapterListPemesananMenu(getApplicationContext(), Appetizer_Activity.results));
--->i think the problem is here