java.lang.RuntimeException:无法在未调用Looper.prepare()的线程内创建处理程序

时间:2015-11-27 13:12:15

标签: android database sqlite xml-parsing android-sqlite

编译时,我收到java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()错误。

我搜索了很多,但我仍然无法找到解决方案 请帮我处理我的代码。

SoapObject request = new SoapObject(NAMESPACE, METHOD_GET_LOGIN_DETAILS);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
request.addProperty("loginUsr", user);
request.addProperty("loginPasswd", pass);
envelope.dotNet = true; // to handle .net services asmx/aspx
envelope.setOutputSoapObject(request);
HttpTransportSE ht = new HttpTransportSE(URL_PROTOCOL + mSettings.getMmpServerpath() + URL_SERVICE_NAME);
ht.debug = true;
String soap_action = NAMESPACE + METHOD_GET_LOGIN_DETAILS;
ht.call(soap_action, envelope);
SoapPrimitive resultString = (SoapPrimitive) envelope.getResponse();
File mImpOutputFile = Supporter.getImpOutputFilePathByCompany("MobManufacturingProcess", "00",
    "FiGoodsDetails" + ".xml");
if (!mImpOutputFile.exists()) {
    Supporter.createFile(mImpOutputFile);
} else {
    mImpOutputFile.delete(); // to refresh the file
    Supporter.createFile(mImpOutputFile);
}

BufferedWriter buf = new BufferedWriter(new FileWriter(mImpOutputFile, true));

buf.append("<" + "Details" + ">");

buf.append(resultString.toString());

buf.append("</" + "Details" + ">");
result = "success";

buf.close();

} catch (

SocketTimeoutException e)

{
    result = "time out error";
    e.printStackTrace();
} catch (

IOException e)

{
    result = "input error";
    e.printStackTrace();
} catch (

XmlPullParserException e)

{
    result = "error";
    e.printStackTrace();
} catch (

Exception e)

{
    Log.e("tag", "error", e);
    result = "error";
}
new DataLoadToDBOperation().execute();
return result;

}

}

@Override
protected void onPostExecute(String result) {
    // TODO Auto-generated method stub
    /* String[] result=totVal.get(totVal.size()); */
    // super.onPostExecute(totVal);

    if (result.equals("success")) {
        new DataLoadToDBOperation().execute();

    } else {
        mToastMsg.showToast(LogInActivity.this, "Invalid Username or Password");
    }

    dialog.cancel();
}

}

private class DataLoadToDBOperation extends AsyncTask < String, String, String > {
    private String user;
    private String pass;
    private ProgressDialog dialog;

    public DataLoadToDBOperation() {
        dialog = new ProgressDialog(LogInActivity.this);
        dialog.setCancelable(false);
    }

    @Override
    protected String doInBackground(String...params) {

        String errMsg = "";

        try {

            String result = "";

            DataLoader fileLoader = new DataLoader(LogInActivity.this, mDBHelper, user);

            List < String > importFileList = mSupporter.loadImportFileList();

            int totImpFile = importFileList.size();

            File salPer_folder_path = Supporter.getImportFolderPath(user);

            List < String > compList = mSupporter.getFolderNames(salPer_folder_path);

            int compSize = compList.size();

            if (compSize != 0) {

                startDBTransaction("db data loading"); // to start db
                // transaction

                for (int c = 0; c < compList.size(); c++) {

                    String serCompName = compList.get(c);

                    for (int i = 0; i < totImpFile; i++) {

                        String fileName = importFileList.get(i);

                        if ((c > 0) && (fileName.equals("FigoodsDetail"))) { // to
                            // load
                            // FigoodsDetail
                            // data
                            // once
                            continue; // to continue for other files
                        }

                        mImpOutputFile = Supporter.getImpOutputFilePathByCompany(spCode, serCompName,
                        fileName + ".xml");

                        if (mImpOutputFile.exists()) {
                            InputStream inputStream;

                            inputStream = new FileInputStream(mImpOutputFile);
                            String[] resultArray = fileLoader.parseDocument(inputStream);

                            result = resultArray[0];
                            errMsg = resultArray[1];

                            if (!result.equals("success")) {
                                mDBHelper.mEndTransaction();
                                break;
                            }

                        } else {
                            result = "File not available";
                            mDBHelper.mEndTransaction();
                            break;
                        }

                    }

                    if (!result.equals("success")) { // to break from
                        // executing other
                        // companies
                        break;
                    }

                }
                endDBTransaction(); // to end db transaction

            } else {
                result = "File not available";
            }

            return result;
        } catch (Exception exe) {
            exe.printStackTrace();
            String errorCode = "Err-CLS-2";
            LogFileCreator.appendLog(errorCode + " : " + exe.getMessage() + "\n" + errMsg);
            String result = "error";
            return result;
        }
    }

    /*
     * @Override protected void onPostExecute(final String result) { if (dialog
     * != null) { if (dialog.isShowing()) { dialog.dismiss(); } }
     * 
     * if (result.equals("success")) {
     * 
     * mCmpnyNameList = loadListOfCompany();
     * 
     * mAdptSpnCompany = new ArrayAdapter<String>(LogInActivity.this,
     * android.R.layout.simple_dropdown_item_1line, mCmpnyNameList);
     * 
     * mSpnCompany.setAdapter(mAdptSpnCompany);
     * 
     * // to delete file after loaded to db mSupporter.deleteFileFolder(user);
     * 
     * mSupporter.navigateTo(MainMenu.class);
     * 
     * } else if (result.equals("nosd")) {
     * mToastMsg.showToast(LogInActivity.this, "Sd card required"); } else if
     * (result.equals("parsing error")) { mToastMsg
     * .showToast(LogInActivity.this, "Error during parsing the data"); } else
     * if (result.equals("File not available")) {
     * mToastMsg.showToast(LogInActivity.this, "File not available"); } else {
     * mToastMsg.showToast(LogInActivity.this, "Errorrrrrrrrr"); }
     * 
     * }
     */

    @Override
    protected void onPreExecute() {
        this.dialog.setMessage("Please wait until the data is loaded");
        this.dialog.show();
    }

    @Override
    protected void onProgressUpdate(String...values) {}
}

1 个答案:

答案 0 :(得分:2)

new DataLoadToDBOperation().execute();

是问题所在。 AsyncTask使用内部处理程序在ui线程上调用它的一些回调,并且这个处理程序必须在ui线程上实例化。从doInBackground

中删除该来电