AsyncTask in TimerTask

  • 4 Antworten
  • Letztes Antwortdatum
F

Flocke123

Ambitioniertes Mitglied
4
Hallo, die Aufgabe ist eigentlich recht simple.
Ich möchte wiederholent einen AsyncTask in meiner MainActivity ausführen.

Code:
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]
[LEFT]public[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] onCreate(Bundle savedInstanceState) {
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]super[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].onCreate(savedInstanceState);
setContentView(R.layout.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]activity_icm[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);

[/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f] [/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT]
[/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// Set AsyncTask [/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT][U]ATgetData[/U] ATgD = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [U]ATgetData[/U]();

[/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f][/COLOR][/SIZE][/COLOR][/SIZE] 
[SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//set Timer[/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT][/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]timerGetData[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Timer();
[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]timerGetData[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].schedule( [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] tf_icm2(), 1000, 10000); [/LEFT]
 
}
 
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][LEFT]public[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]class[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] tf_icm2 [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]extends[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] TimerTask {

[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][/COLOR][/SIZE][/COLOR][/SIZE][/B] 
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] run() {

 
[U]ATgD[/U].execute(); 

[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][/COLOR][/SIZE][/COLOR][/SIZE][/B] 
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]class[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ATgetData [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]extends[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] AsyncTask<Void, Void, Void> {

[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][/COLOR][/SIZE][/COLOR][/SIZE][/B] 
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Void doInBackground(Void... arg0) { [/SIZE]
[SIZE=2][/SIZE] 
// getData
[SIZE=2] 
[/LEFT]
[/SIZE][SIZE=2][LEFT]


}

[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] onPostExecute() {
[/LEFT]
[/SIZE][SIZE=2][LEFT] 
// Update GUI
 
} [/LEFT]

[/SIZE][/SIZE]

In meiner onCreate markiert er mir die Instanzierung des AsyncTasks und ich hab keine Ahnung warum. Meldung "ATgetData cannot be resolved to a type".

Ist der Aufbau so wie ich ihn realisieren möchte überhaupt möglich?

Gruß Flo
 
versuch mal "private" vor die ATgetData klasse zu schreiben.

ansonsten seh ich da ein

publicvoid run()

fehlt da nicht ein leerzeichen? public void
 
swordi schrieb:
versuch mal "private" vor die ATgetData klasse zu schreiben.

"private" gibt er mir als illegale modifier an.

swordi schrieb:
ansonsten seh ich da ein

publicvoid run()

sorry Tippfehler :)

Instanziere ich die Klasse an der falschen Stelle?
Bei der Klasse meckert er auch "The Type ATgetData is never used localy".

Der ursprüngliche Beitrag von 14:05 Uhr wurde um 14:38 Uhr ergänzt:

So kann ich schonmal die Async Klasse instanzieren. Indem ich sie auserhalb der Timer Klasse codiere.
Code:
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]
[LEFT]class[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] tf_icm2 [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]extends[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] TimerTask {

[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] run() {[/SIZE][SIZE=2]
ATgetData ATgD = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ATgetData();
ATgD.execute(); [/SIZE][SIZE=2]

} 
}[/LEFT]

[/SIZE]

ohne Fehler. Nur leider auch ohne erfolg. Er bearbeitet nämlich nicht den AsyncTask

Code:
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]
[LEFT]class[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ATgetData [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]extends[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] AsyncTask<Void, Void, Void> {

[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] onPreExecute() {
[/SIZE][SIZE=2] 
}

[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Void doInBackground(Void... arg0) {

System.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]out[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].println([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"in Do in backround"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);

[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]return[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]null[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];


}

[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] onPostExecute() {
[/LEFT]
[/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f][COLOR=#000000]System.[/COLOR][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]out[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#000000].println([/COLOR][/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"in Post"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#000000]);[/COLOR][/SIZE]
}[/COLOR][/SIZE][/COLOR][/SIZE]
 
wenn "private" ein illegal modifier sein soll, dann bist irgendwie an der falschen stelle.

poste doch mal den gesamten code der klasse.
 
Wenn ich den Async Task auserhalb der Timer Klasse definiere funktionierts, Danke!
 
Zurück
Oben Unten