好的,所以我用这段代码制作一个简单的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)
答案 0 :(得分:1)
您获得了ResourcesNotFoundException
,因为您引用的其中一个资源无法在XML中解析。首先尝试在Eclipse中清理项目。您可以通过转到“项目 - >清理”来确保清理正确的项目。最后,请确保您正在测试的Android版本可以访问您的main.xml
布局文件。您可以将它放在设备无法访问的合格目录中(例如xlarge布局目录)。
以下是相关错误:
android.content.res.Resources$NotFoundException:
[ ... ]
at org.smiley.doom.ClientActivity.onClick(ClientActivity.java:65)
[ ... ]