OK、私はこのコードで簡単なpc-android server-clientappを作成しています:
package org.smiley.doom;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
public class ClientActivity extends Activity implements View.OnClickListener{
public static ImageView im;
public static Socket s;
public static boolean go;
public static TextView log;
Button send;
EditText tip;
InetAddress inet;
int rport;
String ip;
ObjectOutputStream out;
ObjectInputStream in;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tip = (EditText) findViewById(R.id.etIP);
send = (Button) findViewById(R.id.bSEND);
im = (ImageView) findViewById(R.id.ivPIC);
log = (TextView) findViewById(R.id.tvLog);
s = null;
in = null;
out = null;
send.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
switch(arg0.getId()){
case R.id.bSEND:
byte[] b;
int len;
try {
inet = InetAddress.getByName("192.168.0.2");
s = new Socket(inet, 4321);
out = new ObjectOutputStream(s.getOutputStream());
in = new ObjectInputStream(s.getInputStream());
log.setText("Client opened");
len = in.readInt();
b = new byte[len];
in.read(b);
log.setText(b.length);
out.writeBoolean(true);
out.close();
in.close();
s.close();
System.out.println("Client closed");
} catch (UnknownHostException e) {
log.setText(e.getMessage().toString());
} catch (IOException e) {
log.setText(e.getMessage().toString());
}
break;
}
}
}
しかし、何らかの理由で、実行すると、サーバーに接続し、テキストビューログをまったく変更せずにクラッシュします。通常のJavaプロジェクトでほぼ同じコードを実行していたため、サーバーに問題はないことがわかりました。完璧に接続されています。問題は、オブジェクトの出力/入力ストリームを設定しようとしたときのようです(ログの更新とクライアントが接続を要求する間に発生するすべての理由のため)が、私は私の人生の理由を理解できません
いつものようにどんな助けもありがたいです:)
編集:ここに私のlogcatがあります
07-20 14:17:22.374: W/ResourceType(335): No package identifier when getting value for resource number 0x000096ce
07-20 14:17:22.374: D/AndroidRuntime(335): Shutting down VM
07-20 14:17:22.374: W/dalvikvm(335): threadid=1: thread exiting with uncaught exception (group=0x40015560)
07-20 14:17:22.385: E/AndroidRuntime(335): FATAL EXCEPTION: main
07-20 14:17:22.385: E/AndroidRuntime(335): android.content.res.Resources$NotFoundException:
String resource ID #0x96ce
07-20 14:17:22.385: E/AndroidRuntime(335): at android.content.res.Resources.getText(Resources.java:201)
07-20 14:17:22.385: E/AndroidRuntime(335): at android.widget.TextView.setText(TextView.java:2857)
07-20 14:17:22.385: E/AndroidRuntime(335): at
org.smiley.doom.ClientActivity.onClick(ClientActivity.java:65)
07-20 14:17:22.385: E/AndroidRuntime(335): at android.view.View.performClick(View.java:2485)
07-20 14:17:22.385: E/AndroidRuntime(335): at android.view.View$PerformClick.run(View.java:9080)
07-20 14:17:22.385: E/AndroidRuntime(335): at android.os.Handler.handleCallback(Handler.java:587)
07-20 14:17:22.385: E/AndroidRuntime(335): at android.os.Handler.dispatchMessage(Handler.java:92)
07-20 14:17:22.385: E/AndroidRuntime(335): at android.os.Looper.loop(Looper.java:123)
07-20 14:17:22.385: E/AndroidRuntime(335): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-20 14:17:22.385: E/AndroidRuntime(335): at java.lang.reflect.Method.invokeNative(Native Method)
07-20 14:17:22.385: E/AndroidRuntime(335): at java.lang.reflect.Method.invoke(Method.java:507)
07-20 14:17:22.385: E/AndroidRuntime(335): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-20 14:17:22.385: E/AndroidRuntime(335): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-20 14:17:22.385: E/AndroidRuntime(335): at dalvik.system.NativeStart.main(Native Method)