私はAndroidプログラミングが初めてです。ソケットの作成中にアプリケーションがクラッシュします。マニフェストのインターネット許可に追加し、Wifi をオンにしました。Android クライアントを待機している PC にサーバーがあります。私の他のアプリケーションとうまく動作します。このアプリケーションのどこに問題があるのか わかりません。助けていただければ幸いです。
ログエラー
01-23 13:10:32.110: E/AndroidRuntime(4493): 致命的な例外: メイン
01-23 13:10:32.110: E/AndroidRuntime(4493): java.lang.RuntimeException: アクティビティ ComponentInfo{com を開始できません。 example.usapp/com.example.usapp.Settings}: android.os.NetworkOnMainThreadException
01-23 13:10:32.110: E/AndroidRuntime(4493): android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1751) で
01 -23 13:10:32.110: E/AndroidRuntime(4493): android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1767)
01-23 13:10:32.110: E/AndroidRuntime(4493): android.app で.ActivityThread.access$1500(ActivityThread.java:122)
01-23 13:10:32.110: E/AndroidRuntime(4493): android.app.ActivityThread$H.handleMessage(ActivityThread.java:1005) で
01-23 13:10:32.110: E/AndroidRuntime(4493): android.os.Handler.dispatchMessage(Handler.java:99)
01-23 13:10:32.110: E/AndroidRuntime(4493): Android で。 os.Looper.loop(Looper.java:132)
01-23 13:10:32.110: E/AndroidRuntime(4493): android.app.ActivityThread.main(ActivityThread.java:4028)
01-23 13:10: 32.110: E/AndroidRuntime(4493): at java.lang.reflect.Method.invokeNative(Native Method)
01-23 13:10:32.110: E/AndroidRuntime(4493): at java.lang.reflect.Method.invoke( Method.java:491)
01-23 13:10:32.110: E/AndroidRuntime(4493): com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
01-23 13:10: 32.110: E/AndroidRuntime(4493): com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602) で
01-23 13:10:32.110: E/AndroidRuntime(4493): dalvik.system.NativeStart.main(ネイティブ メソッド)
01-23 13:10:32.110: E/AndroidRuntime(4493): 原因: android.os .NetworkOnMainThreadException
01-23 13:10:32.110: E/AndroidRuntime(4493): android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1077)
01-23 13:10:32.110: E/AndroidRuntime(4493) : dalvik.system.BlockGuard$WrappedNetworkSystem.connect(BlockGuard.java:368)
01-23 13:10:32.110: E/AndroidRuntime(4493): org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl) で.java:208)
01-23 13:10:32.110: E/AndroidRuntime(4493): org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:431) で
01-23 13:10:32.110: E/AndroidRuntime(4493): java.net.Socket.connect(Socket.java:901)
01-23 13:10:32.110: E/AndroidRuntime(4493): Java で。 net.Socket.connect(Socket.java:844)
01-23 13:10:32.110: E/AndroidRuntime(4493): com.example.usapp.Stream.doInBackground(Stream.java:46)
01-23 13: 10:32.110: E/AndroidRuntime(4493): com.example.usapp.Settings.onCreate(Settings.java:37)
01-23 13:10:32.110: E/AndroidRuntime(4493): android.app.Instrumentation で.callActivityOnCreate(Instrumentation.java:1048)
01-23 13:10:32.110: E/AndroidRuntime(4493): android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1715) で
01-23 13:10:32.110: E /AndroidRuntime(4493): ... 11 件以上
Java クラス
package com.example.usapp;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Handler;
import android.util.Log;
public class Stream extends AsyncTask<Void, Void, Void>{
Bitmap image;
int frameCounter=0;
int checkCounterFPS=0;
long lastFpsTime=0;
double FPS;
protected Handler handler_bitmap, handlerdata;
boolean firstSetOfFPS=false;
double availableFPS;
double toleranceValue;
boolean firstFalseFPS = false;
byte[] datainput = new byte[1024];
Socket socket;
@Override
protected Void doInBackground(Void... params) {
Log.i("DUPA","po String doInBackground(String... params) ");
try {
Log.i("DUPA","przed socket");
socket = new Socket();
InetSocketAddress adres = new InetSocketAddress("192.168.1.50", 4444);
//adres.
socket.connect(adres);
//socket = new Socket("192.168.1.50", 4444);
Log.i("DUPA","po socket");
BufferedInputStream in = new BufferedInputStream(socket.getInputStream());
Log.i("DUPA","po socketconnected ");
/*
do {
try {
ByteBuffer buffer = ByteBuffer.allocate(9);
in.read(buffer.array());
byte[] bBuffer = new byte[9];
buffer.get(bBuffer, 0, 9);
int len = Integer.parseInt(new String(bBuffer,"US-ASCII"));
int bytesRecieved = 0;
ByteBuffer completeBuf;
completeBuf = ByteBuffer.allocate(len);
while (len > bytesRecieved) {
int inSize = in.available();
if (inSize > len- bytesRecieved) {
buffer = ByteBuffer.allocate(len- bytesRecieved);
bytesRecieved += len- bytesRecieved;
in.read(buffer.array());
completeBuf.put(buffer);
} else {
bytesRecieved += inSize;
buffer = ByteBuffer.allocate(inSize);
in.read(buffer.array());
completeBuf.put(buffer);
}
}
frameCounter++;
long delay = (long) (System.currentTimeMillis() - lastFpsTime);
if (delay > 1000) {
checkCounterFPS++;
FPS = (((double) frameCounter) / delay) * 1000;
handler_bitmap.post(new Runnable() {
public void run() {
///usunieto fps set text
}
});
frameCounter = 0;
lastFpsTime = System.currentTimeMillis();
}
if (checkCounterFPS >= 3 && firstSetOfFPS == true) {
checkCounterFPS = 0;
if (FPS <= ((double) availableFPS - toleranceValue)
|| FPS >= ((double) availableFPS + toleranceValue)) {
firstFalseFPS = true;
}
} else if (firstFalseFPS == true) {
if (FPS <= ((double) availableFPS - toleranceValue)
|| FPS >= ((double) availableFPS + toleranceValue)) {
// setSendText("FPS",(int) FPS);
firstFalseFPS = false;
}
}
//publishProgress(completeBuf);
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.i("DUPA","blad1");
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.i("DUPA","blad2");
}
} while (!socket.isInputShutdown() && !socket.isClosed()); //
// publishProgress("Status: Closed".getBytes());*/
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.i("DUPA","blad3");
}
Log.i("AsyncTask", "doInBackground: Finished");
return null;
}
protected Bitmap onProgressUpdate(ByteBuffer... values) {
Log.i("DUPA","blad4");
return image = BitmapFactory.decodeByteArray(values[0].array(), 0,values[0].array().length);
}
}