我编写了一个应用程序,该应用程序通过蓝牙从 arduino 获取传感器数据。
只要蓝牙套接字可用,一切正常。如果我关闭 arduino 并尝试打开应用程序,那么我的应用程序就会崩溃。
public class GetData extends Activity {
TextView t1, t2, t3, t4, t5, t6, t7, t8;
ImageView schulter,ellenbogen,greifer,pfeil;
long cb1, cb2, m1, m2, m3, m4, m5, m6;
long[] motor = new long[8];
Handler handler = new Handler(); //Assistent zur Ausführung
long timer = 300; //Millisekunden
static final double PI = 3.141592653589793 ;
InputStream inStream = null;
BluetoothSocket btSocket = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_get_data);
runnable.run();
}
public Runnable runnable = new Runnable() {
@Override
public void run() {
referenceXML();
getDatafromBT();
setLongfromBT();
if(((motor[0] == 2) && (motor[7] == 3)) || ((motor[0] == 4) && (motor[7] == 3))) {
setViews();
setImageViews();
XMLbewegungSchulter();
XMLbewegungEllenbogen();
XMLbewegungGreifer();
}
handler.postDelayed(runnable,timer);
}
};
这是BtConnect.class
:
public class BtConnect extends Application {
....
....
....
public long[] getData() {
int bytesAvailable = 0;
long[] results = new long[8];
try {
bytesAvailable = inStream.available();
if (btSocket.isConnected() && bytesAvailable != 0) { // Prüft ob Verbindung noch besteht und Daten verfügbar sind
String StringBuffer = "";
byte[] inByte = new byte[1];
for (int i = 0; i < 8; i++) {
while (1 == 1) {
inStream.read(inByte);
if (inByte[0] == ',') {
results[i] = Long.parseLong(StringBuffer, 10); // Zeichenkette in Zahlen wandeln
//Toast.makeText(mContext, "Ausgabe von inStream: " + results[i] + "\n", Toast.LENGTH_SHORT).show();
break;
} else {
StringBuffer += (char) inByte[0]; //Zeichenkette wird solange dazuaddiert, bis ein Komma von inByte[0] bestätigt wird
}
}
StringBuffer = "";
}
return results;
}
} catch (IOException e) {
return new long[8];
}
return new long[8];
}
}
我知道我的问题只有在我启用时才会出现getDatafromBT()
。如果我禁用它,那么我的应用程序将毫无问题地启动,但我当然不会得到任何数据。
public void getDatafromBT() {
BtConnect btConnect = (BtConnect) getApplication();
motor = btConnect.getData();
}
你是说这个堆栈跟踪吗?对不起,我的英语不是那么好。
java.lang.RuntimeException: Unable to create application de.hs.karlsruhe.hskabot.ws13.BtConnect: java.lang.ClassCastException: de.hs.karlsruhe.hskabot.ws13.BtConnect cannot be cast to de.hs.karlsruhe.hskabot.ws13.GetData
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4447)
at android.app.ActivityThread.access$1300(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: de.hs.karlsruhe.hskabot.ws13.BtConnect cannot be cast to de.hs.karlsruhe.hskabot.ws13.GetData
at de.hs.karlsruhe.hskabot.ws13.BtConnect.onCreate(BtConnect.java:79)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1007)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4444)
at android.app.ActivityThread.access$1300(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)