0

我有一个扩展 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";

这将向方法返回“noConfig”,该onPostExecute方法应显示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来自 my ,下面Layout是基本流程:

启动画面初始化。doInBackground在方法中解析的 xml 文件中的一些设置。如果配置文件不存在,只需发送一条消息并启动 Main Activity。

4

2 回答 2

0

在你的行

new Intent(context, this.class);

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

您应该尝试更改this.classWhateverActivity.class

于 2012-08-29T11:26:58.903 回答
0

鉴于问题与手机有关,而手机制造商倾向于改变/修补奇怪的东西,我会看看:

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

另外,你需要 sleep()

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

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

消除关于它应该如何处理启动意图的任何歧义。您也可以从 Activity 中调用方法来处理此问题,而不是从 Async 任务中调用。

于 2012-08-29T11:47:14.097 回答