我有一个列表视图。在单击列表视图时会调用一个意图。。但是当我反复执行OnListItemClick时,会引发StackOverflowError

我的代码是:

protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);

    Cursor cursor = (Cursor) lAdapter.getItem(position);
    final String lpn = cursor.getString(cursor
        .getColumnIndex(SQLHelper.EMPLOYEE_LPN));
    System.out.println("EMPLOYEE_LPN :   " + lpn);
    final Context con = this;

    final ProgressDialog empDialog = ProgressDialog.show(this, "", "Loading Employee Details ....");
    empDialog.setIcon(R.drawable.icon64);
    empDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    empDialog.show();

    Thread mBackground = new Thread() {

        @Override
        public void run() {


        try{
         // preparing a looper on current thread  the current thread is being detected implicitly
                Looper.prepare();
                DirectoryApp appState = (DirectoryApp) getApplicationContext();
            Object[][] requestArray = new Object[][] {{"lpn",lpn}};
            SOAPHelper soapHelper = new SOAPHelper();
            String result = soapHelper.getHttpTransportResponse(
                appState.getNAMESPACE_DIRECTORY(), "",
                appState.getMETHOD_NAME_GET_EMPLOYEE_DETAILS(), appState.getURL_EYDIRECTORY_SERVICE(), requestArray);

            if(result.equals("")){
                Toast.makeText(ListContacts.this, getString(R.string.connErr),
                    Toast.LENGTH_LONG).show();
            }
            else{
                    Intent intentEmp = new Intent(con,
                    EmployeeDetails.class);
                intentEmp.putExtra("result",
                    result);
                startActivity(intentEmp);

            }
            empDialog.dismiss();
            handler.sendEmptyMessage(1);

        }
        catch (Exception e) {
            empDialog.dismiss();
            Bundle bundle = new Bundle();
            bundle.putInt("flag", 0);
            Message message = new Message();
            message.setData(bundle);
            handler.sendMessage(message);
        }

        }

    };

    // start the background thread
    mBackground.start();
    Looper.loop();


    }


    private Handler handler = new Handler() {

    @Override
    public void handleMessage(Message msg) {

        super.handleMessage(msg);

        int flag = msg.getData().getInt("flag");
        switch (flag) {
        case 0:
            Toast.makeText(ListContacts.this, getString(R.string.connErr),
                Toast.LENGTH_LONG).show();
            break;
        case 1:
            break;
        default:
            Toast.makeText(ListContacts.this, getString(R.string.connErr),
                Toast.LENGTH_LONG).show();
        }

    }
};


唯一的例外是:

07-27 12:39:38.566: ERROR/AndroidRuntime(6694): FATAL EXCEPTION: main
07-27 12:39:38.566: ERROR/AndroidRuntime(6694): java.lang.StackOverflowError
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.view.View.draw(View.java:6739)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.view.ViewGroup.drawChild(ViewGroup.java:1648)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.widget.AbsListView.dispatchDraw(AbsListView.java:1365)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.widget.ListView.dispatchDraw(ListView.java:3046)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.view.View.draw(View.java:6845)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.widget.AbsListView.draw(AbsListView.java:2257)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.view.ViewGroup.drawChild(ViewGroup.java:1648)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.view.View.draw(View.java:6742)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.view.ViewGroup.drawChild(ViewGroup.java:1648)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.view.View.draw(View.java:6742)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.view.ViewGroup.drawChild(ViewGroup.java:1648)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.view.View.draw(View.java:6742)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.view.ViewGroup.drawChild(ViewGroup.java:1648)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.view.View.draw(View.java:6742)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1872)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.view.ViewRoot.draw(ViewRoot.java:1449)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1194)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1771)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.os.Looper.loop(Looper.java:144)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at org.sipdroid.sipua.ui.ListContacts.onListItemClick(ListContacts.java:381)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.app.ListActivity$2.onItemClick(ListActivity.java:345)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.widget.AdapterView.performItemClick(AdapterView.java:284)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.widget.ListView.performItemClick(ListView.java:3382)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.os.Handler.handleCallback(Handler.java:587)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.os.Looper.loop(Looper.java:144)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at org.sipdroid.sipua.ui.ListContacts.onListItemClick(ListContacts.java:381)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.app.ListActivity$2.onItemClick(ListActivity.java:345)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.widget.AdapterView.performItemClick(AdapterView.java:284)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.widget.ListView.performItemClick(ListView.java:3382)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.os.Handler.handleCallback(Handler.java:587)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.os.Looper.loop(Looper.java:144)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at org.sipdroid.sipua.ui.ListContacts.onListItemClick(ListContacts.java:381)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.app.ListActivity$2.onItemClick(ListActivity.java:345)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.widget.AdapterView.performItemClick(AdapterView.java:284)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.widget.ListView.performItemClick(ListView.java:3382)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.os.Handler.handleCallback(Handler.java:587)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at android.os.Looper.loop(Looper.java:144)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at org.sipdroid.sipua.ui.ListContacts.onListItemClick(ListContacts.java:381)
07-27 12:39:38.566: ERROR/AndroidRuntime(6694):     at

最佳答案

我认为您的Looper.loop()调用在错误的线程中运行-它在onListItemClick()方法中,因此它在“前景”线程的上下文中运行。因此,onListItemClick()永不返回,随后对其的调用只会建立堆栈。

我认为它应该在run()子句之前的mBackground线程的catch方法中。

另外,我对使用Looper的了解不多,但是我看不到要退出该Looper的地方。

关于android - 重复执行OnListItemClick时,为什么会在Android上收到StackOverflowError?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6840701/

10-09 00:40