В моей деятельности у меня есть следующий код:
mChannel= new SignalChannel();
mChannel.run();
SignalChannel - это класс, который расширяет поток следующим образом:
package com.eddieharari.poker5o;
import android.os.AsyncTask;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class SignalChannel extends Thread {
public boolean StopMe = false;
private AppConstants mAppConstants = new AppConstants();
private Socket mSocket;
private String mServer = mAppConstants.ServerIP;
private int mPort = mAppConstants.SignalPort;
private Logger mLogger = new Logger();
private InetAddress ServerIP;
private PrintWriter mPrintWriter;
private BufferedReader mBufferedReader;
@Override
public void run() {
try {
ServerIP = InetAddress.getByName(mServer);
mSocket = new Socket(mServer, mPort);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Пока - все еще получаю ошибку:
j
ava.lang.RuntimeException: Unable to start activity ComponentInfo{com.eddieharari.poker5o/com.eddieharari.poker5o.LobbyActivity}: android.os.NetworkOnMainThreadException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1565)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:389)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:230)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:212)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:436)
at java.net.Socket.connect(Socket.java:621)
at java.net.Socket.connect(Socket.java:570)
at java.net.Socket.<init>(Socket.java:450)
at java.net.Socket.<init>(Socket.java:218)
at com.eddieharari.poker5o.SignalChannel.run(SignalChannel.java:27)
at com.eddieharari.poker5o.LobbyActivity.onCreate(LobbyActivity.java:38)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
Я пытаюсь выяснить, почему мои сети рассматриваются в основном потоке, я открываю новый поток для этих сетевых операций.
спасибо,
Edd ie.