无法更新我的Android应用程序中的用户界面

时间:2014-02-08 12:26:58

标签: android eclipse android-asynctask

package com.asyncu;

import android.os.Bundle;
import android.app.Activity;
//import android.app.Notification;
import android.util.Log;
import android.view.*;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Build;

import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;


public class MainActivity extends Activity {
    Button bttn;
    CheckedTextView ctv;
    TextView result;
    Button rset;
    NetworkTask networktask;
    byte[] buffer = new byte[256];
    Boolean connected=false;
    DatagramSocket ds=null;
    boolean x=true;
    static int packetCount = 0;
    byte[] p;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bttn=(Button)findViewById(R.id.Button1);
        result=(TextView)findViewById(R.id.result);
        bttn.setOnClickListener(buttonConnectOnClickListener);
    }
    private OnClickListener buttonConnectOnClickListener = new OnClickListener() {
        public void onClick(View v){
            if(!connected){//if not connected
                System.out.println("connecting to Server");
               networktask = new NetworkTask(); //New instance of NetworkTask
                networktask.execute();
            }
            else{
                System.out.println("disconnecting from Server...");
                if(networktask!=null){
                    networktask.closeSocket();
                    networktask.cancel(true);
                }

            }
        }
    };
    public class NetworkTask extends AsyncTask<String, String, String> 
    {

        @Override
        protected String doInBackground(String... params)
        {
            //boolean res= false;

            try
            {
                Log.i("AsyncTask", "doInBackground: Creating socket");
                ds=new DatagramSocket(5000);
                Log.i("AsyncTask", "doInBackground: socket created");
                 DatagramPacket recv_packet = new DatagramPacket(buffer, buffer.length);
                 packetCount = 0;
                while(x)
                  {
                    System.out.println("in while");

                     Log.i("AsyncTask", "doInBackground: in while");

                     ds.receive(recv_packet);
                    Log.i("AsyncTask", "doInBackground: packets received");
                    packetCount++;

                    publishProgress(""+packetCount);
                    Log.i("AsyncTask", "doInBackground: after publish");



                  }
            }
            catch(RuntimeException e)
            {
                System.out.println("Error"+e.getMessage()+"\n"+e.getStackTrace().toString());
            }
            catch(Exception e)
            {
                System.out.println("Error"+e.getMessage()+"\n"+e.getStackTrace().toString());
            }
        finally
        {
            closeSocket();
        }
            return null;
    }
        void closeSocket()
        {
            ds.close();
            x=false;
        }
        public void onProgressUpdate(String... currentPacketCount)
        {
            super.onProgressUpdate(currentPacketCount);
            Log.e("AsyncTask", "doInBackground: " + currentPacketCount.toString());
            result.setText(""+currentPacketCount);
        }

    }


}

我正在尝试在android中开发UDP客户端,它从服务器接收数据包并递增计数器同时将值打印到UI。 logcat显示收到数据包。但是不显示计数器值。我无法追踪出错的地方。 请检查我的代码并提供有用的见解。提前谢谢。

布局文件

<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" >

    <ToggleButton
        android:id="@+id/Button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/checkedTextView1"
        android:layout_below="@+id/checkedTextView1"
        android:text="ToggleButton"
        android:textOff="Connect"
        android:textOn="Disconnect" />


    <CheckedTextView
        android:id="@+id/checkedTextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/checkedTextView1"
        android:text="No. of packets received" />

    <TextView
        android:id="@+id/result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/Button1"
        android:layout_alignParentRight="true"
        android:layout_marginRight="42dp"
        android:text="result" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

删除此行

result.setText(""+currentPacketCount);

添加此行

result.setText(""+currentPacketCount[0]);