gpt4 book ai didi

android - 内部类不显示TextView

转载 作者:行者123 更新时间:2023-11-29 21:40:27 25 4
gpt4 key购买 nike

我有一个扩展了 Activity 的主类,在它里面定义了一个内部类,它是用异步任务扩展的,但是在内部类中我使用了 TextView.setText("Hi) 但它没有显示。任何人都可以建议我吗?

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

最佳答案

移动你的初始化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()
{

}
});

关于android - 内部类不显示TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17269049/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com