I'm trying to make an app that connects to my tcp server on my pc to send text but every time I start the thread, my app crashes. please help. I just started using android studio so im not that familiar with it.
Here's my code:
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();
}
}
}
}
}
Here's what the debugger says when I click on the button to start the thread to connect to my pc
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'
Your problem is on this line:
Thread Thread1 = null;
You are setting the thread to a null value and that's where it says the error is coming from. Try assigning it a different value and see if that changes anything.
You are starting Thread at onCreate()
this.Thread1 = new Thread(new Thread1());
this.Thread1.start();
Then again starting the same thread at onClick()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.