我正在尝试创建一个连接到我的电脑上的tcp服务器的应用程序来发送文本,但每次启动该线程时,我的应用程序崩溃了。请帮忙。我刚刚开始使用android studio,所以我不熟悉它。
这是我的代码:
package com.example.alex.hahanice;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class MainActivity extends AppCompatActivity {
Handler UIHandler;
Thread Thread1 = null;
public static final int SERVERPORT = 6000;
public static final String SERVERIP = "192.168.1.76";
@Override
protected void onCreate(Bundle savedInstanceState) {
this.Thread1 = new Thread(new Thread1());
this.Thread1.start();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View v) {
View view;
runOnUiThread(new Runnable() {
public void run() {
Thread1.start();
}
});
}
class Thread1 implements Runnable
{
public void run() {
Socket socket = null;
try
{
InetAddress serverAddr = InetAddress.getByName(SERVERIP);
socket = new Socket(serverAddr, SERVERPORT);
Thread2 commThread = new Thread2(socket);
new Thread(commThread).start();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
class Thread2 implements Runnable
{
private Socket clientSocket;
private BufferedReader input;
public Thread2(Socket clientSocket)
{
this.clientSocket = clientSocket;
try
{
this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));
}
catch(IOException e)
{
e.printStackTrace();
}
}
public void run()
{
while(!Thread.currentThread().isInterrupted())
{
try
{
String read = input.readLine();
if(read != null)
{
//UIHandler.post(new updateUIThread(read));
}
else
{
Thread1 = new Thread(new Thread1());
Thread1.start();
return;
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
}
这是调试器在单击按钮启动线程连接到我的电脑时所说的内容
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.alex.hahanice, PID: 3772
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:4659)
at android.view.View$PerformClick.run(View.java:19462)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5692)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4659)
at android.view.View$PerformClick.run(View.java:19462)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5692)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalThreadStateException: Thread already started
at java.lang.Thread.checkNotStarted(Thread.java:871)
at java.lang.Thread.start(Thread.java:1025)
at com.example.alex.hahanice.MainActivity$1.run(MainActivity.java:50)
at android.app.Activity.runOnUiThread(Activity.java:5001)
at com.example.alex.hahanice.MainActivity.onClick(MainActivity.java:48)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4659)
at android.view.View$PerformClick.run(View.java:19462)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5692)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
Disconnected from the target VM, address: 'localhost:8600', transport: 'socket'
答案 0 :(得分:0)
你的问题就在这一行:
Thread Thread1 = null;
您正在将线程设置为空值,并且它表示错误来自的位置。尝试为其分配不同的值,看看是否会发生任何变化。
答案 1 :(得分:0)
您正在onCreate()
启动Threadthis.Thread1 = new Thread(new Thread1());
this.Thread1.start();
然后再次在onClick()
上启动相同的线程