2013-06-24 60 views
1

我有一個主要的類擴展與活動和內部它一個內部類是定義它是與asynctask擴展,但在內部類我使用TextView.setText(「嗨),但它不顯示.AnyOne可以建議我嗎?TextView不會顯示在內部類

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

移動你的初始化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()並更新UIi 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更新ui。

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

       } 
       }); 
+0

感謝u.onPostExecute工作.. –

+0

@KuldeepMishra歡迎您請通過點擊打勾once.http接受了答案://meta.stackexchange.com/questions/5234/how-does-accepting - 回答工作 – Raghunandan

+0

@KuldeepMishra會幫你解答。如果是的話,接受答案。 – Raghunandan

0

tv.setText("Hi"); 

略低於

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