簡體   English   中英

將Xml布局到我的android應用

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM