2

I have a few issues I'd like to share with you guys.

All my files are on GITHUB

First one concerns the error that's in the title. FirstRun.java:82 refers to alert.show(); and FirstRun.java:63 to alertDialog.show();

The behaviour I'm observing goes as:

  1. The first alertDialog appears for a few seconds
  2. The application closes (not a force close)
  3. When I start to reboot the app, a blank screen is displayed (see following issues), meaning that my second activity is running. I'm using SharedPrefs to see whether or not one activity should be launched.

Complete logcat:

    32444-32444/carpedujourproductions.quickpronote E/WindowManager: Activity carpedujourproductions.quickpronote.FirstRun has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{967f7300 V.E..... R.....ID 0,0-729,594} that was originally added here
            android.view.WindowLeaked: Activity carpedujourproductions.quickpronote.FirstRun has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{967f7300 V.E..... R.....ID 0,0-729,594} that was originally added here
            at android.view.ViewRootImpl.<init>(ViewRootImpl.java:345)
            at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:239)
            at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
            at android.app.Dialog.show(Dialog.java:281)
            at carpedujourproductions.quickpronote.FirstRun.onCreate(FirstRun.java:63)
            at android.app.Activity.performCreate(Activity.java:5207)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2281)
            at android.app.ActivityThread.access$600(ActivityThread.java:148)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1263)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5124)
            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 de.robv.android.xposed.XposedBridge.main(XposedBridge.java:110)
            at dalvik.system.NativeStart.main(Native Method)  

32444-32444/carpedujourproductions.quickpronote E/WindowManager: Activity carpedujourproductions.quickpronote.FirstRun has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{968738b8 V.E..... R......D 0,0-729,585} that was originally added here
            android.view.WindowLeaked: Activity carpedujourproductions.quickpronote.FirstRun has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{968738b8 V.E..... R......D 0,0-729,585} that was originally added here
            at android.view.ViewRootImpl.<init>(ViewRootImpl.java:345)
            at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:239)
            at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
            at android.app.Dialog.show(Dialog.java:281)
            at android.app.AlertDialog$Builder.show(AlertDialog.java:951)
            at carpedujourproductions.quickpronote.FirstRun.onCreate(FirstRun.java:82)
            at android.app.Activity.performCreate(Activity.java:5207)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2281)
            at android.app.ActivityThread.access$600(ActivityThread.java:148)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1263)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5124)
            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 de.robv.android.xposed.XposedBridge.main(XposedBridge.java:110)
            at dalvik.system.NativeStart.main(Native Method)

Another thing:

SharedPrefs are doing strange things to me. After launching the app the first time, settings seem to have been saved and a (non-empty) file exists in /data/data/carpedujourproductions.quickpronote/shared_prefs/ though it can't be read by any of my root file explorers (ES; Solid)


Lastly:

I'm having a bit of a trouble to handle conditions. If you look at my MainActivity.java, Android Studio tells me the strings urlt & url are never used. How could I write them so that they would follow the order that is displayed on the file? (read etab_sel --> determine and write value to urlt --> read user_is --> determine and write value to url --> execute JS code with url as one of the variable.

BIG BIG thanks to anyone who can provide answsers on any of these. The three may be related somehow though.

Carp Fisherman
  • 141
  • 1
  • 3
  • 17

2 Answers2

4

Your problem stems from the 2 Spinners that you are using in FirstRun.java:

Spinner, Spinner2

Also, I cannot tell if the code inside onItemSelected() is the way you intend it to be:

@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
    switch (parent.getId()) {
    case R.id.spinner:
        etab_sel = parent.getItemAtPosition(pos).toString();
        Spinner2.setVisibility(View.VISIBLE);
    case R.id.spinner2:
        user_is = parent.getItemAtPosition(pos).toString();
        onStop();
    }
}

Because, currently, if an item is selected on Spinner, and case R.id.spinner is fired up, a fall through will happen and statements inside case R.id.spinner2 will also be executed, resulting in a call to onStop().

Is this the intended behavior? Or, did you just forget to place break; statements?

Moreover, in onStop(), you call finish(). And since alertDialog and dialog are showing at this time, you are greeted with the following messages:

Activity carpedujourproductions.quickpronote.FirstRun has leaked window

Activity carpedujourproductions.quickpronote.FirstRun has leaked window

Now, to make matters worse:

onItemSelected() is called when a Spinner is initialized.

So, as soon as you initialize R.id.spinner > onItemSelected() is called > case R.id.spinner > fallthrough > onStop() > editor.putBoolean("IsInitialized", true); > finish(); while alertDialog and alert are showing > leaked window

And since you call:

editor.putBoolean("IsInitialized", true);

in onStop(), FirstRun is never started on subsequent app launches:

(from MainActivity.java)

boolean IsInitialized = settings.getBoolean("IsInitialized", false);

if (IsInitialized == false) {
    Intent intent = new Intent(MainActivity.this, FirstRun.class);
    startActivity(intent);
    finish();
}

As you can see, there isn't just one fix here. You will have to rethink the logic aspect of FirstRun. Should you set IsInitialized to true if the user doesn't select data from spinners on first launch?

To get rid of the exceptions (for now), do the following:

// Declare two global boolean variables
boolean justStarted_1 = true;
boolean justStarted_2 = true;

....
....

@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {

    // Spinner is set; Ignore
    if (justStarted_1) {

        justStarted_1 = false;

    // Spinner2 is set; ignore
    } else if (justStarted_2) {

        justStarted_2 = false;

    // finally: here
    } else {
        switch (parent.getId()) {
        case R.id.spinner:
            etab_sel = parent.getItemAtPosition(pos).toString();
            Spinner2.setVisibility(View.VISIBLE);
            break;
        case R.id.spinner2:
            user_is = parent.getItemAtPosition(pos).toString();
            onStop();
            break;
        }
    }
}

Another thing: you'll have to keep uninstalling the app to see FirstRun. A simple fix for the time being is to use:

editor.putBoolean("IsInitialized", false);

Finish your work on FirstRun, replace false with true, do something else!

Vikram
  • 51,313
  • 11
  • 93
  • 122
  • I added what you recommended (forgot break; )and strated thinking of another way to display my windows, and tried your temp code, but it seems a bit off : I have the alertDialogs that keep coming up and the activity keeps reloading. Is there something you'd recommend me to use to wait each time that something is clicked and process the elements one by one? – Carp Fisherman Sep 02 '13 at 08:39
  • @CarpFisherman Have you updated your code at [GitHub](https://github.com/DaS4ilor/QuickPronote/)? – Vikram Sep 02 '13 at 08:59
  • So, yeah, actually it was just tat I didn't clear data before launching. I still get this when the process of registering all the variables is not sone (i.e. like activity is reloaded by changing phone orientation or quitting and relaunching). Maybe having an intermediate method where onStop() would only be called and IsInitialized set to true as all off the other strings are not empty? Anyways, thanks so far. Another thing though, I completed the whole process once and checked if the sharedprefs file was readable, it wasn't. Is it normal? – Carp Fisherman Sep 02 '13 at 10:49
  • @CarpFisherman `...checked if the sharedprefs file was readable...`: Read through the answers on this question: [Android: Viewing SharedPreferences file?](http://stackoverflow.com/questions/5951418/android-viewing-sharedpreferences-file). And, you should check if `(etab_sel, inputname, inputpass, manurl, user_is)` are all set(and valid) before setting `isInitialized` to `true`. If the conditions haven't been met, let the user keep seeing `FirstRun` on subsequent launches until s/he meets your app's requirements. Let me know, if I can improve my answer in any way. Good luck! – Vikram Sep 02 '13 at 11:07
  • Actually I used a root file explorer.I think I'll have to ask you help here before I mess everything up. So I did that, but the dialogs and activity kept launching over and over (due to the fact that I can't dismiss the `alertDialog`) so I tried replacing the alertDialogs with a toast for one, and a view for the other. Now, the app keeps crashing with NullPointerExceptions. I'd be so grateful if you could take a look at the [debug branch](https://github.com/DaS4ilor/QuickPronote/tree/debug/) You'll note that I tried what you've recommended - conditions. – Carp Fisherman Sep 02 '13 at 13:39
  • Normally I fixed the SharedPrefs issue that was just because of my strings were actually arrays. – Carp Fisherman Sep 02 '13 at 13:41
  • @CarpFisherman `if (etab_sel != "" && inputname != "" && inputpass != "" && user_is != "" && manurl == "")`: Strings are objects in Java/Android platform. To test equality on Objects, you use `someObject.equals(otherObject)`. `etab_sel != ""` is not correct. Use `etab_sel.equals("")`. You'll need to change the equality check for other Strings throughout your project. – Vikram Sep 02 '13 at 15:45
  • Still having a NPE. Located says it's `Spinner.setAdaptater(adaptater)` that's not working (`FirstRun.java:73`) – Carp Fisherman Sep 02 '13 at 16:26
  • @CarpFisherman So, I just took a look at [alert_dialog.xml](https://github.com/DaS4ilor/QuickPronote/blob/debug/res/layout/alert_dialog.xml). There are no `Spinners` defined in it. Because of this `Spinner = (Spinner) findViewById(R.id.spinner);` sets `Spinner` to `null`. Same for `Spinner2`. Hence, the NPE.I am guessing that you want to use `etab_select.xml` in `setContentView(R.layout.alert_dialog);`? – Vikram Sep 02 '13 at 16:42
  • ok, I merged the two vews to sort this out. But I keep getting that thing where the app restarts the activity again and again (I thought this was because of the alertDialog not being dismissed) Any thoughts? Basically, I think the app never launches `MainActivity.java`. I get no errors in logcat. (Commit pushed to branch master.) – Carp Fisherman Sep 02 '13 at 17:04
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/36668/discussion-between-user2558882-and-carp-fisherman) – Vikram Sep 02 '13 at 17:20
2

From your code i can see that you are finishing activity from your dialog to which you are passing the context of same activity. Your Dialog is having reference of activity which is finished.

AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
        alertDialog.setTitle("Remise \u00e0 z\u00e9ro");
        alertDialog.setMessage("Voulez-vous effacer tous vos param\u00e8tres (Identifiant, Mot de passe, type d\'accès de votre appareil?");
        alertDialog.setButton("Oui", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

                SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                SharedPreferences.Editor editor = settings.edit();
                editor.putBoolean("IsInitialized", false);
                editor.putString("etab", "");
                editor.putString("name", "");
                editor.putString("key", "");
                editor.putString("manurl", manurl);
                editor.putString("user_is", "");
                editor.commit();
                finish();

            }
        });
        alertDialog.setButton("Non", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int i) {
            }
        });
Siddhesh
  • 1,370
  • 11
  • 28
  • I didn't exactly get what you were saying. Besides, this is from the main activity while logcat states errors in the side activity `FirstRun.java`. In the code you quoted, the intention is to clear data and reload the activity. Are you suggesting to have a `alertDialog.dismiss();` there before `finish();` ? In my other activity I had `Intent intent = new Intent(FirstRun.this, MainActivity.class); startActivity(intent);` added. It results in no more closes, but the 2 dialogs don't appear long enough and the `MainActivty.java` is launched. – Carp Fisherman Aug 30 '13 at 15:14
  • yes `dismiss` your dialog before finishing your activity as you are passing `MainActivity`.this o `dialog` and finishing `MainActivity` without dismissing dialog which holds reference to current instance of `MainActivity` – Siddhesh Aug 31 '13 at 07:07
  • I just did that (going to commit to github soon with that), but the above comment still stands. The errors come from `FirstRun.java`, not `MainActivity`. Also, I detailled the new behaviour. – Carp Fisherman Aug 31 '13 at 11:37