soapPrimitiveData()出错无法在未调用Looper.prepare()的线程内创建处理程序

时间:2013-11-12 11:50:58

标签: android android-asynctask handler runtimeexception looper

public class MainActivity extends Activity {

private EditText username;
private EditText pass;
private final String NAMESPACE = "http://tempuri.org/";
private final String URL = "http://122.252.200.98/fts/registration.asmx";

String user_id;
String password;
String tran="2";
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button signin = (Button) findViewById(R.id.button1);
    signin.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {


            username = (EditText) findViewById(R.id.editText1);
            user_id = username.getText().toString();

            pass = (EditText)  findViewById(R.id.editText2);
            password = pass.getText().toString();



            new LoginTask().execute();

        }
    });   
}

private boolean doLogin(String user_id1, String password1, String transactiontype)    {   

    boolean result=false;
    final String SOAP_ACTION = "http://tempuri.org/getUser";
    final String METHOD_NAME = "getUser";     
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

    PropertyInfo username = new PropertyInfo();
    PropertyInfo passw=new PropertyInfo();
    PropertyInfo trans=new PropertyInfo();

    username.setValue(user_id);
    username.setType(String.class);
    username.setName("custname");
    request.addProperty(username);

    passw.setValue(password);
    passw.setType(String.class);
    passw.setName("pwd");
    request.addProperty(passw);

    trans.setValue(tran);
    trans.setType(String.class);
    trans.setName("signtype");
    request.addProperty(trans);

    SoapSerializationEnvelope envelope = new   SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true; 

    envelope.setOutputSoapObject(request);

    System.out.println(request);

    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);


    try {
        androidHttpTransport.call(SOAP_ACTION, envelope);
        //SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
        Object response= (Object)envelope.getResponse();
        Log.i("reply", response.toString());
        System.out.println("response" +response);

        if(response.toString().equalsIgnoreCase("Success-User Exists"))
        {
            result = true;
            System.out.println(result);
            Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT).show();
        }
        else if(response.toString().equalsIgnoreCase("Username does not Exists"))
        {
            result=false;
            Toast.makeText(getApplicationContext(), "username incorrect", Toast.LENGTH_SHORT).show();
        }
        else 
        {
            Toast.makeText(getApplicationContext(), "passsword incorrect", Toast.LENGTH_SHORT).show();
        }

    }catch(SocketException ex)
    {
        Log.e("Error : " , "Error on soapPrimitiveData() " + ex.getMessage());
        ex.printStackTrace();
    }
    catch (Exception e) {
        Log.e("Error : " , "Error on soapPrimitiveData() " + e.getMessage());
        e.printStackTrace();
    }
    return result;

}


private class LoginTask extends AsyncTask<Void, Void, Void> {

    private final ProgressDialog dialog = new ProgressDialog(
            MainActivity.this);

    protected void onPreExecute() {

        this.dialog.setMessage("Logging in...");
        this.dialog.show();

    }


    protected Void doInBackground(Void... unused) {

        boolean auth=doLogin(user_id,password,tran);
        System.out.println(auth);
        Looper.prepare();
        return null;// don't interact with the ui!
    }


    protected void onPostExecute(Void result) {


        if (this.dialog.isShowing()) {
            this.dialog.dismiss();
        }         
    }

} 
} 

记录cat错误:

11-12 17:10:41.943: E/Error :(19833): Error on soapPrimitiveData() Can't create handler  inside thread that has not called Looper.prepare()
11-12 17:10:41.944: W/System.err(19833): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
11-12 17:10:41.952: W/System.err(19833):    at android.os.Handler.<init>(Handler.java:152)
11-12 17:10:41.954: W/System.err(19833):    at android.widget.Toast.<init>(Toast.java:99)
11-12 17:10:41.957: W/System.err(19833):    at android.widget.Toast.makeText(Toast.java:262)
11-12 17:10:41.962: W/System.err(19833):    at com.example.logincheck.MainActivity.doLogin(MainActivity.java:113)
11-12 17:10:41.965: W/System.err(19833):    at com.example.logincheck.MainActivity.access$0(MainActivity.java:57)
11-12 17:10:41.967: W/System.err(19833):    at com.example.logincheck.MainActivity$LoginTask.doInBackground(MainActivity.java:145)
11-12 17:10:41.970: W/System.err(19833):    at com.example.logincheck.MainActivity$LoginTask.doInBackground(MainActivity.java:1)
11-12 17:10:41.973: W/System.err(19833):    at android.os.AsyncTask$2.call(AsyncTask.java:216)
11-12 17:10:41.975: W/System.err(19833):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:337)
11-12 17:10:41.978: W/System.err(19833):    at java.util.concurrent.FutureTask.run(FutureTask.java:169)
11-12 17:10:41.981: W/System.err(19833):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1119)
11-12 17:10:41.983: W/System.err(19833):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:612)
11-12 17:10:41.986: W/System.err(19833):    at java.lang.Thread.run(Thread.java:1052)

这个问题已经问了好几次。但它对我的问题没有帮助。 调试时我得到了上述错误。我不知道我的代码中的错误是什么。我浏览了这个链接link。但我仍然没有弄到问题所在。

2 个答案:

答案 0 :(得分:1)

我认为在AsyncTask的doInBackground方法中显示toast的问题。您需要将与UI相关的内容移至onPostExecute

您正在doLogin方法中显示Toast,该方法在AsyncTask的doInBackground方法中调用。你需要删除它。

答案 1 :(得分:0)

查看错误:

  android.widget.Toast.<init>(Toast.java:99)
  at android.widget.Toast.makeText(Toast.java:262)

它说你的错误是显示祝酒词。 您只能在UI线程上显示toasts。

     protected Void doInBackground(Void... unused) {

    boolean auth=doLogin(user_id,password,tran);
    System.out.println(auth);
    Looper.prepare();
    return null;// don't interact with the ui!
}

以上是在后台线程上发生的,doLogin()正在使用toasts。

请参阅this回答,了解如何从异步任务调用Toast。

你也可以调用Activity.runOnUiThread(Runnable r)并将你的toast调用放在其中。

要制作此DRY代码,您可以创建一个名为showToast(String text)的方法,如下所示:

    public void showToast(String text) {
         try {
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        Toast.makeText(MainAcivity.this, text, Toast.LENGTH_LONG).show();
                    }
                });
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    }