早上好!所以我有这个
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”.. 抱歉,我刚刚开始 Android 开发,再次抱歉我的英语 ^^
最佳答案
textview1
应该是 MainActivity
中的一个变量,例如
TextView textview1 = (TextView) findViewById(R.id.textview1);
然后将 textview1
传递给您的 getXmlTask
关于java - 将 Xml 布局到我的 Android 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20515520/