TextView未显示

时间:2013-11-18 15:46:14

标签: java android textview

嘿,我的问题是,无论我是否将其定义为可见,都不会显示textview的内容。在logcat中没有显示任何问题...

我使用像JFrankie这样的AsyncTask告诉我但是我的textview的价值现在还没有显示出来的事情呢?现在我得到一个logcat错误,套接字已经关闭了吗?

这是我的方法:

  public void fetchUpdate(View view) {
     AsyncTask<Void, Void, String> at= new Task();

    at.execute();
    try {
        tv.setText(at.get());
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
  }

AsyncTask部分:

   package com.example.s_crewclientprealpha;

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.OptionalDataException;
    import java.io.OutputStream;
    import java.net.Socket;
    import java.net.UnknownHostException;
    import android.os.AsyncTask;
    import android.widget.TextView;

    public class Task extends AsyncTask<Void,Void,String>{
     Socket serverside = null;
   InputStream i=null;
   OutputStream o=null;
   Thread t=null;
   Thread update=null;
   Thread txtviewable=null;
   String Restaurant=null;
   String UserID=null;
   String UserBestellung=null;
   CharSequence text=null;
   TextView tv=null;
   ObjectOutputStream oos = null;
   ObjectInputStream ois=null;

@Override
protected String doInBackground(Void... params) {
    connecttoServer();

    try {
        oos.writeInt(1);
        oos.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {

        Restaurant=(String) ois.readObject();       
} catch (OptionalDataException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
    return Restaurant;

}
@Override
protected void onPostExecute(String result){


}


public void connecttoServer(){
     try {
            serverside=new Socket("veteran1.ez.lv",5544);//serverside ist der server
            i= serverside.getInputStream();
            o= serverside.getOutputStream();
            oos = new ObjectOutputStream(o);
            ois= new ObjectInputStream(i);
             } catch (UnknownHostException e) {
                 e.printStackTrace();

            // TODO Auto-generated catch block

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NullPointerException e){

            e.printStackTrace();
        }


}

} XML部分:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayoutMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/holzbackground"
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"
android:icon="@drawable/ic_launcher" >
<TextView
    android:id="@+id/textViewHeadline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="@string/txtdev"
    android:visibility="visible" />

<EditText
    android:id="@+id/UsersName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textViewHeadline"
    android:ems="10"
    android:inputType="textPersonName"
    android:text="@string/usernamebsptxt" >

    <requestFocus />
</EditText>

<TextView
    android:id="@+id/textViewBottom"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="89dp"
    android:text="@string/txtview2" />

<EditText
    android:id="@+id/bestellungstext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/UsersName"
    android:layout_marginTop="18dp"
    android:ems="10"
    android:hint="@string/HinttxtOrder"
    android:text="@string/HinttxtOrder" />

<Button
    android:id="@+id/buttontest"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/bestellungstext"
    android:onClick="setOrder"
    android:text="@string/sendorderbuttontxt" />

<Button
    android:id="@+id/updatebutton"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/buttontest"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/buttontest"
    android:text="@string/update"
    android:onClick="fetchUpdate" />

<Button
    android:id="@+id/buttontoadminconsole"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:text="@string/buttontxttoadminconsole"
    android:onClick="toAdminConsole" />

logcat的:

    11-19 20:07:17.482: W/System.err(11921): java.net.SocketException: Socket closed
    11-19 20:07:17.482: W/System.err(11921):    at       libcore.io.Posix.recvfromBytes(Native Method)
    11-19 20:07:17.482: W/System.err(11921):    at  libcore.io.Posix.recvfrom(Posix.java:140)
   11-19 20:07:17.482: W/System.err(11921):     at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:164)
   11-19 20:07:17.482: W/System.err(11921):     at libcore.io.IoBridge.recvfrom(IoBridge.java:506)
   11-19 20:07:17.482: W/System.err(11921):     at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
   11-19 20:07:17.482: W/System.err(11921):     at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
   11-19 20:07:17.482: W/System.err(11921):     at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
   11-19 20:07:17.482: W/System.err(11921):     at libcore.io.Streams.readSingleByte(Streams.java:41)
   11-19 20:07:17.492: W/System.err(11921):     at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:236)
   11-19 20:07:17.492: W/System.err(11921):     at java.io.FilterInputStream.read(FilterInputStream.java:114)
   11-19 20:07:17.492: W/System.err(11921):     at java.io.ObjectInputStream.checkReadPrimitiveTypes(ObjectInputStream.java:405)
   11-19 20:07:17.492: W/System.err(11921):     at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:770)
   11-19 20:07:17.492: W/System.err(11921):     at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2006)
   11-19 20:07:17.492: W/System.err(11921):     at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1963)
  11-19 20:07:17.492: W/System.err(11921):  at com.example.s_crewclientprealpha.Task.doInBackground(Task.java:42)
 11-19 20:07:17.492: W/System.err(11921):   at  com.example.s_crewclientprealpha.Task.doInBackground(Task.java:1)
 11-19 20:07:17.492: W/System.err(11921):   at android.os.AsyncTask$2.call(AsyncTask.java:287)
 11-19 20:07:17.492: W/System.err(11921):   at java.util.concurrent.FutureTask.run(FutureTask.java:234)
 11-19 20:07:17.492: W/System.err(11921):   at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
 11-19 20:07:17.492: W/System.err(11921):   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
 11-19 20:07:17.492: W/System.err(11921):   at   
 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
 11-19 20:07:17.492: W/System.err(11921):   at java.lang.Thread.run(Thread.java:841)

1 个答案:

答案 0 :(得分:1)

在我看来,它无法工作,因为你在一个线程中运行更新,然后流程继续,当它到达TextView更新时,值(你应该在textview中写入)可以为null或为空。 Thread结果和Textview之间没有同步。 你不应该使用这种方法,我建议你使用AsyncTask(小心内存泄漏)或齐射来检索远程信息并更新TextView。 如果您使用AsyncTask,您在线程中编写的内容应该放在doInBackground方法中,而TextView更新应该放在onPostExecute方法中。