当我调用显示对话框的方法时,会发生空指针异常

时间:2015-01-20 05:12:43

标签: java android click

我使用AmbilwarnaColorDialog来选择颜色。所以我包括了Ambilwarna库。我想通过单击图像打开颜色对话框。所以我只是叫下面的方法。但它显示我的空指针异常。谁能帮我? 我的代码:

int color;

 void openDialog(boolean supportsAlpha) {
    AmbilWarnaDialog dialog = new AmbilWarnaDialog(MainActivity.this,
            color, supportsAlpha,
            new AmbilWarnaDialog.OnAmbilWarnaListener() {

                @Override
                public void onOk(AmbilWarnaDialog dialog, int color) {
                    Toast.makeText(getApplicationContext(), "ok",
                            Toast.LENGTH_SHORT).show();
                    MainActivity.this.color = color;
                }

                @Override
                public void onCancel(AmbilWarnaDialog dialog) {
                    Toast.makeText(getApplicationContext(), "cancel",
                            Toast.LENGTH_SHORT).show();
                }
            });
    dialog.show();
}

我的导入是:

import yuku.ambilwarna.AmbilWarnaDialog;

LogCat显示如下:

01-20 10:54:56.260: E/AndroidRuntime(14407): FATAL EXCEPTION: main
01-20 10:54:56.260: E/AndroidRuntime(14407): java.lang.NullPointerException
01-20 10:54:56.260: E/AndroidRuntime(14407):    at  yuku.ambilwarna.AmbilWarnaDialog.<init>(AmbilWarnaDialog.java:84)
 01-20 10:54:56.260: E/AndroidRuntime(14407):   at com.example.demofordrawline.MainActivity.openDialog(MainActivity.java:178)
01-20 10:54:56.260: E/AndroidRuntime(14407):    at com.example.demofordrawline.MainActivity.onClick(MainActivity.java:251)
01-20 10:54:56.260: E/AndroidRuntime(14407):    at android.view.View.performClick(View.java:4212)
01-20 10:54:56.260: E/AndroidRuntime(14407):    at android.view.View$PerformClick.run(View.java:17476)
01-20 10:54:56.260: E/AndroidRuntime(14407):    at android.os.Handler.handleCallback(Handler.java:800)
01-20 10:54:56.260: E/AndroidRuntime(14407):    at android.os.Handler.dispatchMessage(Handler.java:100)
01-20 10:54:56.260: E/AndroidRuntime(14407):    at android.os.Looper.loop(Looper.java:194)
01-20 10:54:56.260: E/AndroidRuntime(14407):    at android.app.ActivityThread.main(ActivityThread.java:5371)
01-20 10:54:56.260: E/AndroidRuntime(14407):    at java.lang.reflect.Method.invokeNative(Native Method)
01-20 10:54:56.260: E/AndroidRuntime(14407):    at java.lang.reflect.Method.invoke(Method.java:525)
01-20 10:54:56.260: E/AndroidRuntime(14407):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
01-20 10:54:56.260: E/AndroidRuntime(14407):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
01-20 10:54:56.260: E/AndroidRuntime(14407):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

试试这段代码

        package yuku.ambilwarna.demo;

    import yuku.ambilwarna.AmbilWarnaDialog;
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;

    public class AmbilWarnaDemoActivity extends Activity {
        int color = 0xffffff00;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            final Button button = (Button) findViewById(R.id.button1);

            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    openDialog();
                }
            });
        }

        void openDialog() {
            AmbilWarnaDialog dialog = new AmbilWarnaDialog(
                    AmbilWarnaDemoActivity.this, color,
                    new AmbilWarnaDialog.OnAmbilWarnaListener() {
                        @Override
                        public void onOk(AmbilWarnaDialog dialog, int color) {

                            AmbilWarnaDemoActivity.this.color = color;
                            Toast.makeText(getApplicationContext(),
                                    "Color is " + color, Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onCancel(AmbilWarnaDialog dialog) {
                            Toast.makeText(getApplicationContext(), "cancel",
                                    Toast.LENGTH_SHORT).show();
                        }
                    });
            dialog.show();
        }
    }