当我反复执行OnListItemClick时,为什么我在Android上得到StackOverflowError?

时间:2011-07-27 07:14:17

标签: android stack-overflow

我有一个列表视图。在单击列表视图时会调用一个intent。但是当我重复执行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

2 个答案:

答案 0 :(得分:2)

我认为您的Looper.loop()调用在错误的线程中运行 - 它位于onListItemClick()方法中,因此它在'foreground'线程的上下文中运行。因此onListItemClick()永远不会返回,后续调用它只会构建堆栈。

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

另外,我对使用Looper并不是很了解,但我看不出你要放弃Looper的位置。

答案 1 :(得分:0)

我得到了答案:

@Override
public void run() { 
try { 
// preparing a looper on current thread 
// the current thread is being detected implicitly 
Looper.prepare(); 
// now, the handler will automatically bind to the 
// Looper that is attached to the current thread 
// You don't need to specify the Looper explicitly 
handler = new Handler(); 
// After the following line the thread will start 
// running the message loop and will not normally 
// exit the loop unless a problem happens or you 
// quit() the looper (see below) 
Looper.loop(); 
} catch (Throwable t) { 
Log.e(TAG, "halted due to an error", t); 
} 
}