0

我正在尝试在两个意图之间创建一个进度对话框。这是代码:

        public class DissertationActivity extends ListActivity {
/** Called when the activity is first created. */

public ArrayList<String> book_Array = new ArrayList<String>();
ArrayAdapter<String> adapter;

String href = "";
String href1 = "";
String search_Word = "";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bundle extras = getIntent().getExtras();
    search_Word = extras.getString("query1");

    adapter = new ArrayAdapter<String>(this, R.layout.list_item_1,
            book_Array);
    setListAdapter(adapter);

    new LoginProgressTask().execute();


}

class LoginProgressTask extends AsyncTask<String, Integer, String> {

    ProgressDialog pDialog = new ProgressDialog(DissertationActivity.this);


    protected void onPreExecute() {
        this.pDialog.setMessage("Progress start");
        this.pDialog.show();


    }


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

        ListView lv = getListView();
        lv.setTextFilterEnabled(true);

        try {
            Document doc = null;
            Document guestLink = null;

            guestLink = Jsoup.connect("https://aulib.abdn.ac.uk:443/F")
                    .get();
            Element link = guestLink.select("p > a").first();
            href1 = link.attr("href");
            href = href1.substring(0, href1.length() - 2); // removes -0
                                                            // from
                                                            // the

            // href_Array.add(href); //adds href to the array because string
            // wont add to the public var.
            doc = Jsoup.connect(
                    href + "&request=" + search_Word
                            + "&find_code=WRD&adjacent=N&x=0&y=0").get();
            // System.out.println(doc);
            Elements headings = doc.select("td:eq(3)");
            // System.out.println(headings);
            for (Element heading : headings) {
                // System.out.println(heading.text());
                String j = heading.text();

                book_Array.add(j);

            }

        } catch (IOException e) {
            e.printStackTrace();

        }

        book_Array.remove(0);
        adapter.notifyDataSetChanged();
        book_Array.remove(1);
        adapter.notifyDataSetChanged();
        book_Array.remove(2);
        adapter.notifyDataSetChanged();
        book_Array.remove("Search");
        adapter.notifyDataSetChanged();
        book_Array.remove(" | ");
        adapter.notifyDataSetChanged();
        book_Array.remove(0);
        adapter.notifyDataSetChanged();

        lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> a, View v, int position,
                    long id) {
                // Context context = getApplicationContext();
                int query = position;
                // String text = book_Array.get(position);
                // int duration = Toast.LENGTH_SHORT;
                // Toast toast = Toast.makeText(context,
                // String.valueOf(position), //shows the postion in the
                // array
                // list
                // duration);
                // toast.show();

                Intent intent = new Intent(DissertationActivity.this,
                FullDetailsActivity.class);
        intent.putExtra("href", href);
        intent.putExtra("query1", (int) query);
        intent.putExtra("search_Word", search_Word);

        startActivity(intent);
            }

        });
        return null;
    }


    protected void onPostExecute(String...params) {

        pDialog.dismiss();


}
      }}

runWorker(ThreadPoolExecutor.java:1088) 04-23 17:45:16.752: E/AndroidRuntime(2425): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) 04-23 17:45: 16.752: E/AndroidRuntime(2425): at java.lang.Thread.run(Thread.java:1019) 04-23 17:45:16.752: E/AndroidRuntime(2425): 引起:android.view.ViewRoot$CalledFromWrongThreadException :只有创建视图层次结构的原始线程才能接触其视图。04-23 17:45:16.752: E/AndroidRuntime(2425): 在 android.view.ViewRoot.checkThread(ViewRoot.java:2941) 04-23 17:45:16.752: E/AndroidRuntime(2425): 在 android. view.ViewRoot.focusableViewAvailable(ViewRoot.java:1717) 04-23 17:45:16.752: E/AndroidRuntime(2425): 在 android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:452) 04-23 17:45: 16.752:E/AndroidRuntime(2425):在 android.view.ViewGroup。

进度条设法加载,但代码在几秒钟后崩溃。意图应该在哪里,在 doInBackground 中还是在 postExecute 中?如果意图是在 postExecte 中,我将如何将变量从 doInBackground 传递到 postExecute?

代码有什么问题以及需要更改什么以使其不再崩溃?

4

2 回答 2

1

你可以UI在更新doInBackGround()

看到您的日志跟踪,我可以告诉您您正在尝试更新 doInBackGround 中的 UI,您无法直接执行此操作,而是调用 runOnUiThread 方法,一切都会好起来的......使用

YourActivity.this.runOnUiThread(new Runnable() {                                            
public void run() {
 ListView lv = getListView();
 lv.setTextFilterEnabled(true);
 // do all UI related stuff in this
}
}
于 2012-04-23T16:51:09.240 回答
0

我的猜测是,您可能会因为线路而看到那些崩溃

adapter.notifyDataSetChanged();

尝试删除那些。还有一种方法叫做

protected void onProgressUpdate(Integer... progress)

您可能希望使用此方法与主 UI 线程进行通信,了解您取得的任何进展。不确定这是否会有所帮助。但是,如果您必须进行更新,doInBackground您可能应该使用runOnUiThread

于 2012-04-23T17:08:33.287 回答