在asynctask Android中添加数据库连接

时间:2014-08-04 04:36:03

标签: java android android-asynctask

我正在尝试在asynctask中添加数据库连接。这是我正在处理的代码:

DailySync.class:

DownloadSupplierMaster objAsyncTask;

// TODO get all checked items
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.daily_synchronize);

    initControls();

    objAsyncTask = new DownloadSupplierMaster(this);     

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.dailysync_menu, menu);

        final MenuItem itemSync;

        itemSync = menu.findItem(R.id.sync);


        itemSync.setOnMenuItemClickListener(new OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(MenuItem item) {


                StringBuffer responseText = new StringBuffer();
                responseText.append("Selected modules are...\n");

                ArrayList<SyncDataItems> itemList = dataAdapter.itemList;

                for (int i = 0; i < itemList.size(); i++) {
                 SyncDataItems itemName = itemList.get(i);

                 if (itemName.isSelected()) {
                  responseText.append("\n" + itemName.getItem() );
                 }
                }


                objAsyncTask.execute();


                return true;
            }
        });


        return super.onCreateOptionsMenu(menu);
}


@Override
public void onTaskComplete(String result) {


}

DownloadTask.class:

public class DownloadTask extends AsyncTask<Void, Void, Void> {
private Activity activity;
private AsyncTaskListener callback;

public DownloadSupplierMaster(Activity act) {
 this.activity = act;
 this.callback = (AsyncTaskListener)act;
}

@Override
protected void onPreExecute() {
        super.onPreExecute();

}

protected Void doInBackground(Void...params) {

    arraylist = new ArrayList<HashMap<String, String>>();

    // Retrieve JSON Objects from the given website URL in JSONfunctions.class
    String result = JSONFunctions.getJSONfromURL(URL);

    try {
        JSONArray jr = new JSONArray(result);
        HashMap<String, String> map = new HashMap<String, String>();

        String[] keys = { SupplierMaster.TAG_SUPPLIERCODE, SupplierMaster.TAG_SUPPLIERNAME, 
                SupplierMaster.TAG_SUPPLIERGROUP, SupplierMaster.TAG_SUPPLIERTYPE, 
                SupplierMaster.TAG_SUPPLIERADDRESS, SupplierMaster.TAG_SUPPLIERADDRESSALT, SupplierMaster.TAG_PHONE1, 
                SupplierMaster.TAG_PHONE2, SupplierMaster.TAG_FAX,
                SupplierMaster.TAG_EMAIL, SupplierMaster.TAG_WEBSITE, 
                SupplierMaster.TAG_EXPENSEACCOUNT, SupplierMaster.TAG_CONTACTPERSON,
                SupplierMaster.TAG_CREDITLIMIT, SupplierMaster.TAG_TOTALCREDIT, SupplierMaster.TAG_TIN,
                SupplierMaster.TAG_TAXCODE, SupplierMaster.TAG_TERMS };

        for(int i=0;i<jr.length();i++) {    

            jb = (JSONObject)jr.get(i);

            for (String key : keys) {
                map.put(key, jb.getString(key));
            }
            arraylist.add(map);

            String suppliercode = jb.getString(SupplierMaster.TAG_SUPPLIERCODE);
            String suppliername = jb.getString(SupplierMaster.TAG_SUPPLIERNAME);
            String suppliergroup = jb.getString(SupplierMaster.TAG_SUPPLIERGROUP);
            String suppliertype = jb.getString(SupplierMaster.TAG_SUPPLIERTYPE);
            String supplieraddress = jb.getString(SupplierMaster.TAG_SUPPLIERADDRESS);
            String supplieraddressalt = jb.getString(SupplierMaster.TAG_SUPPLIERADDRESSALT);
            String phone1 = jb.getString(SupplierMaster.TAG_PHONE1);
            String phone2 = jb.getString(SupplierMaster.TAG_PHONE2);
            String fax = jb.getString(SupplierMaster.TAG_FAX);
            String email = jb.getString(SupplierMaster.TAG_EMAIL);
            String website = jb.getString(SupplierMaster.TAG_WEBSITE);
            String expenseaccount = jb.getString(SupplierMaster.TAG_EXPENSEACCOUNT);
            String contactperson = jb.getString(SupplierMaster.TAG_CONTACTPERSON);
            String creditlimit = jb.getString(SupplierMaster.TAG_CREDITLIMIT);
            String totalcredit = jb.getString(SupplierMaster.TAG_TOTALCREDIT);
            String tin = jb.getString(SupplierMaster.TAG_TIN);
            String taxcode = jb.getString(SupplierMaster.TAG_TAXCODE);
            String terms = jb.getString(SupplierMaster.TAG_TERMS);

            context.dbConnect();   // I'M GETTING A NULLPOINTEREXCEPTION IN THIS LINE!!!!!!!!!!!!

            dbHelper.insertORReplaceToSUPPLIERCARD(suppliercode, suppliername, suppliergroup, 
                    suppliertype, supplieraddress, supplieraddressalt, phone1, 
                    phone2, fax, email, website, expenseaccount, contactperson, 
                    creditlimit, totalcredit, tin, taxcode, terms);

            dbHelper.close();

        }




    } catch (JSONException e) {
        Log.e("Error", e.getMessage());
        e.printStackTrace();
    }

    return null;
} 

@Override
protected void onPostExecute(Void args) {
    callback.onTaskComplete("Test");
}

AysncTaskListener.class:

public interface AsyncTaskListener {
     public void onTaskComplete(String result);
}

问题是,我在context.dbConnect()中得到了nullpointerexception;

您认为这是什么问题?有任何想法吗?我很乐意感谢你的帮助。感谢。

1 个答案:

答案 0 :(得分:0)

这个上下文对象来自哪里?你能确认它有价值吗?

从设计角度来看,我建议创建一个单独的数据库助手类,然后可以从AsyncTask访问它并让它处理整个应用程序的数据库交互,因为这样会更容易处理这些类型的异常以及为应用程序维护所有与数据库相关的代码非常容易。