0

我正在开发一个简单的 android 应用程序,它有几个应该连接到 TCP 服务器的布局。它适用于主页,但不适用于其他页面。谁能告诉我问题出在哪里?

下面是我的源代码

客户端.java

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class Client extends Activity {

    private Socket socket;

    private static final int SERVERPORT = 2222;
    private static final String SERVER_IP = "172.18.123.176";
    SeekBar seekbar;
    TextView value;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);  
        //


        value = (TextView) findViewById(R.id.textview);
         seekbar = (SeekBar) findViewById(R.id.seekBar1);





        seekbar.setOnSeekBarChangeListener( new OnSeekBarChangeListener()
        {
        public void onProgressChanged(SeekBar seekBar, int progress,
           boolean fromUser)
        {
               // TODO Auto-generated method stub
              value.setText("SeekBar value is "+progress);
                                                        }

             public void onStartTrackingTouch(SeekBar seekBar)
        {
                // TODO Auto-generated method stub
                                                        }

          public void onStopTrackingTouch(SeekBar seekBar)
        {
           // TODO Auto-generated method stub
                                                        }
        });


        //

        new Thread(new ClientThread()).start();
    }



    public void onClick1(View view) {
        try {
            TextView et = (TextView) findViewById(R.id.textView1);
            String str = et.getText().toString();
            PrintWriter out = new PrintWriter(new BufferedWriter(
                    new OutputStreamWriter(socket.getOutputStream())),
                    true);
            out.println(str);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void onClick2(View view) {
        try {
            TextView et = (TextView) findViewById(R.id.TextView01);
            String str = et.getText().toString();
            PrintWriter out = new PrintWriter(new BufferedWriter(
                    new OutputStreamWriter(socket.getOutputStream())),
                true);
            out.println(str);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
    } catch (Exception e) {
            e.printStackTrace();
        }
    }





    class ClientThread implements Runnable {

        @Override
        public void run() {

            try {
                InetAddress serverAddr = InetAddress.getByName(SERVER_IP);

                socket = new Socket(serverAddr, SERVERPORT);

            } catch (UnknownHostException e1) {
                e1.printStackTrace();
            } catch (IOException e1) {
                e1.printStackTrace();
            }

        }

    }
}

主要的.xml

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_x="2dp"
    android:layout_y="116dp"
    android:onClick="onClick1" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_x="146dp"
    android:layout_y="134dp"
    android:text="LIGHT ON" />

<Button
    android:id="@+id/Button01"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_x="0dp"
    android:layout_y="190dp"
    android:onClick="onClick2" />

<TextView
    android:id="@+id/TextView01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_x="146dp"
    android:layout_y="210dp"
    android:text="LIGHT OFF" />

<TextView
    android:id="@+id/TextView02"
    android:layout_width="225dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="40dip"
    android:layout_marginTop="40dip"
    android:layout_x="43dp"
    android:layout_y="350dp"
    android:gravity="center"
    android:text="Living Room Light"
    android:textColor="#0b84aa"
    android:textSize="25dip" />

<TextView
    android:id="@+id/textview"
    android:layout_width="288dp"
    android:layout_height="wrap_content"
    android:layout_x="12dp"
    android:layout_y="87dp" />

<SeekBar
    android:id="@+id/seekBar1"
    android:layout_width="205dp"
    android:layout_height="wrap_content"
    android:layout_x="70dp"
    android:layout_y="59dp"
    android:max="100" />

</AbsoluteLayout>
4

1 回答 1

1

好吧,无论如何,由于您没有发布日志跟踪,因此从您的代码中我看不到您已启动,ClientThread因此您socketnull任何按钮按下都会导致崩溃。

添加到末尾Create

new Thread(new ClientThread()).start();  
于 2013-09-09T06:56:55.170 回答