Android 在listview上加载数据
我想通过一个中间类将数据加载到listview,通过构造函数传输数据。但在数据加载过程中遇到了一个我无法解决的错误,希望大家能帮助我 类loadListviewAndroid 在listview上加载数据,android,Android,我想通过一个中间类将数据加载到listview,通过构造函数传输数据。但在数据加载过程中遇到了一个我无法解决的错误,希望大家能帮助我 类loadListview public class LoadListView { private Context mContext = null; ArrayList<String> phonenumberArray = new ArrayList<String>(); ArrayList<String> codeInvoic
public class LoadListView {
private Context mContext = null;
ArrayList<String> phonenumberArray = new ArrayList<String>();
ArrayList<String> codeInvoiceArray = new ArrayList<String>();
ArrayList<String> nameArray = new ArrayList<String>();
ArrayList<String> addressArray = new ArrayList<String>();
ArrayList<String> urlArray = new ArrayList<String>();
ListView listview=(ListView)((Activity)mContext).findViewById(R.id.listView1);
String name,phonenumber,address,url,code,iddelivery;
public LoadListView(Context mContext, String name,String phone,String address,String url,String code, String ID)
{
this.mContext=mContext;
this.name=name;
this.phonenumber=phone;
this.code=code;
this.address=address;
this.url=url;
this.iddelivery=ID;
}
// Xóa các item trong mảng để tránh sự trùng lặp
public void clearArray()
{
codeInvoiceArray.clear();
nameArray.clear();
phonenumberArray.clear();
addressArray.clear();
urlArray.clear();
}
// load dữ liệu lên listview
public void loaddatalistview()
{
clearArray();
phonenumberArray.add(phonenumber);
codeInvoiceArray.add(code);
nameArray.add(name);
addressArray.add(address);
urlArray.add(url);
listview.setAdapter(new DataAdapter(mContext, codeInvoiceArray
.toArray(new String[codeInvoiceArray.size()]), nameArray
.toArray(new String[nameArray.size()]), phonenumberArray
.toArray(new String[phonenumberArray.size()]), addressArray
.toArray(new String[addressArray.size()])));
listview.setOnItemClickListener(new OnItemClickListener() {
// xử lý sự kiện khi chọn item trên listview
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent j = new Intent(mContext, Menu_Main.class);
// lấy thông tin thông qua vị trí của item được chọn
String url = urlArray.get(position);
String address = addressArray.get(position);
String mahoadon = codeInvoiceArray.get(position);
String phone = phonenumberArray.get(position);
// chuyển thông tin này qua bên phần MENU
j.putExtra("url", url);
j.putExtra("address", address);
j.putExtra("mahoadon", mahoadon);
j.putExtra("phone", phone);
j.putExtra("ID", iddelivery);
mContext.startActivity(j);
}
});
}
}
错误通知
12-25 14:03:38.033: W/System.err(21335): java.lang.NullPointerException
12-25 14:03:38.041: W/System.err(21335): at Support.LoadListView.<init>(LoadListView.java:23)
12-25 14:03:38.041: W/System.err(21335): at com.longvan.saigonfleamarket.Invoice.workoffline(Invoice.java:226)
12-25 14:03:38.041: W/System.err(21335): at com.longvan.saigonfleamarket.Invoice.onCreate(Invoice.java:65)
12-25 14:03:38.041: W/System.err(21335): at android.app.Activity.performCreate(Activity.java:4465)
12-25 14:03:38.041: W/System.err(21335): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
12-25 14:03:38.041: W/System.err(21335): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
12-25 14:03:38.041: W/System.err(21335): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
12-25 14:03:38.041: W/System.err(21335): at android.app.ActivityThread.access$600(ActivityThread.java:128)
12-25 14:03:38.041: W/System.err(21335): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
12-25 14:03:38.041: W/System.err(21335): at android.os.Handler.dispatchMessage(Handler.java:99)
12-25 14:03:38.041: W/System.err(21335): at android.os.Looper.loop(Looper.java:137)
12-25 14:03:38.041: W/System.err(21335): at android.app.ActivityThread.main(ActivityThread.java:4514)
12-25 14:03:38.041: W/System.err(21335): at java.lang.reflect.Method.invokeNative(Native Method)
12-25 14:03:38.041: W/System.err(21335): at java.lang.reflect.Method.invoke(Method.java:511)
12-25 14:03:38.041: W/System.err(21335): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
12-25 14:03:38.041: W/System.err(21335): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
12-25 14:03:38.041: W/System.err(21335): at dalvik.system.NativeStart.main(Native Method)
12-25 14:03:38.041: I/System.out(21335): Workoffline!!!
12-25 14:03:38.033:W/System.err(21335):java.lang.NullPointerException
12-25 14:03:38.041:W/System.err(21335):at Support.LoadListView。(LoadListView.java:23)
12-25 14:03:38.041:W/System.err(21335):位于com.longvan.saigonfleemarket.Invoice.workufline(Invoice.java:226)
12-25 14:03:38.041:W/System.err(21335):位于com.longvan.saigonfleemarket.Invoice.onCreate(Invoice.java:65)
12-25 14:03:38.041:W/System.err(21335):位于android.app.Activity.performCreate(Activity.java:4465)
12-25 14:03:38.041:W/System.err(21335):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
12-25 14:03:38.041:W/System.err(21335):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
12-25 14:03:38.041:W/System.err(21335):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
12-25 14:03:38.041:W/System.err(21335):在android.app.ActivityThread.access$600(ActivityThread.java:128)
12-25 14:03:38.041:W/System.err(21335):位于android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
12-25 14:03:38.041:W/System.err(21335):位于android.os.Handler.dispatchMessage(Handler.java:99)
12-25 14:03:38.041:W/System.err(21335):在android.os.Looper.loop(Looper.java:137)
12-25 14:03:38.041:W/System.err(21335):位于android.app.ActivityThread.main(ActivityThread.java:4514)
12-25 14:03:38.041:W/System.err(21335):位于java.lang.reflect.Method.invokenactive(本机方法)
12-25 14:03:38.041:W/System.err(21335):位于java.lang.reflect.Method.invoke(Method.java:511)
12-25 14:03:38.041:W/System.err(21335):在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
12-25 14:03:38.041:W/System.err(21335):位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
12-25 14:03:38.041:W/System.err(21335):在dalvik.System.NativeStart.main(本机方法)
12-25 14:03:38.041:I/System.out(21335):工作线!!!
这一行看起来不正确,因为您试图调用空对象上的方法:
ListView listview=(ListView)((Activity)mContext).findViewById(R.id.listView1);
这应该在构造函数中完成:
public class LoadListView {
// ... (declare ListView with other class variables)
ListView listview;
public LoadListView(Context mContext, String name,String phone,String address,String url,String code, String ID)
{
// ... (setting other class variables)
this.listview = (ListView)((Activity)mContext).findViewById(R.id.listView1);
}
请按照日志记录检查您的23号线 我建议,您应该检查mContext是否为null
mContext!=空
因此,只需在之前获取“mContext”的值即可
跟踪您的代码,找出哪一行导致NullPointerException它清楚地显示在第23行中您得到了错误。。。。
public class LoadListView {
// ... (declare ListView with other class variables)
ListView listview;
public LoadListView(Context mContext, String name,String phone,String address,String url,String code, String ID)
{
// ... (setting other class variables)
this.listview = (ListView)((Activity)mContext).findViewById(R.id.listView1);
}
ListView listview=(ListView)((Activity)mContext).findViewById(R.id.listView1);