好的,所以我正在使用以下代码制作一个简单的 pc-android 服务器客户端应用程序:
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;
}
}
}
但是由于某种原因,当运行时,它会连接到服务器然后崩溃而不更改我的 textview 日志,我知道这不是服务器的问题,因为我在普通的 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)