1

我是java和andriod的新手,我认为我在线程程序启动时做错了,没有任何错误。

我插入了一个按钮只是为了测试,它显示没有问题。文本视图甚至不显示“Hello world”默认字符串。

这是代码

    package com.example.studentservis;

    import android.R.string;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.app.Activity;
    import android.view.Menu;
    import android.widget.TextView;
    import java.net.*;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    import java.io.*;
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;
    import org.jsoup.select.Elements;
    import org.w3c.dom.Text;

    public class MainActivity extends Activity  {

        StringHandler stringHandler = new StringHandler();
        @Override
        protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main); 

            thread.run();

            TextView txtView = (TextView)this.findViewById(R.id.textView1);
            txtView.setText(stringHandler.getString());

        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.activity_main, menu);


            return true;
        }

        Thread thread = new Thread(){
            public void run(){
                try {

                    stringHandler.setString(webRequest());


                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    stringHandler.setString(e.getMessage());
                }
            }
        };


        public String webRequest() throws Exception{
            String servisURL = "http://www.sczg.unizg.hr/student-servis/";
            Document doc = Jsoup.connect(servisURL).get();
            Elements jobNode = doc.select("div.jobBox");

            return jobNode.get(0).text();
        }

        public class StringHandler
        {
            public String str = "test";

            public void setString(String s)
            {
                str = s;
            }

            public String getString()
            {
                return str;
            }
        }



    }
4

3 回答 3

2

当您启动一个新线程时,UI 线程不会等到新线程完成。这就是线程的目的。

这里 :

thread.run();

当您启动新线程来更新时stringHandler,UI 线程会尝试处理下一个命令,即:

txtView.setText(stringHandler.getString());

当 UI 线程到达这里时,stringHandler还没有更新,所以它是空的,因此它不会在 textview 中显示任何内容。

您应该做的就是在新线程完成后尝试从新线程更新文本视图。

试试这个:

Thread thread = new Thread(){
    public void run(){
        try {

            stringHandler.setString(webRequest());


        } catch (Exception e) {
            // TODO Auto-generated catch block
            stringHandler.setString(e.getMessage());
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                TextView txtView = (TextView)this.findViewById(R.id.textView1);
                txtView.setText(stringHandler.getString());
            }
        });
    }
};
于 2013-06-03T10:26:27.447 回答
1

这部分代码:txtView.setText(stringHandler.getString());

应该在线程完成后放置。

        thread.run(); <------ Start running thread in background

        TextView txtView = (TextView)this.findViewById(R.id.textView1);
        txtView.setText(stringHandler.getString());
        // the textView was set without knowing whether the Thread is finished or not.
        // thus, textView is shown as empty, because stringHandler.getString() still has empty string

我的建议是你在里面实现它asyncTask,这样你就可以在里面设置文本onPostExecute()

祝你好运^^

编辑: 这是一些示例 AsyncTask

private class StringSet extends AsyncTask <Void,Void,Void> {

@Override 
protected void doInBackground(Void... params) {

    try {
         stringHandler.setString(webRequest());
    } catch (Exception e) {
         stringHandler.setString(e.getMessage());
    }
}

@Override 
protected void onPostExecute(Void... results) {
    txtView.setText(stringHandler.getString());
    }
}

并将您的替换thread.run()new StringSet().execute();

于 2013-06-03T10:25:49.037 回答
1

为此,您可以使用处理程序或 asyctask。异步任务更容易。

http://developer.android.com/reference/android/os/AsyncTask.html

您可以创建自己的线程,但请记住在 ui 线程上更新 ui。

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="166dp"
    android:text="Loading..." />

  </RelativeLayout> 

MainActivity.java

删除 StringHandler 因为我觉得在这种情况下没有必要。

public class MainActivity extends Activity  {

 TextView txtView ;
 String s;
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); 
    txtView  = (TextView)this.findViewById(R.id.textView1); // initialize textview
    //txtView.setText(stringHandler.getString());
    new Thread(new Runnable() {  // thread
        @Override  
        public void run(){

            try {
                s = webRequest(); // get string 
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            runOnUiThread(new Runnable() //run on ui threa
            {
               public void run() 
               { 
                    txtView.setText(s); // update ui on the ui thread.
               } 
            });


        }
}).start();


}

public String webRequest() throws Exception{   // web request
    String servisURL = "http://www.sczg.unizg.hr/student-servis/";
    Document doc = Jsoup.connect(servisURL).get();
    Elements jobNode = doc.select("div.jobBox");

    return jobNode.get(0).text();
}
}

结果快照

在此处输入图像描述

于 2013-06-03T10:27:03.963 回答