1

我有一个带有活动的主类扩展,并且在其中定义了一个内部类,它通过 asynctask 扩展但在内部类中我使用了 TextView.setText("Hi) 但它没有显示。AnyOne 可以建议我吗?

public class MainActivity extends Activity
{
TextView tv;
String s;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); 

    mytask m=new mytask(s);
    //Log.d("String1",s);
    //tv.setText("Hi");
    m.execute();
}
public class mytask extends AsyncTask<String, Void, String>
{
    private static final String SOAP_ACTION = "http://tempuri.org/HelloWorld";
    private static final String NAMESPACE = "http://tempuri.org/";
    private static final String METHOD_NAME = "HelloWorld";
    private static final String URL =    "http://10.0.2.2:1070/HelloWorld/WebService.asmx?wsdl";

    String k;

    public mytask(String s) 
    {
        k=s;
    }

//  @SuppressWarnings("unused")
    protected String doInBackground(String... params)
    {
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

        tv=(TextView)findViewById(R.id.text_view);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);
        Log.d("Envelope", envelope.toString());

        try 
        {
            Log.d("res", "entered");

            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

            //this is the actual part thnteredat will call the webservice
            androidHttpTransport.call(SOAP_ACTION, envelope);

            // Get the SoapResult from the envelope body.
            SoapObject result = (SoapObject)envelope.bodyIn;
            //responce=
            Log.d("res1", result.toString());
            if(result != null)
            {

                s=result.getPropertyAsString(0).toString();
                Log.d("string", s);
                tv.setText("Hi");
                //Get the first property and change the label text

            }
            else
            {
                Toast.makeText(getApplicationContext(), "No Response",Toast.LENGTH_LONG).show();
            }
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
        return null;
    }
    @Override
    protected void onPostExecute(String result) 
    {   
        super.onPostExecute(result);    
    }
    @Override
    protected void onPreExecute() 
    {
        super.onPreExecute();
    }
}
public boolean onCreateOptionsMenu(Menu menu)
{
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

4

2 回答 2

2

移动你的初始化onCreate

   tv=(TextView)findViewById(R.id.text_view);

您无法从doInBackground(). 将下面移动到onPostExecute(param).

   tv.setText("Hi"); 
   Toast.makeText(getApplicationContext(), "No Response",Toast.LENGTH_LONG).show();

http://developer.android.com/reference/android/os/AsyncTask.html

检查 4 个步骤部分下的链接和主题。

您可以在 中返回结果doInBackground()。计算结果doInBackground()是 的参数onPostExecute(param)。所以返回结果doInBackground()并更新 ui onPostExecute()

doInBackgrounnd()返回结果中并将返回类型更改为SoapObject. 将结果声明为类变量。

然后

@Override
protected void onPostExecute(SoapObject result) 
{   
    super.onPostExecute(result);
    if(result!=null)
    {
         tv.setText("Sucess...");  
         Toast.makeText(getApplicationContext(), "Sucess",Toast.LENGTH_LONG).show();    
    }        
}

您也可以使用runOnUiThread. 但我建议你在onPostExecute.

  runOnUiThread(new Runnable() //run on ui thread
  {
          public void run() 
                  { 

                 }
                 });
于 2013-06-24T06:20:53.947 回答
0

tv.setText("Hi");

略低于

tv=(TextView)findViewById(R.id.text_view);  and write this code in onCreate method
于 2013-06-24T06:19:27.260 回答