我想问..我有 2 个表格,说它是表格 1 和表格 2,我在表格 1 上有列表视图,我想从表格 2 设置数据..所以我只是创建一些虚拟项目来尝试它,我想知道为什么不能工作..
所以这是我在form2上的代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.form2);
ArrayList<MyClass> searchResults = GetSearchResults();
form1.listview.setAdapter(new MyAdapter(this, searchResults));
}
private ArrayList<MyClass> GetSearchResults(){
ArrayList<MyClass> results = new ArrayList<MyClass>();
MyClass sr = new MyClass();
sr.setJumlah_menu("Justin Schultz");
sr.setNama_menu("San Francisco, CA");
results.add(sr);
return results;
}
这是我的适配器代码:
public class AdapterListPemesananMenu extends BaseAdapter {
private 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);
//holder.txtPhone = (TextView) convertView.findViewById(R.id.phone);
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;
}
}
当我从form1转到form2时,它总是强制关闭..
我的代码有什么问题?
当我在form1上尝试代码(来自form2)时,它运行良好..
这是来自 logcat :
10-22 04:33:30.647: E/AndroidRuntime(15247): FATAL EXCEPTION: main
10-22 04:33:30.647: E/AndroidRuntime(15247): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aplikasipemesananmenu/list_pemesananmenu_view.SingleListView_Activity}: java.lang.NullPointerException
10-22 04:33:30.647: E/AndroidRuntime(15247): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
10-22 04:33:30.647: E/AndroidRuntime(15247): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
10-22 04:33:30.647: E/AndroidRuntime(15247): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-22 04:33:30.647: E/AndroidRuntime(15247): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
10-22 04:33:30.647: E/AndroidRuntime(15247): at android.os.Handler.dispatchMessage(Handler.java:99)
10-22 04:33:30.647: E/AndroidRuntime(15247): at android.os.Looper.loop(Looper.java:130)
10-22 04:33:30.647: E/AndroidRuntime(15247): at android.app.ActivityThread.main(ActivityThread.java:3693)
10-22 04:33:30.647: E/AndroidRuntime(15247): at java.lang.reflect.Method.invokeNative(Native Method)
10-22 04:33:30.647: E/AndroidRuntime(15247): at java.lang.reflect.Method.invoke(Method.java:507)
10-22 04:33:30.647: E/AndroidRuntime(15247): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
10-22 04:33:30.647: E/AndroidRuntime(15247): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
10-22 04:33:30.647: E/AndroidRuntime(15247): at dalvik.system.NativeStart.main(Native Method)
10-22 04:33:30.647: E/AndroidRuntime(15247): Caused by: java.lang.NullPointerException
10-22 04:33:30.647: E/AndroidRuntime(15247): at list_pemesananmenu_view.SingleListView_Activity.onCreate(SingleListView_Activity.java:67)
10-22 04:33:30.647: E/AndroidRuntime(15247): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-22 04:33:30.647: E/AndroidRuntime(15247): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
10-22 04:33:30.647: E/AndroidRuntime(15247): ... 11 more
10-22 04:33:31.958: E/DataRouter(125): smd_file_fd = 16 and external_file_fd = 24
10-22 04:33:31.958: E/DataRouter(125): ==KDW(3)== smd_new_status = (358)
10-22 04:33:31.958: E/DataRouter(125): ==KDW(4)== smd_new_status = (320)
10-22 04:33:31.958: E/DataRouter(125): DCD pinstatus is ON
任何帮助将不胜感激谢谢...