使用SharedPreferences.contains()

时间:2016-10-06 10:56:13

标签: java android sharedpreferences android-sharedpreferences

首先,我想说对不起,如果我的英语不太正确,因为我是德国人。还有一些类/方法/变量名称是德语。

我的问题是我的应用程序总是在strtup崩溃。我已经发现(通过评论不同的东西),因为我在onResume()方法中的if条件,但我无法解决这个问题。

当应用程序完成后,它的意思是,告诉我,当天我要带什么去学校。当然还会有一些额外的/酷的功能。

我已经检查过,所有活动都有匹配的布局。

以下是我的启动器活动代码:

package de.mbauer.schulplaner;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

public class Main extends AppCompatActivity {

static SharedPreferences fachPrefs;
static SharedPreferences.Editor fachPrefEdit;
Intent keinFach;

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

    fachPrefs = this.getSharedPreferences("FaecherDatei", Context.MODE_PRIVATE);
    fachPrefEdit = fachPrefs.edit();

    if (!fachPrefs.contains("Fach0")) {
        keinFach = new Intent(Main.this, KeinFachVorhanden.class);
        startActivity(keinFach);
    }
}
@Override
protected void onDestroy() {
    super.onDestroy();
    fachPrefEdit.clear();
    fachPrefEdit.commit();
    }
}

这是Activity,由onResume()中的Intent调用:

package de.mbauer.schulplaner;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class KeinFachVorhanden extends AppCompatActivity implements View.OnClickListener {
    TextView tv1;
    Button btn1;
    Intent faechererstellenOeffnen = new Intent(KeinFachVorhanden.this, FaecherErstellen.class );

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

    btn1 = (Button) findViewById(R.id.button);
    btn1.setOnClickListener(this);
    tv1 = (TextView) findViewById(R.id.textView);
}

@Override
public void onClick(View v) {
    startActivity(faechererstellenOeffnen);
}
}

这是xml Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.mbauer.schulplaner">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".Main">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".FaecherErstellen" />
    <activity android:name=".KeinFachVorhanden"></activity>
</application>

这是LogCat:

10-06 12:08:45.648 13621-13621/? I/art: Late-enabling -Xcheck:jni
10-06 12:08:45.747 13621-13621/de.mbauer.schulplaner W/System: ClassLoader           referenced unknown path: /data/app/de.mbauer.schulplaner-2/lib/arm64
10-06 12:08:45.754 13621-13621/de.mbauer.schulplaner I/InstantRun: Instant Run Runtime started. Android package is de.mbauer.schulplaner, real application class is null.
10-06 12:08:45.972 13621-13621/de.mbauer.schulplaner W/System: ClassLoader referenced unknown path: /data/app/de.mbauer.schulplaner-2/lib/arm64
10-06 12:08:46.170 13621-13621/de.mbauer.schulplaner W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
10-06 12:08:46.409 13621-13621/de.mbauer.schulplaner I/HwSecImmHelper: mSecurityInputMethodService is null
10-06 12:08:46.419 13621-13621/de.mbauer.schulplaner E/HAL: load: id=gralloc != hmi->id=gralloc
10-06 12:08:46.506 13621-13621/de.mbauer.schulplaner I/Process: Sending signal. PID: 13621 SIG: 9

堆栈跟踪:

$ adb shell am start -n "de.mbauer.schulplaner/de.mbauer.schulplaner.Main" -    a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 17199 on device huawei-ale_l21-QLF7N16307003297
W/System: ClassLoader referenced unknown path:    /data/app/de.mbauer.schulplaner-1/lib/arm64
I/InstantRun: Instant Run Runtime started. Android package is    de.mbauer.schulplaner, real application class is null.
W/System: ClassLoader referenced unknown path: /data/app/de.mbauer.schulplaner-1/lib/arm64
W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter   android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android. graphics.PorterDuffColorFilter, android.content.res.ColorStateList,  android.graphics.PorterDuff$Mode) would have incorrectly overridden the package- private method in android.graphics.drawable.Drawable
I/HwSecImmHelper: mSecurityInputMethodService is null
E/HAL: load: id=gralloc != hmi->id=gralloc
E/HAL: load: id=gralloc != hmi->id=gralloc
I/OpenGLRenderer: Initialized EGL, version 1.4
W/OpenGLRenderer: load: so=/system/lib64/libhwuibp.so
              dlopen failed: library "/system/lib64/libhwuibp.so" not found
W/OpenGLRenderer: Initialize Binary Program Cache: Load Failed
E/HAL: load: id=gralloc != hmi->id=gralloc
I/Process: Sending signal. PID: 17199 SIG: 9
Application terminated.    

Gradle Build:

// Top-level build file where you can add configuration options common to   all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

我希望,你可以帮助我。

度过愉快的一天:)

1 个答案:

答案 0 :(得分:2)

您应该像这样获得共享偏好

**,asads
sasd,*,asa,*

现在,如果在共享首选项中有hospitalJson,它将返回字符串,否则字符串将为null。然后你可以检查它是否为null,如下面的代码: -

SharedPreference pref = getApplicationContext().getSharedPreferences("CachedResponse", 0);

String hospitalJson = pref.getString("hospitalJson", null);