Android TextView未显示在内部类中
我有一个带有活动的主类extend,里面有一个内部类define,它是使用asynctask扩展的,但在内部类中我使用了TextView.setText(“Hi”),但它没有显示。有人能推荐我吗Android TextView未显示在内部类中,android,Android,我有一个带有活动的主类extend,里面有一个内部类define,它是使用asynctask扩展的,但在内部类中我使用了TextView.setText(“Hi”),但它没有显示。有人能推荐我吗 public class MainActivity extends Activity { TextView tv; String s; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(sa
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;
}
公共类MainActivity扩展活动
{
文本视图电视;
字符串s;
@凌驾
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mytask m=新的mytask;
//对数d(“弦1”,s);
//tv.setText(“Hi”);
m、 执行();
}
公共类mytask扩展了AsyncTask
{
私有静态最终字符串SOAP_ACTION=”http://tempuri.org/HelloWorld";
私有静态最终字符串命名空间=”http://tempuri.org/";
私有静态最终字符串方法\u NAME=“HelloWorld”;
私有静态最终字符串URL=”http://10.0.2.2:1070/HelloWorld/WebService.asmx?wsdl";
串k;
公共mytask(字符串s)
{
k=s;
}
//@SuppressWarnings(“未使用”)
受保护的字符串doInBackground(字符串…参数)
{
SoapObject请求=新的SoapObject(名称空间、方法名称);
tv=(TextView)findViewById(R.id.text\u视图);
SoapSerializationEnvelope=新的SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(请求);
Log.d(“信封”,信封.toString());
尝试
{
Log.d(“res”,“entered”);
HttpTransportSE androidHttpTransport=新的HttpTransportSE(URL);
//这是thnteredat将调用webservice的实际部分
调用(SOAP_操作,信封);
//从信封正文获取SoapResult。
SoapObject结果=(SoapObject)envelope.bodyIn;
//回应=
Log.d(“res1”,result.toString());
如果(结果!=null)
{
s=result.getPropertyAsString(0.toString();
Log.d(“字符串”,s);
tv.setText(“Hi”);
//获取第一个属性并更改标签文本
}
其他的
{
Toast.makeText(getApplicationContext(),“无响应”,Toast.LENGTH_LONG.show();
}
}
捕获(例外e)
{
e、 printStackTrace();
}
返回null;
}
@凌驾
受保护的void onPostExecute(字符串结果)
{
super.onPostExecute(结果);
}
@凌驾
受保护的void onPreExecute()
{
super.onPreExecute();
}
}
公共布尔onCreateOptions菜单(菜单)
{
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
}放
tv.setText("Hi");
就在下面
tv=(TextView)findViewById(R.id.text_view); and write this code in onCreate method
将您的初始化
onCreate
tv=(TextView)findViewById(R.id.text_view);
您无法从doInBackground()
更新ui。请将下面的命令移动到onPostExecute(param)
检查4个步骤部分下的链接和主题
您可以在doInBackground()
中返回结果。doInBackground()
计算的结果是onPostExecute(param)
的一个参数。因此,在doInBackground()
中返回结果,并在onPostExecute()
中更新ui
在doInBackgrounnd()
中返回结果,并将返回类型更改为SoapObject
。将结果声明为类变量
然后
您也可以使用runOnUiThread
。但我建议您在onPostExecute
中更新ui
runOnUiThread(new Runnable() //run on ui thread
{
public void run()
{
}
});
@KuldeepMishra欢迎您,请点击勾选一次来接受答案。@KuldeepMishra的答案有帮助。如果是,请接受答案。
@Override
protected void onPostExecute(SoapObject result)
{
super.onPostExecute(result);
if(result!=null)
{
tv.setText("Sucess...");
Toast.makeText(getApplicationContext(), "Sucess",Toast.LENGTH_LONG).show();
}
}
runOnUiThread(new Runnable() //run on ui thread
{
public void run()
{
}
});