共享首选ifelse语句

时间:2017-10-09 14:33:21

标签: java android if-statement sharedpreferences

我使用if-else语句和共享首选项,但它崩溃了。 我的代码:

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);
            }
        }
    });

当" skulestring"并不等于"快速交替..."它工作,它启动新的活动。但当它等于" Vel eit alternativ .."时,应用程序崩溃。

我的错误日志:

    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) 

感谢您的帮助! :)

2 个答案:

答案 0 :(得分:0)

首先,使用equals而不是==来比较Strings对象:

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);
}

并且在调用firebase路径时错误看起来像是sencondActivity

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

也许你可以向我们展示这一部分。 此致!

答案 1 :(得分:0)

异常堆栈跟踪中有一条消息,明确指出

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

因此,它可能会崩溃,因为您的Firebase路径包含无效字符...