应用程序正在崩溃共享首选项

时间:2014-12-23 16:15:56

标签: android eclipse

我正在尝试使用共享首选项在edittext中设置drawables,但我不知道哪里出错,它在活动开始时崩溃。

我希望它根据onClick of button更改drawable,将其保存到共享首选项(因此它将一直保留到用户再次更改),然后根据共享首选项更改drawable。

这是活动:

package com.wishlist.bow;

import android.app.ActionBar;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.EditText;


public class ActivitySetup extends ActionBarActivity {

public static final String MyPREFERENCES = "MyPrefs";
public static final String mena = "euro";

SharedPreferences menaPref = getApplicationContext().getSharedPreferences("MyPrefs", MODE_PRIVATE);
Editor editor = menaPref.edit();


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

    ActionBar bar = getActionBar();
    bar.setDisplayShowTitleEnabled(false);

    EditText edittextSrc = (EditText) findViewById(R.id.howMuch);

    if (menaPref.contains("dollar"))
    {
        edittextSrc.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.dollar, 0);
    }

    if (menaPref.contains("euro"))
    {
        edittextSrc.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.euro, 0);
    }

    if (menaPref.contains("koruny"))
    {
        edittextSrc.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.koruny, 0);
    }

    if (menaPref.contains("libra"))
    {
        edittextSrc.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.libra, 0);
    }
}

public void confirm(View view){

}

public void euro(View view){

    String euro = "euro";

    editor.putString(mena, euro);
    editor.commit();

}

public void dollar(View view){

    String dollar = "dollar";

    editor.putString(mena, dollar);
    editor.commit();

}

public void koruny(View view){

    String koruny = "koruny";

    editor.putString(mena, koruny);
    editor.commit();
}

public void libra(View view){

    String libra = "libra";

    editor.putString(mena, libra);
    editor.commit();

}
}

这是崩溃日志:

12-23 17:00:15.277: E/AndroidRuntime(9394): FATAL EXCEPTION: main
12-23 17:00:15.277: E/AndroidRuntime(9394): Process: com.wishlist.bow, PID: 9394
12-23 17:00:15.277: E/AndroidRuntime(9394): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.wishlist.bow/com.wishlist.bow.ActivitySetup}: java.lang.NullPointerException
12-23 17:00:15.277: E/AndroidRuntime(9394):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2163)
12-23 17:00:15.277: E/AndroidRuntime(9394):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2286)
12-23 17:00:15.277: E/AndroidRuntime(9394):     at android.app.ActivityThread.access$800(ActivityThread.java:144)
12-23 17:00:15.277: E/AndroidRuntime(9394):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
12-23 17:00:15.277: E/AndroidRuntime(9394):     at android.os.Handler.dispatchMessage(Handler.java:102)
12-23 17:00:15.277: E/AndroidRuntime(9394):     at android.os.Looper.loop(Looper.java:212)
12-23 17:00:15.277: E/AndroidRuntime(9394):     at android.app.ActivityThread.main(ActivityThread.java:5135)
12-23 17:00:15.277: E/AndroidRuntime(9394):     at java.lang.reflect.Method.invokeNative(Native Method)
12-23 17:00:15.277: E/AndroidRuntime(9394):     at java.lang.reflect.Method.invoke(Method.java:515)
12-23 17:00:15.277: E/AndroidRuntime(9394):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
12-23 17:00:15.277: E/AndroidRuntime(9394):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
12-23 17:00:15.277: E/AndroidRuntime(9394):     at dalvik.system.NativeStart.main(Native Method)
12-23 17:00:15.277: E/AndroidRuntime(9394): Caused by: java.lang.NullPointerException
12-23 17:00:15.277: E/AndroidRuntime(9394):     at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)
12-23 17:00:15.277: E/AndroidRuntime(9394):     at com.wishlist.bow.ActivitySetup.<init>(ActivitySetup.java:17)
12-23 17:00:15.277: E/AndroidRuntime(9394):     at java.lang.Class.newInstanceImpl(Native Method)
12-23 17:00:15.277: E/AndroidRuntime(9394):     at java.lang.Class.newInstance(Class.java:1208)
12-23 17:00:15.277: E/AndroidRuntime(9394):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
12-23 17:00:15.277: E/AndroidRuntime(9394):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2154)
12-23 17:00:15.277: E/AndroidRuntime(9394):     ... 11 more

3 个答案:

答案 0 :(得分:3)

您在方法可用之前调用方法。

把它放在班级

SharedPreferences menaPref;
Editor editor;

并在onCreate中使用此

menaPref = getApplicationContext().getSharedPreferences("MyPrefs", MODE_PRIVATE);
editor = menaPref.edit();

答案 1 :(得分:3)

这只是一个想法,但尝试移动

menaPref = getApplicationContext().getSharedPreferences("MyPrefs", MODE_PRIVATE);

到onCreate。

答案 2 :(得分:1)

getApplicationContext()在onCreate或onStart之后可用,但不是这种方式。请在onCreate中初始化您的sharedPreferences。

喜欢这个

SharedPreferences menaPref;
Editor editor;

public void onCreate(..){
    menaPref = getApplicationContext().getSharedPreferences("MyPrefs", MODE_PRIVATE);
    editor = menaPref.edit();
}