Asynctask,传递变量和Intents

时间:2012-04-23 16:35:13

标签: android

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

        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();


}
      }}

04-23 17:45:16.752:E / AndroidRuntime(2425):致命异常:AsyncTask#1  04-23 17:45:16.752:E / AndroidRuntime(2425):java.lang.RuntimeException:执行doInBackground()时发生错误 04-23 17:45:16.752:E / AndroidRuntime(2425):在android.os.AsyncTask $ 3.done(AsyncTask.java:200) 04-23 17:45:16.752:E / AndroidRuntime(2425):at java.util.concurrent.FutureTask $ Sync.innerSetException(FutureTask.java:274) 04-23 17:45:16.752:E / AndroidRuntime(2425):at java.util.concurrent.FutureTask.setException(FutureTask.java:125) 04-23 17:45:16.752:E / AndroidRuntime(2425):at java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:308) 04-23 17:45:16.752:E / AndroidRuntime(2425):at java.util.concurrent.FutureTask.run(FutureTask.java:138) 04-23 17:45:16.752:E / AndroidRuntime(2425):at java.util.concurrent.ThreadPoolExecutor.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.focusableViewAvailable(ViewGroup.java:452) 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.View.setFlags(View.java:4631) 04-23 17:45:16.752:E / AndroidRuntime(2425):在android.view.View.setFocusableInTouchMode(View.java:3206) 04-23 17:45:16.752:E / AndroidRuntime(2425):在android.widget.AdapterView.checkFocus(AdapterView.java:694) 04-23 17:45:16.752:E / AndroidRuntime(2425):在android.widget.AdapterView $ AdapterDataSetObserver.onChanged(AdapterView.java:789) 04-23 17:45:16.752:E / AndroidRuntime(2425):在android.database.DataSetObservable.notifyChanged(DataSetObservable.java:31) 04-23 17:45:16.752:E / AndroidRuntime(2425):在android.widget.BaseAdapter.notifyDataSetChanged(BaseAdapter.java:50) 04-23 17:45:16.752:E / AndroidRuntime(2425):在android.widget.ArrayAdapter.notifyDataSetChanged(ArrayAdapter.java:247) 04-23 17:45:16.752:E / AndroidRuntime(2425):at com.library_app.DissertationActivity $ LoginProgressTask.doInBackground(DissertationActivity.java:103) 04-23 17:45:16.752:E / AndroidRuntime(2425):at com.library_app.DissertationActivity $ LoginProgressTask.doInBackground(DissertationActivity.java:1) 04-23 17:45:16.752:E / AndroidRuntime(2425):在android.os.AsyncTask $ 2.call(AsyncTask.java:185) 04-23 17:45:16.752:E / AndroidRuntime(2425):at java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:306) 04-23 17:45:16.752:E / AndroidRuntime(2425):...还有4个 04-23 17:45:16.902:E / WindowManager(2425):活动com.library_app.DissertationActivity泄露了最初添加的窗口com.android.internal.policy.impl.PhoneWindow$DecorView@40666e70 04-23 17:45:16.902:E / WindowManager(2425):android.view.WindowLeaked:Activity com.library_app.DissertationActivity泄露了最初添加的窗口com.android.internal.policy.impl.PhoneWindow$DecorView@40666e70这里 04-23 17:45:16.902:E / WindowManager(2425):在android.view.ViewRoot。(ViewRoot.java:259) 04-23 17:45:16.902:E / WindowManager(2425):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148) 04-23 17:45:16.902:E / WindowManager(2425):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) 04-23 17:45:16.902:E / WindowManager(2425):在android.view.Window $ LocalWindowManager.addView(Window.java:465) 04-23 17:45:16.902:E / WindowManager(2425):在android.app.Dialog.show(Dialog.java:241) 04-23 17:45:16.902:E / WindowManager(2425):at com.library_app.DissertationActivity $ LoginProgressTask.onPreExecute(DissertationActivity.java:59) 04-23 17:45:16.902:E / WindowManager(2425):在android.os.AsyncTask.execute(AsyncTask.java:391) 04-23 17:45:16.902:E / WindowManager(2425):at com.library_app.DissertationActivity.onCreate(DissertationActivity.java:49) 04-23 17:45:16.902:E / WindowManager(2425):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 04-23 17:45:16.902:E / WindowManager(2425):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722) 04-23 17:45:16.902:E / WindowManager(2425):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784) 04-23 17:45:16.902:E / WindowManager(2425):在android.app.ActivityThread.access $ 1500(ActivityThread.java:123) 04-23 17:45:16.902:E / WindowManager(2425):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:939) 04-23 17:45:16.902:E / WindowManager(2425):在android.os.Handler.dispatchMessage(Handler.java:99) 04-23 17:45:16.902:E / WindowManager(2425):在android.os.Looper.loop(Looper.java:130) 04-23 17:45:16.902:E / WindowManager(2425):在android.app.ActivityThread.main(ActivityThread.java:3835) 04-23 17:45:16.902:E / WindowManager(2425):at java.lang.reflect.Method.invokeNative(Native Method) 04-23 17:45:16.902:E / WindowManager(2425):at java.lang.reflect.Method.invoke(Method.java:507) 04-23 17:45:16.902:E / WindowManager(2425):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:847) 04-23 17:45:16.902:E / WindowManager(2425):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605) 04-23 17:45:16.902:E / WindowManager(2425):at dalvik.system.NativeStart.main(Native Method) [抱歉不知道如何缩进]

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

代码有什么问题,需要更改什么才能使它不再崩溃?

2 个答案:

答案 0 :(得分: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
}
}

答案 1 :(得分:0)

我的猜测是,你可能会因为行

而看到那些崩溃
adapter.notifyDataSetChanged();

尝试删除它们。还有一种叫做

的方法
protected void onProgressUpdate(Integer... progress)

您可能希望使用此方法与主UI线程进行通信,以及您所做的任何进度。不确定这是否有帮助。但是,如果您必须在doInBackground中进行更新,则可能应使用runOnUiThread