![](/img/trans.png)
[英]Android How to programatically determine which XML layout my app is using?
[英]Layout an Xml to my android App
早上好 ! 所以我有这个
MainActivity.java
import......
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getXmlTask task = new getXmlTask(textview1 , "http://www.3pi.tf/test.xml");
task.execute();
}
}
还有我的getXmlTask.java
import....etc
public class getXmlTask extends AsyncTask<Void, Void, String>{
private static final String TAG2 = null;
private WeakReference<TextView> textViewReference;
private String url;
public void GetXmlTask(TextView textView, String url) {
this.textViewReference = new WeakReference<TextView>(textView);
this.url = url;
}
@Override
protected String doInBackground(Void... params) {
HttpClient hc = new DefaultHttpClient();
Log.v(TAG2, "testnew");
HttpPost post = new HttpPost(url);
Log.v(TAG2, "testurl");
HttpResponse rp = null;
try {
rp = hc.execute(post);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.v(TAG2, "testpost");
if(rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
{
try {
return EntityUtils.toString(rp.getEntity());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return "Error";
}
@Override
protected void onPostExecute(String result) {
TextView textView = textViewReference.get();
if(textView != null) {
textView.setText(result);
}
}
}
getXmlTask.java上没有错误,但我在行上有此错误
getXmlTask task = new getXmlTask(textview1 , "http://www.3pi.tf/test.xml");
它说“无法将textview1解析为变量” ...但是我在mainlayout.xml上使用“ + id”。
textview1
应该是您MainActivity
的变量,例如
TextView textview1 = (TextView) findViewById(R.id.textview1);
然后将textview1
传递给您的getXmlTask
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.