TextView不在内部类中显示

时间:2013-06-24 06:15:18

标签: android

我有一个带有活动的主类扩展,里面有一个内部类是定义,它是用asynctask扩展但在内部类我使用TextView.setText(“你好)但它没有显示.AnyOne可以建议我吗?” p>

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;
}

}

2 个答案:

答案 0 :(得分:2)

移动初始化onCreate

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

您无法从doInBackground()更新ui。将下面的内容移至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()中返回结果,并在onPostExecute()中更新ui。

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更新ui。

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

                 }
                 });

答案 1 :(得分:0)

tv.setText("Hi");

正好在

之下
tv=(TextView)findViewById(R.id.text_view);  and write this code in onCreate method