ColorPicker不显示对话框

时间:2015-04-08 23:53:18

标签: java android

我正在制作一个颜色选择器应用程序。我已经完成了布局,但是一旦按下“更改背景”按钮,它就不会加载对话框(颜色选择器)。我只是想知道如何显示它并使我的应用程序正常工作,谢谢。这是我的主要活动代码。我不认为你真的需要看到其他类,因为它们都是复制和粘贴代码,但如果需要,我愿意提供它们。

MainActivity.java

package com.miller.colorpicker;

import android.app.Activity; 
import android.content.Context; 
import android.content.SharedPreferences; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.RelativeLayout;

    public class MainActivity extends Activity {

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

            SharedPreferences sharedPref = getSharedPreferences("",Context.MODE_PRIVATE);
            int colour = sharedPref.getInt("colour", Color.WHITE);
            RelativeLayout parent = (RelativeLayout) findViewById(R.id.layout);
            parent.setBackgroundColor(colour);

            final Button button = (Button) findViewById(R.id.button_text);
            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    // Perform action on click

                }
            });
        } }

04-08 22:16:59.631: E/AndroidRuntime(1144): FATAL EXCEPTION: main
04-08 22:16:59.631: E/AndroidRuntime(1144): Process: com.miller.colorpicker, PID: 1144
04-08 22:16:59.631: E/AndroidRuntime(1144): java.lang.NullPointerException
04-08 22:16:59.631: E/AndroidRuntime(1144):     at com.miller.colorpicker.MainActivity$1.onClick(MainActivity.java:53)
04-08 22:16:59.631: E/AndroidRuntime(1144):     at android.view.View.performClick(View.java:4438)
04-08 22:16:59.631: E/AndroidRuntime(1144):     at android.view.View$PerformClick.run(View.java:18422)
04-08 22:16:59.631: E/AndroidRuntime(1144):     at android.os.Handler.handleCallback(Handler.java:733)
04-08 22:16:59.631: E/AndroidRuntime(1144):     at android.os.Handler.dispatchMessage(Handler.java:95)
04-08 22:16:59.631: E/AndroidRuntime(1144):     at android.os.Looper.loop(Looper.java:136)
04-08 22:16:59.631: E/AndroidRuntime(1144):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-08 22:16:59.631: E/AndroidRuntime(1144):     at java.lang.reflect.Method.invokeNative(Native Method)
04-08 22:16:59.631: E/AndroidRuntime(1144):     at java.lang.reflect.Method.invoke(Method.java:515)
04-08 22:16:59.631: E/AndroidRuntime(1144):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-08 22:16:59.631: E/AndroidRuntime(1144):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-08 22:16:59.631: E/AndroidRuntime(1144):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

之前我使用过此示例: https://github.com/lomza/android-color-picker

// create OnAmbilWarnaListener instance
// new color can be retrieved in onOk() event
OnAmbilWarnaListener onAmbilWarnaListener = new OnAmbilWarnaListener() {
    @Override
    public void onCancel(AmbilWarnaDialogFragment dialogFragment) {
        Log.d("TAG", "onCancel()");
    }

    @Override
    public void onOk(AmbilWarnaDialogFragment dialogFragment, int color) {
        Log.d("TAG", "onOk(). Color: " + color);

        MainActivity.this.mColor = color;
    }
};

// create new instance of AmbilWarnaDialogFragment and set OnAmbilWarnaListener listener to it
// show dialog fragment with some tag value
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
AmbilWarnaDialogFragment fragment = AmbilWarnaDialogFragment.newInstance(mColor);
fragment.setOnAmbilWarnaListener(onAmbilWarnaListener);

fragment.show(ft, "color_picker_dialog");