0

以下是仅在我尝试调用方法 soapPHP() 时才会产生异常的代码,否则它会完美运行。经过多次尝试解决这个问题,我现在筋疲力尽。你们的任何帮助都将不胜感激。

private ProgressDialog pd;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    ThemeUtils.sTheme = ThemeUtils.THEME_BLUE;

    ThemeUtils.onActivityCreateSetTheme(MainMenuActivity.this);

    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.main_menu_screen);

    asynThread = new AsynThread();

    handler = new Handler() 
    {
        @Override
        public void dispatchMessage(Message msg) 
        {
            super.dispatchMessage(msg);
            dispLayMsg(globalMessage);
        }
    };

    asynThread.execute(GET_MAIN_MENU);

}

--

public Object soapPHP(String METHOD_NAME, String SOAP_ACTION, String NAMESPACE, String URL)  
{
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
    request.addProperty("yourName", "Mohsin"); 

    SoapSerializationEnvelope envelope = 
    new SoapSerializationEnvelope(SoapEnvelope.VER11);

    envelope.setOutputSoapObject(request);
    AndroidHttpTransport httpTransport = new AndroidHttpTransport(URL);
    httpTransport.debug = true;
    Object response = null;
    try {

        httpTransport.call(SOAP_ACTION, envelope);
        response = envelope.getResponse();

    } 
    catch (Exception e) 
    {
    }

    return response;
}

--

public class AsynThread extends AsyncTask<String, Void, String> 
{
    private String inputParam = "";

    @Override
    protected void onPreExecute() 
    {
        // TODO Auto-generated method stub
        super.onPreExecute();
        pd = ProgressDialog.show(MainMenuActivity.this, "Attention..",
            "Please wait");
    }

    @Override
    protected String doInBackground(String... params) 
    {
        // TODO Auto-generated method stub
        inputParam = params[0];
        if (inputParam.equalsIgnoreCase(GET_MAIN_MENU)) 
        {
            menu_adapter = new MenuListAdapter();
            MenuListItem item = new MenuListItem();

            item.setMenu_name("Menu");

            item.setMenu_desc("Menu Description");

            menu_adapter.add(item);

            Object obj = soapPHP(METHOD_NAME, SOAP_ACTION, NAMESPACE, URL);

            menu_adapter.add(item);

            menu_list = (ListView) findViewById(R.id.main_menu_list);

            menu_list.setAdapter(menu_adapter);
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) 
    {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        pd.dismiss();
        /*
        * if (inputParam.equalsIgnoreCase(UPLOAD)) { // uploadData(); }
        */
    }

}

--

LogCat 中的异常:

07-30 17:51:10.882: E/WindowManager(5916): android.view.WindowLeaked: Activity com.tgu.sample.onlinemenu.ui.MainMenuActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4052db78 that was originally added here
4

1 回答 1

0

首先,这是因为您尝试在 UiThread 上运行 ProgressDialog 并且它必须在不同的线程上运行。

你可以试试这样使用

public class AsyncTaskHttpGet extends AsyncTask<String, Integer, String> {
private IHttpCallbackHandler _dataListener;
private ProgressDialog _dialog;
private Context mContext;

public AsyncTaskHttpGet(IHttpCallbackHandler dataListener, ProgressDialog dialog, Context context) {
    _dataListener = dataListener;
    _dialog = dialog;
    mContext = context;
}

@Override
protected String doInBackground(String... urls) {
    //todo: request via GET to certain URL
    return HttpClientWrapper.HttpGet(urls[0]);
}

@Override
protected void onPostExecute(String result) {
    if ( _dialog != null ) {
        if (_dialog.isShowing()) {
            _dialog.dismiss();
        }
    }
    _dataListener.OnHttpRequestDoneLoad(result); //gets data oncomplete
}

@Override
protected void onProgressUpdate(Integer... progress) {
}

@Override
protected void onPreExecute() {
    if ( _dialog != null ) {
        _dialog.setMessage(mContext.getString(R.string.load_data));
        _dialog.show();
    }
}

public interface IHttpCallbackHandler {
    void OnHttpRequestDoneLoad(String result);
}

}

于 2012-07-31T10:02:48.950 回答