gpt4 book ai didi

java - Android 应用程序与 TCP 服务器的问题

转载 作者:行者123 更新时间:2023-12-01 14:08:50 24 4
gpt4 key购买 nike

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

下面是我的源代码

Client.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();
}

}

}
}

ma​​in.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>

最佳答案

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

添加到创建的末尾:

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

关于java - Android 应用程序与 TCP 服务器的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18692725/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com