Forum: Mikrocontroller und Digitale Elektronik Globale Variable in Java (Android app)


von Tobi (Gast)


Lesenswert?

Hallo,

ich bin im moment dabei eine Android App für die Gegenseite eines 
ESP8266 zu Programmieren. Jedoch habe ich das Problem Daten aus einen 
Task, der für den Empfang der Daten zuständig ist (doInBackground async 
task), in eine fragmented Klasse zu bekommen. Ich habe schon mehrere 
Varianten versucht, leider funktionieren die nicht. Ich bekomme jedes 
mal die Fehlermeldung
1
Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to net.madebytobi.app.GlobalApplication
Ich hoffe hier kennst sich jemand mit java aus und kann mir 
weiterhelfen.

Mein Versuch:
In fragmented class:
1
GlobalApplication mApp = ((GlobalApplication)getActivity().getApplicationContext());
2
((TextView) getView().findViewById(R.id.cuttingspeed)).setText(Float.toString(mApp.get_overview_data(0)));
In doInBackground task:
1
GlobalApplication mApp = ((GlobalApplication)getApplicationContext());
2
overview_data[0] = ByteBuffer.wrap(input_register).order(ByteOrder.LITTLE_ENDIAN).getFloat();
The GlobalApplication class:
1
package net.madebytobi.app;
2
3
import android.app.Application;
4
5
/**
6
 * Created by Tobi on 21.01.2016.
7
 */
8
public class GlobalApplication extends Application {
9
    private float [] overview_data = new float [4];
10
    private String mGlobalVarValue;
11
12
    public float get_overview_data(int index) {
13
        return overview_data[index];
14
    }
15
16
    public void set_overview_data(float data, int index) {
17
        overview_data[index] = data;
18
    }
19
}

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.