-2

I'm using an if-else statement together with a sharedpreference, but it crashes. My code:

button = (Button) findViewById(R.id.Ferdig1);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            SharedPreferences skuule = getSharedPreferences("skule", Context.MODE_PRIVATE);
            String skuleString = skuule.getString("skule", "");

            if (skuleString == "Vel eit alternativ..") {
                Toast.makeText(MainActivity.this, "Vel eit alternativ...", Toast.LENGTH_SHORT).show();
            }
            else if (skuleString != "Vel eit alternativ..") {
                Intent myIntent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(myIntent);
            }
        }
    });

When "skulestring" isn't equal to "Vel eit alternativ.." it works and it starts the new Activity. But when it is equal to "Vel eit alternativ..", the app crashes.

My ERROR log:

    10-09 16:31:53.543 15146-15146/com.heli.minvekeplan E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.heli.minvekeplan, PID: 15146
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.heli.minvekeplan/com.heli.minvekeplan.SecondActivity}: com.google.firebase.database.DatabaseException: Invalid Firebase Database path: Vel eit alternativ.... Firebase Database paths must not contain '.', '#', '$', '[', or ']'
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)
    at android.app.ActivityThread.access$800(ActivityThread.java:156)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:211)
    at android.app.ActivityThread.main(ActivityThread.java:5389)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
 Caused by: com.google.firebase.database.DatabaseException: Invalid Firebase Database path: Vel eit alternativ.... Firebase Database paths must not contain '.', '#', '$', '[', or ']'
    at com.google.android.gms.internal.zzbqh.zzjm(Unknown Source)
    at com.google.firebase.database.DatabaseReference.child(Unknown Source)
    at com.heli.minvekeplan.SecondActivity.onCreate(SecondActivity.java:82)
    at android.app.Activity.performCreate(Activity.java:5990)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442) 
    at android.app.ActivityThread.access$800(ActivityThread.java:156) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:211) 
    at android.app.ActivityThread.main(ActivityThread.java:5389) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815) 

Thank you for helping! :)

user1506104
  • 6,554
  • 4
  • 71
  • 89
Kjell Ove
  • 21
  • 1
  • 10

2 Answers2

0

First at all, use equals instead of == to compare Strings objects:

if (skuleString.equals("Vel eit alternativ..")) {
    Toast.makeText(MainActivity.this, "Vel eit alternativ...", Toast.LENGTH_SHORT).show();
}
else {
    Intent myIntent = new Intent(MainActivity.this, SecondActivity.class);
    startActivity(myIntent);
}

and the error looks like the sencondActivity when is calling a firebase path

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.heli.minvekeplan/com.heli.minvekeplan.SecondActivity}: com.google.firebase.database.DatabaseException: Invalid Firebase Database path:

Maybe you can show us this part. Regards!

user1506104
  • 6,554
  • 4
  • 71
  • 89
Chelo
  • 443
  • 4
  • 7
0

There is a message in the exception stack trace, that clearly points out

Caused by: com.google.firebase.database.DatabaseException: Invalid Firebase Database path: Vel eit alternativ.... Firebase Database paths must not contain '.', '#', '$', '[', or ']'

Hence, it might crash because your Firebase path contains invalid characters…

deHaar
  • 17,687
  • 10
  • 38
  • 51