为Seekbar实现监听器时的NPE

时间:2015-10-10 13:13:20

标签: android android-seekbar

我有一个按钮,当我点击它时打开一个带有搜索栏的自定义对话框。单击该按钮后,我的应用程序崩溃,抛出NullPointerException。这是我的代码:

Dialog customDialog = new Dialog(RoomActivity.this);
customDialog.setTitle("Set value");
customDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
customDialog.setContentView(R.layout.bar_layout);
customDialog.show();

SeekBar seekBar = (SeekBar)findViewById(R.id.valueBar);

seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        s.setState(String.valueOf(progress));
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
    }
});

堆栈跟踪:

10-10 18:33:56.649  23833-23833/com.myapp.hussainchachuliya.myapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.myapp.hussainchachuliya.myapp, PID: 23833
java.lang.NullPointerException
        at com.myapp.hussainchachuliya.myapp.Activities.RoomActivity$3.onLongClick(RoomActivity.java:180)
        at android.view.View.performLongClick(View.java:4505)
        at android.widget.TextView.performLongClick(TextView.java:8674)
        at android.view.View$CheckForLongPress.run(View.java:18767)
        at android.os.Handler.handleCallback(Handler.java:808)
        at android.os.Handler.dispatchMessage(Handler.java:103)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:5349)
        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:835)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:651)
        at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:136)
        at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

SeekBar seekBar = (SeekBar)customDialog.findViewById(R.id.valueBar);