AsyncTask适用于三星S3,但不适用于HTC Desire

时间:2012-08-29 11:18:22

标签: android

我有一个扩展AsyncTask的类:

package Logic;


public class ReadConfig extends AsyncTask<String, Integer, String> {

 ProgressBar progressBar;
 Context context;
 URL url;
 FileInputStream config;
 static CreateApplicationFolder folders;
 File file;
 DocumentBuilderFactory dbf;
 DocumentBuilder db;
 Document doc;

 SharedPreferences prefs;
 File configFile;

 public ReadConfig(Context context, ProgressBar progressBar)
        throws ParserConfigurationException, SAXException, IOException {

    this.context = context;
    this.progressBar = progressBar;

    folders = new CreateApplicationFolder(context);
    dbf = DocumentBuilderFactory.newInstance();
    db = dbf.newDocumentBuilder();
    configFile = new File(folders.getPathToNode() + "/config.xml");

    prefs = PreferenceManager.getDefaultSharedPreferences(context);
}

protected void onPreExecute() {

    progressBar.incrementProgressBy(1);

}

@Override
protected String doInBackground(String... params) {
    android.os.Debug.waitForDebugger();
    int i;

    if (!configFile.exists()) {
        sleep();
        return "noConfig";
    } else {
        try {
            doc = db.parse(configFile);
        } catch (SAXException e2) {
            Log.e("SAX: ", e2.getMessage());
        } catch (IOException e2) {
            Log.e("IO: ", e2.getMessage());
        }
        doc.getDocumentElement().normalize();

        Log.i("ROOT NODE: ", doc.getDocumentElement().getNodeName());

        NodeList listOfMail = doc.getElementsByTagName("mail");

        int totalMail = listOfMail.getLength();
        Log.i("LENGTH: ", Integer.toString(totalMail));

        for (i = 0; i < totalMail; i++) {

            Node firstMailSetting = listOfMail.item(i);
            Element e = (Element) firstMailSetting;

            if (i == 0) {

                putSharedPrefs("host", getTagValue("host", e));
                putSharedPrefs("mail", getTagValue("account", e));
                putSharedPrefs("port", getTagValue("port", e));
                putSharedPrefs("pw", getTagValue("password", e));
            } else if (i == 1) {
                putSharedPrefs("supportMail", getTagValue("account", e));
            } else if (i == 2) {
                putSharedPrefs("Tomail", getTagValue("account", e));
            }
            sleep();

        }
    }

    return "";
}

protected void onPostExecute(String result) {

    if (result.equals("noConfig")) {
        Toast.makeText(context, "No Config file was found..",
                Toast.LENGTH_LONG).show();
        Intent mainIntent = new Intent(context, MAIN.class);
        context.startActivity(mainIntent);
    } else {
        Intent mainIntent = new Intent(context, MAIN.class);
        context.startActivity(mainIntent);
    }
}

private String getTagValue(String sTag, Element eElement) {
    try {
        NodeList nlList = eElement.getElementsByTagName(sTag).item(0)
                .getChildNodes();
        Node nValue = (Node) nlList.item(0);
        return nValue.getNodeValue();
    } catch (Exception e) {
        return "";
    }

}

public void putSharedPrefs(String tag, String value) {
    SharedPreferences.Editor prefEditor = prefs.edit();
    prefEditor.putString(tag, value);
    prefEditor.commit();
}

public void sleep() {
    for (int j = 0; j < 1; j++) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
    }
}

}

以下代码行中的“问题”服务员:

if (!configFile.exists()) {
        sleep();
        return "noConfig";

这会向onPostExecute方法返回“noConfig”,该方法应显示Toast

protected void onPostExecute(String result) {

    if (result.equals("noConfig")) {
        Toast.makeText(context, "No Config file was found..",
                Toast.LENGTH_LONG).show();
        Intent mainIntent = new Intent(context, MAIN.class);
        context.startActivity(mainIntent);
    } else {
        Intent mainIntent = new Intent(context, MAIN.class);
        context.startActivity(mainIntent);
    }
}

此代码适用于三星S3,但当我在HTC上测试它时,屏幕变黑并且Toast只是一直显示而不启动新的Activity。没有错误,也没有例外。有任何想法吗?

修改

从我的启动画面调用AsyncTask:

try {
        new ReadConfig(Splash.this, pd).execute("");
    } catch (ParserConfigurationException e) {
        Log.e("Parser: ", e.getMessage());
    } catch (SAXException e) {
        Log.e("Sax: ", e.getMessage());
    } catch (IOException e) {
        Log.e("IO: ", e.getMessage()); 
    }

pd对象是我ProgressDialog的{​​{1}},下面是基本流程:

Splashscreen init。 xml文件中的一些设置,在Layout方法中解析。如果配置文件不存在,只需向其发送消息并启动主活动。

2 个答案:

答案 0 :(得分:0)

在你的行

new Intent(context, this.class);

this.class表示你的ReadConfig类一个Activity,这很奇怪。你是否从你的活动中得到了这个代码(它可能是一个内部类?)

您应该尝试将this.class更改为WhateverActivity.class

答案 1 :(得分:0)

鉴于问题与手机有关,手机制造商倾向于改变/修补奇怪和草皮我会看:

尝试删除: android.os.Debug.waitForDebugger();

另外,你需要睡眠()

专注于您未启动的活动的具体问题,我可能会尝试

Intent mainIntent = new Intent(context.getApplicationContext(), MAIN.class);
context.getApplicationContext().startActivity(mainIntent);

删除任何关于如何处理启动意图的歧义。您也可以在Activity中调用一个方法来处理这个问题,而不是从异步任务中调用。