-2

i programming an app, that gets sensor data from arduino via bluetooth.

Everything is working as long the Bluetooth Socket is available. If I switch off arduino and try to open the app then my app crash.

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);
        }
    };

And here is the 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];
}

}

I know that my problem appear only if I enable getDatafromBT(). If I disable it then my app start without problem, but of course I won't get any data.

    public void getDatafromBT() {

    BtConnect btConnect = (BtConnect) getApplication();
    motor = btConnect.getData();
}

Do you mean this stacktrace? Sorry my english is not that good.

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)
Steinfeld
  • 75
  • 1
  • 2
  • 13
  • What happened when you debugged? Where does the code crash? What log statements are printed? What is in the print statements? What do you expect vs. what happens and when? What have you tried? – Kon Apr 02 '15 at 14:56
  • It helps if you share the **stack trace** http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors – EpicPandaForce Apr 02 '15 at 15:04

1 Answers1

0

You're getting attribute from null object. So NPE. if you don't want to get exception do an if-else check:

public void getDatafromBT() {
  BtConnect btConnect = (BtConnect) getApplication();
  if (btConnect != null) 
    motor = btConnect.getData();
  else 
    // toast, close app or anything you need app does...
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109