0

我想通过一个中间类将数据加载到listview,我通过构造函数传输数据。但是在数据加载过程中遇到了我无法解决的错误,希望大家帮帮我。

类加载列表视图

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);
        }
    });
}

}

类主

public class Invoice extends Activity {
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.invoice);
    mDb.openDB();
        Cursor mCursor = mDb.getAllInvoice(IDDelivery);
        if (mCursor.moveToFirst()) {
            do {

                LoadListView load=new LoadListView(Invoice.this, mCursor.getString(1), mCursor.getString(2) ,mCursor.getString(3), mCursor.getString(4), mCursor.getString(5), IDDelivery);
                load.loaddatalistview();

            } while (mCursor.moveToNext());
            //loaddatalistview();
        }
        mDb.closeDB();
}

错误通知

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!!!
4

2 回答 2

0

此行看起来不正确,因为您试图在空对象上调用方法:

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);
}
于 2012-12-25T07:24:12.247 回答
0

请按照 log-cat 的说法检查您的第 23 行,

我建议,您应该检查 mContext 是否不为空 mContext != null

所以只需在之前获取“ mContext ”的值

ListView listview=(ListView)((Activity)mContext).findViewById(R.id.listView1);
于 2012-12-25T07:26:09.227 回答