简体   繁体   English

将Xml布局到我的android应用

[英]Layout an Xml to my android App

Good morning ! 早上好 ! So I have this 所以我有这个

MainActivity.java 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(); 
 }
} 

and there is my getXmlTask.java 还有我的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);
        }       
    }

}

No error on getXmlTask.java but i have this error on line getXmlTask​​.java上没有错误,但我在行上有此错误

getXmlTask task = new getXmlTask(textview1 , "http://www.3pi.tf/test.xml");

It says "textview1 cannot be resolved to a variable"... but I "+id" on my mainlayout.xml.. Sorry i just begin on android development and sorry again for my english ^^ 它说“无法将textview1解析为变量” ...但是我在mainlayout.xml上使用“ + id”。

textview1 should be a variable in your MainActivity like textview1应该是您MainActivity的变量,例如

TextView textview1 = (TextView) findViewById(R.id.textview1);

and then pass textview1 to your getXmlTask 然后将textview1传递给您的getXmlTask

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM