Frage

I'm using Lars Werkman's HoloColorPicker in my app, and would like to save the color and reuse it in other activities.

Should a color value be saved as string, int or something else? I have tried string and int, but with no sucess.

I don't know much about programming, so try to explain as thoroughly as possible :)

HoloColorPicker - GitHub

picker.getColor(); returns an ARGB value, how can I save that value and use it somewhere else?

@Override
public void onColorChanged(int color) {
    //gives the color when it's changed.

    colorValue = Integer.valueOf(picker.getColor());

    editor.clear();
    editor.putInt("themes_colorcode", colorValue);
    editor.commit();
}

The code below should work in onCreate, right?

    actionBar.setBackgroundDrawable(new ColorDrawable(colorValue));

But I get this error:

Process: com.okramuf.musikteori, PID: 28274
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.okramuf.musikteori/com.okramuf.musikteori.settings}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.okramuf.musikteori.settings.onCreate(settings.java:51)
        at android.app.Activity.performCreate(Activity.java:5231)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
War es hilfreich?

Lösung 2

This code works:

I added

private static int colorValue = 0;

and

@Override
public void onColorChanged(int color) {
    //gives the color when it's changed.

    colorValue = picker.getColor();

    editor.clear();
    editor.putInt("themes_colorcode", colorValue);
    editor.commit();
}

Andere Tipps

in android you want to seperate all your resources like Strings , colors , dimen(size of text), drawables etc. outside of your code , we do it to give our code more flexibility . for example if you want to change the color of your red color in your app to light red you simply change it in your resources in one place and it affect your whole app... when you open a project in android notice you have a res folder. inside it you will find values folder. stand on values folder and right click your button. add an xml file and call it colors. you can take a look at this site , it's a nice one:

http://www.rapidtables.com/web/color/RGB_Color.htm

you can pick a color you want and you will see it's value .for example #FFFFFF for white. now you need to open the file you created and press ctrl+space and add a color. it will be something like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <color name="opaque_red">#FF0000</color>
</resources>

to get a color from your resource you can call it from your Activity like this: write this in onCreate():

Recources res = getResources();
TextView tv = (TextView)findViewById(R.id.my_tv);
tv.setBackgroundResource(R.color.opaque_red);

or you can define it in your xml layout like this:

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:textColor="@color/opaque_red"
    android:text="@string/hello" />

hope it answered your question...

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top