我在通过蓝牙反序列化我的课程时遇到了一些麻烦。从PC到Android设备我在我的电脑应用程序上使用bluecove快照2.1.1库我在android上的输入流确实接收字节,因为我很快就将一些数据发送到我的ouputstream我的android应用程序崩溃。 所以在这里我的堆栈跟踪。此外,蓝牙套接字正在工作,因为我可以在PC上将整数发送到字节
W/System.err: java.lang.ClassNotFoundException: bluetooth.LiteProject
W/System.err: at java.lang.Class.classForName(Native Method)
W/System.err: at java.lang.Class.forName(Class.java:324)
W/System.err: at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:2258)
W/System.err: at java.io.ObjectInputStream.readNewClassDesc(ObjectInputStream.java:1641)
W/System.err: at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:657)
W/System.err: at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1782)
W/System.err: at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:761)
W/System.err: at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1983)
W/System.err: at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1940)
W/System.err: at com.example.ihat3i.androidtesting.BluetoothThreadClasses.BluetoothDataShare.deserialize(BluetoothDataShare.java:121)
W/System.err: at com.example.ihat3i.androidtesting.MainActivity.onClick(MainActivity.java:96)
W/System.err: at android.view.View.performClick(View.java:5714)
W/System.err: at android.widget.TextView.performClick(TextView.java:10926)
W/System.err: at android.view.View$PerformClick.run(View.java:22589)
W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err: at android.os.Looper.loop(Looper.java:148)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7325)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
W/System.err: Caused by: java.lang.ClassNotFoundException: Didn't find class "bluetooth.LiteProject" on path: DexPathList[[dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-support-annotations-25.0.0_733f073c18693e37682f4ac22b5f541d6f65a521-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-slice_9-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-slice_8-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-slice_7-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-slice_6-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-slice_5-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-slice_4-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-slice_3-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-slice_2-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-slice_1-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-slice_0-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-internal_impl-25.0.0_9bc67552422813c99b6458ea8770395016890bfd-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-internal_impl-25.0.0_5030af852f549789ae15eb264dc11320e23256e8-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-internal_impl-25.0.0_4a2c66133c72c8cb335db37fa043de6026539626-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-internal_impl-25.0.0_3a303aa6f7e828e43c1336863ca83eefc55642dc-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-internal_impl-25.0.0_2d05349ce99bb610027ae1a1f205e7ff481c80ea-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-com.android.support-support-vector-drawable-25.0.0_93d794c274330c4bac9e23b24b0692ce4a48b160-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-com.android.support-support-v4-25.0.0_bf3767cfe44655d3a165ffe94a66d5d9feaa2a26-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-com.android.support-support-media-compat-25.0.0_8edae4c59f3b3f76c9b225952262e5bc47b157df-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-com.android.support-support-fragment-25.0.0_f7d3af020703d4b4bd21e04d132251937de287d4-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-com.android.support-support-core-utils-25.0.0_86f6e8f7e9b82e3cd1e20da88591291ea705ca15-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-com.android.support-support-core-ui-25.0.0_6d54f4432204da9a7699fadec1a26048490d7085-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-com.android.support-support-compat-25.0.0_95ad1fee5438892c0a020310eab8df2a9dced983-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-com.android.support-appcompat-v7-25.0.0_44eb0fc3008faa9171481a9770d2142ed7ee8ee5-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-com.android.support-animated-vector-drawable-25.0.0_788179e51b8915f8f95cd660c0d2ba6af7650941-classes.dex"],nativeLibraryDirectories=[/data/app/com.example.ihat3i.androidtesting-1/lib/arm, /vendor/lib, /system/lib]]
W/System.err: at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
W/System.err: at com.android.tools.fd.runtime.IncrementalClassLoader$DelegateClassLoader.findClass(IncrementalClassLoader.java:90)
W/System.err: at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
W/System.err: at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
W/System.err: ... 21 more
W/System.err: Suppressed: java.lang.ClassNotFoundException: Didn't find class "bluetooth.LiteProject" on path: DexPathList[[zip file "/data/app/com.example.ihat3i.androidtesting-1/base.apk"],nativeLibraryDirectories=[/data/app/com.example.ihat3i.androidtesting-1/lib/arm, /vendor/lib, /system/lib]]
W/System.err: at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
W/System.err: at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
W/System.err: at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
W/System.err: ... 22 more
W/System.err: Suppressed: java.lang.ClassNotFoundException: Didn't find class "bluetooth.LiteProject" on path: DexPathList[[dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-support-annotations-25.0.0_733f073c18693e37682f4ac22b5f541d6f65a521-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-slice_9-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-slice_8-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-slice_7-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-slice_6-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-slice_5-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-slice_4-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-slice_3-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-slice_2-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-slice_1-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-slice_0-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-internal_impl-25.0.0_9bc67552422813c99b6458ea8770395016890bfd-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-internal_impl-25.0.0_5030af852f549789ae15eb264dc11320e23256e8-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-internal_impl-25.0.0_4a2c66133c72c8cb335db37fa043de6026539626-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-internal_impl-25.0.0_3a303aa6f7e828e43c1336863ca83eefc55642dc-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-internal_impl-25.0.0_2d05349ce99bb610027ae1a1f205e7ff481c80ea-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-com.android.support-support-vector-drawable-25.0.0_93d794c274330c4bac9e23b24b0692ce4a48b160-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-com.android.support-support-v4-25.0.0_bf3767cfe44655d3a165ffe94a66d5d9feaa2a26-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-com.android.support-support-media-compat-25.0.0_8edae4c59f3b3f76c9b225952262e5bc47b157df-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-com.android.support-support-fragment-25.0.0_f7d3af020703d4b4bd21e04d132251937de287d4-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-com.android.support-support-core-utils-25.0.0_86f6e8f7e9b82e3cd1e20da88591291ea705ca15-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-com.android.support-support-core-ui-25.0.0_6d54f4432204da9a7699fadec1a26048490d7085-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-com.android.support-support-compat-25.0.0_95ad1fee5438892c0a020310eab8df2a9dced983-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-com.android.support-appcompat-v7-25.0.0_44eb0fc3008faa9171481a9770d2142ed7ee8ee5-classes.dex", dex file "/data/data/com.example.ihat3i.androidtesting/files/instant-run/dex/slice-com.android.support-animated-vector-drawable-25.0.0_788179e51b8915f8f95cd660c0d2ba6af7650941-classes.dex"],nativeLibraryDirectories=[/data/app/com.example.ihat3i.androidtesting-1/lib/arm, /vendor/lib, /system/lib]]
W/System.err: at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
W/System.err: at com.android.tools.fd.runtime.IncrementalClassLoader$DelegateClassLoader.findClass(IncrementalClassLoader.java:90)
W/System.err: at com.android.tools.fd.runtime.IncrementalClassLoader.findClass(IncrementalClassLoader.java:62)
W/System.err: at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
W/System.err: at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
W/System.err: ... 23 more
W/System.err: Suppressed: java.lang.ClassNotFoundException: bluetooth.LiteProject
W/System.err: at java.lang.Class.classForName(Native Method)
W/System.err: at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
W/System.err: at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
W/System.err: at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
W/System.err: ... 24 more
W/System.err: Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
这里是我的android代码,我收到了Object并反序列化了
public class BluetoothDataShare extends Thread {
private final BluetoothSocket mmSocket;
private static InputStream mmInStream;
private static OutputStream mmOutStream ;
public static LiteProject lite = null;
public BluetoothDataShare(BluetoothSocket socket) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the input and output streams, using temp objects because
// member streams are final
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "temp sockets not created");
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public static LiteProject deserialize() {
ObjectInputStream o = null;
LiteProject liteProject = null;
try {
o = new ObjectInputStream(mmInStream);
} catch (IOException e) {
e.printStackTrace();
}
try {
liteProject = (LiteProject) o.readObject();
o.close();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return liteProject;
}
/* Call this from the main activity to send data to the remote device */
public void write(byte[] bytes) {
try {
mmOutStream.write(bytes);
} catch (IOException e) { }
}
/* Call this from the main activity to shutdown the connection */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
Log.e(TAG, "close of connect socket failed");
}
}
}
这是我在电脑上序列化的代码
public static byte[] serialize(LiteProject liteProject){
ByteArrayOutputStream b = new ByteArrayOutputStream();
try(ObjectOutputStream o = new ObjectOutputStream(b)){
o.writeObject(liteProject);
}catch(IOException ex ){}
return b.toByteArray();
}
这是outputStream初始化的地方
private StreamConnection mConnection;
// Constant that indicate command from devices
private static final int EXIT_CMD = -1;
private static final int SWIPE_RIGHT = 1;
private static final int SWIPE_LEFT = 2;
public static OutputStream outputStream;
public ProcessConnectionThread(StreamConnection connection)
{
mConnection = connection;
}
@Override
public void run() {
try {
// prepare to receive data
InputStream inputStream = mConnection.openInputStream();
outputStream = mConnection.openOutputStream();
System.out.println("waiting for input");
while (true) {
int command = inputStream.read();
if (command == EXIT_CMD)
{
System.out.println("finish process");
break;
}
processCommand(command);
}
} catch (Exception e) {
e.printStackTrace();
}
}
这是输出流将序列化对象发送到Android设备的地方
if(isSelected()){
OutputStream outputStream = ProcessConnectionThread.outputStream;
if(outputStream != null){
Project project = projects.get(selectedIndex());
LiteProject liteProject = new LiteProject(project);
outputStream.write(ProjectMethods.serialize());
最后这是LiteProject类,两侧都是相同的(pc和android)
public class LiteProject implements Serializable {
private static final long serialVersionUID = 2L;
private int slideCount;
private String[] notes;
private int[] animationsCount;
private String name;
public LiteProject(Project project){
slideCount = project.getSlideCount();
notes = project.notes;
name = project.getName();
}
public String getName(){return name;}