Wie übergibt man einen Intent von der onPostExecute() method.

T

the_time

Ambitioniertes Mitglied
0
Hallo,
ich versuch einen Intent von onPostExecute zu onLocationChanged Methode zu übergeben aber die App restartet sich hineinander und es ist dann halt schwerig sie zu beenden. Im Logcat scheint ab und zu diese Fehlermedlung unten. Ohne das Intent code funktioniert die app ganz gut. Ich habe den Intent teil im code durch Kommentare wie "Ich habe diesen Part auch hinzugefügt " gekennzeichnet.

Error:
PHP:
05-17 21:19:12.553: E/ActivityThread(19497): Performing stop of activity that is not resumed: {com.bustracker/com.bustracker.MainActivity}
05-17 21:19:12.553: E/ActivityThread(19497): java.lang.RuntimeException: Performing stop of activity that is not resumed: {com.bustracker/com.bustracker.MainActivity}
05-17 21:19:12.553: E/ActivityThread(19497): 	at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3822)
05-17 21:19:12.553: E/ActivityThread(19497): 	at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3910)
05-17 21:19:12.553: E/ActivityThread(19497): 	at android.app.ActivityThread.access$1200(ActivityThread.java:177)
05-17 21:19:12.553: E/ActivityThread(19497): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
05-17 21:19:12.553: E/ActivityThread(19497): 	at android.os.Handler.dispatchMessage(Handler.java:102)
05-17 21:19:12.553: E/ActivityThread(19497): 	at android.os.Looper.loop(Looper.java:145)
05-17 21:19:12.553: E/ActivityThread(19497): 	at android.app.ActivityThread.main(ActivityThread.java:5944)
05-17 21:19:12.553: E/ActivityThread(19497): 	at java.lang.reflect.Method.invoke(Native Method)
05-17 21:19:12.553: E/ActivityThread(19497): 	at java.lang.reflect.Method.invoke(Method.java:372)
05-17 21:19:12.553: E/ActivityThread(19497): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
05-17 21:19:12.553: E/ActivityThread(19497): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)

Beste Grüße,
the_time.

MainActiviy:

PHP:
public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
       // Ich habe this hinzugefügt//
        LocationListener ll = new myLocationListener(this);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 0, ll);

    }
      //Inner class in MainActivity
    class myLocationListener implements LocationListener {
        // Ich habe bContext and the constructor hinzugefügt//
        final Context bContext;

        public myLocationListener(Context context){
            bContext = context;
        }

        @Override
        public void onLocationChanged(Location location) {

                PostData sender = new PostData();
                // Ich habe  context  parameter hinzugefügt//
                sender.post_data(jSONString, bContext);

                 // Ich habe diesen Part auch hinzugefügt//
                Bundle extra = getIntent().getExtras();
                if (extras != null) {
                    ArrayList<Integer> c = extras
                            .getIntegerArrayList("stop_route");
                    for (int item : c) {
                        System.out.println("The Intent is not empty: "
                                + item);
                    }
                }               
        }

PostData class:
PHP:
public class PostData {
        String jSONString;
 //Ich habe here routes variable hinzugefügt//
       ArrayList<Integer> routes = new ArrayList<Integer>();

        public PostData() {
            super();

        }

        //Ich habe context hinzugefügt//
        public void post_data(String jSONString, Context context) {
            this.jSONString = jSONString;

        new MyAsyncTask(context).execute(jSONString);
        }
            //Inner class in ht PostData class.
        class MyAsyncTask extends AsyncTask<String, Integer, Void> {

            //Ich habe here context variable und den Konstruktor hinzugefügt//
            final Context mContext;

            public MyAsyncTask(Context context){
                mContext = context;
            }

            @Override
            protected Void doInBackground(String... params) {


                  routes = data.getRoutes();


                return null;

            }
            //Ich habe here diese Methode hinzugefügt//
                @Override
        protected void onPostExecute(Void result) {
            // Intent with Conetxt of the Asyntask class and
            Intent intent = new Intent(mContext, MainActivity.class);
            intent.putExtra("stop_route", routes);
            mContext.startActivity(intent);

        }

        }

    }
 
Zuletzt bearbeitet:
Hallo das hier liefert also eine NullPointer ex:

PHP:
Bundle extra = getIntent().getExtras(); 
                if (extras != null) { 
                    ArrayList<Integer> c = extras 
                            .getIntegerArrayList("stop_route"); 
                    for (int item : c) { 
                        System.out.println("The Intent is not empty: " 
                                + item);

c wird mit null überschrieben!

Hast du dich vertippt vllt? Weil dein Bundle object nennt sich extra...aber du versuchst die Arraylist aus "extras" zu kriegen!!!
 
Also ich kriege die Daten aus der ArrayList c in onLocationChanged() Methode aber die App restartet sich ständig bis sie einfriert. routes = data.getRoutes(); liefert diese ArrayList[31,7]

PHP:
05-17 21:24:04.929: I/System.out(19497): The Intent is not empty: 31
05-17 21:24:04.939: I/System.out(19497): The Intent is not empty: 7
05-17 21:24:04.939: I/System.out(19497): The Intent is not empty: 31
05-17 21:24:04.939: I/System.out(19497): The Intent is not empty: 7
05-17 21:24:04.939: I/System.out(19497): The Intent is not empty: 31
05-17 21:24:04.949: I/System.out(19497): The Intent is not empty: 7
05-17 21:24:04.949: I/System.out(19497): The Intent is not empty: 31
05-17 21:24:04.949: I/System.out(19497): The Intent is not empty: 7
05-17 21:24:04.949: I/System.out(19497): The Intent is not empty: 31
05-17 21:24:04.949: I/System.out(19497): The Intent is not empty: 7
05-17 21:24:04.949: I/System.out(19497): The Intent is not empty: 31
05-17 21:24:04.959: I/System.out(19497): The Intent is not empty: 7
05-17 21:24:04.959: I/System.out(19497): The Intent is not empty: 31
05-17 21:24:04.969: I/System.out(19497): The Intent is not empty: 7
05-17 21:24:04.969: I/System.out(19497): The Intent is not empty: 31
05-17 21:24:04.969: I/System.out(19497): The Intent is not empty: 7
05-17 21:24:04.969: I/System.out(19497): The Intent is not empty: 31
05-17 21:24:04.979: I/System.out(19497): The Intent is not empty: 7
05-17 21:24:04.979: I/System.out(19497): The Intent is not empty: 31
05-17 21:24:04.979: I/System.out(19497): The Intent is not empty: 7
05-17 21:24:04.979: I/System.out(19497): The Intent is not empty: 31
05-17 21:24:04.989: I/System.out(19497): The Intent is not empty: 7
05-17 21:24:04.989: I/System.out(19497): The Intent is not empty: 31
05-17 21:24:04.999: I/System.out(19497): The Intent is not empty: 7
05-17 21:24:04.999: I/System.out(19497): The Intent is not empty: 31

Es ist halt auch komisch dass soviel ausgegeben wird mehr als 50 auf einmal.
 
Zuletzt bearbeitet:
Ich habe es geändert aber ich habe am Anfang diese Fehlermeldung bekommen " java.lang.RuntimeException: Unable to instantiate application android.app.Application: java.lang.IllegalStateException: Unable to get package info for com.bustracker; is package not installed?"

Hat das damit zu tun dass sich die App immer wieder stratet dass ich nicht mit onStop und onResume in th MainActivity arbeite? Denn ich send Daten an den Server jede 3 sekunden um die App zu test sonst später jede 60 sekunden.
 
Zuletzt bearbeitet:
da gibts bestimmt noch ein paar mehr fehlermeldungen...dieser fehler kommt sehr oft wenn man seinen code nicht mit try catch blöcken bei gewissen stellen versieht

Der ursprüngliche Beitrag von 23:11 Uhr wurde um 23:11 Uhr ergänzt:

sorry aber den vorletzten satz habe ich überhaupt nicht verstanden
 
Ich meine das Hauptproblem von meiner App zur Zeit dass sie sich am Anfang immer wieder start.
Ich sende die Daten an den Server jede 3 Sekunden danach rufe ich AsynTask. Dort werden die Daten an den Server geschickt. Ich kriege eine Antwort vom Server welche ich wieder an die MainActivity zurückschicke. Die MainActivity startet dann nochmal und sendet wieder Daten.... Soll ich in meinem fall nicht mit onResume and onStop Methoden arbeiten?
 
Falls du diese Methoden manuell aufrufst: ja lass das lieber!
 
In deiner onPostExecute-Methode startest du die Activity ja immer wieder neu, kein Wunder gibts dann eine Endlosschleife ;)

Dein Ansatz mit dem Intent funktioniert so nicht. Wenn du den AsyncTask schon in deiner Activity benutzt, musst du keinen Umweg über Intents nehmen, um Daten zur Activity zu übergeben.

onPostExecute wird direkt auf dem UI-Thread ausgeführt, deshalb darfst du da auch direkt auf die Activity zugreifen. Dadurch hast du verschiedene Möglichkeiten, das Problem zu lösen.

Ich zeig dir mal ein Beispiel, das etwas komplex aussieht, aber meiner Meinung nach die sauberste Lösung ist:

Erstelle ein Callback-Interface:
Code:
public interface AsyncTaskCallback{
    void onAsyncTaskFinished(List<Integer> result);
}

Deine Activity implementiert dieses Interface und enthält dann den Code, der ausgeführt wird, wenn der AsyncTask fertig ist:

Code:
public class MainActivity extends ActionBarActivity implements AsyncTaskCallback {

    //...

    @Override
    public void onAsyncTaskFinished(List<Integer> result) {

        //Ausgabe der Resultate
        for (int item : result) {
            System.out.println("Resultat:  " + item);
        }
    }
}

Deinem AsyncTask gibts du dann im Konstruktor eine Objekt mit dem Interface mit:

Code:
public class PostData {

    //...

    private AsyncTaskCallback callback;

    public PostData(AsyncTaskCallback callback) {
        this.callback = callback;
    }
}

und in onPostExecute rufst du das Callback auf und übergibst der Activity damit das Resultat:

Code:
protected void onPostExecute(Void result) {
    //Callback aufrufen und die routes übergeben
    callback.onAsyncTaskFinished(routes);
}

dann musst du beim Aufruf des Tasks natürlich noch die Referenz zur Activity mitgeben:
Code:
PostData sender = new PostData(MainActivity.this);

Ganz kurz zusammengefasst passiert etwa folgendes:
  1. In onLocationChanged wird der AsyncTask gestartet
  2. Der AsyncTask ruft am Schluss die Interface-Methode auf
  3. Weil die MainActivity das Interface implementiert, wird MainActivity.onAsyncTaskFinished ausgeführt
  4. Innerhalb MainActivity.onAsyncTaskFinished kannst du nun alles tun, was passieren soll, wenn der AsyncTask fertig ist
 
Zuletzt bearbeitet:
  • Danke
Reaktionen: the_time
Danke für deine Antwort ich glaube ich werde deine umsetzen aber Es funktionierte heute morgen nachdem ich zu der MainActivity folgendes "android:launchMode="singleTask" in der Manifest hinzugefügt habe.

Was danach kommt habe ich auch geändert aber es funktionierte auch ohne diese Änderungen.

Ich habe auch folgende Methode in der MainActivity hinzugefügt:
PHP:
	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
                setIntent(intent);
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            ArrayList<Integer> routeList = extras
                    .getIntegerArrayList("stop_route");
            for (int item : routeList) {
               System.out.println("The Intent is not empty: " + item);
            	               
                
            }
        }
		
	}


PHP:
		@Override
		protected void onPostExecute(Void result) {
			// Intent with Conetxt of the Asyntask class and
			Intent intent = new Intent(mContext, MainActivity.class);
			intent.putIntegerArrayListExtra("stop_route", routes);
			intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
			mContext.startActivity(intent);

		}
 
Zuletzt bearbeitet:
Hallo,
ich habe deinen Ansatz oben angwendet aber ich habe ein Problem wenn die App im Background läuft dann werden die Daten nicht an die onAsyncTaskFinished Methode im MainActivity weitergegeben. Hat das einen Grund? Denn wenn die App im Vordergrund läuft hat sie keine Probleme mit der Übergabe.
 

Ähnliche Themen

M
Antworten
3
Aufrufe
162
moin
M
S
Antworten
7
Aufrufe
1.157
swa00
swa00
L
Antworten
15
Aufrufe
908
jogimuc
J
Zurück
Oben Unten