ShowDialog onClick偏好

时间:2013-11-11 17:15:24

标签: java android xml

我有一个PreferenceActivity,其中有一个偏好。我想点击这个Preference会在另一个班级打开一个Dialog。 Brightness类包含要呈现的对话框。这是代码。

// This is the PreferenceActivity Class (Settings)
Preference bright;
bright = (Preference) this.findPreference("bri");
bright.setOnPreferenceClickListener( new OnPreferenceClickListener() {
            public boolean onPreferenceClick(Preference preference) {

                Brightness brightness = new Brightness();
                brightness.ShowDialog();

                return true;

            }
            });

亮度等级

public class Brightness extends Activity{

    private SeekBar brightbar;
    private int brightness;
    @SuppressWarnings("unused")
    private ContentResolver cResolver;
    private Window window;
    TextView txtPerc;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ShowDialog();

    }


    public void ShowDialog() {
    final AlertDialog.Builder popDialog = new AlertDialog.Builder(getApplicationContext());
    final LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
    final View Viewlayout = inflater.inflate(R.layout.dialog_brightness, (ViewGroup) findViewById(R.id.layout_dialog));  

        popDialog.setTitle("Set Brightness");
        popDialog.setView(Viewlayout);

    brightbar = (SeekBar) findViewById(R.id.brightbar);
    txtPerc = (TextView) findViewById(R.id.txtPercentage);
    cResolver = getContentResolver();
    window = getWindow();
    brightbar.setMax(255);
    brightbar.setKeyProgressIncrement(1);

    brightbar = (SeekBar) findViewById(R.id.brightbar);
    txtPerc = (TextView) findViewById(R.id.txtPercentage);
    cResolver = getContentResolver();
    window = getWindow();
    brightbar.setMax(255);
    brightbar.setKeyProgressIncrement(1);

    try
    {
        brightness = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
        float perc = (brightness /(float)255)*100;
        txtPerc.setText((int)perc +" %");
    }
    catch (SettingNotFoundException e)
    {
        Log.e("Error", "Cannot access system brightness");
        e.printStackTrace();
    }

    brightbar.setProgress(brightness);
    brightbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
    {
        public void onStopTrackingTouch(SeekBar seekBar)
        {
            Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, brightness);
            LayoutParams layoutpars = window.getAttributes();
            layoutpars.screenBrightness = brightness / (float)255;
            window.setAttributes(layoutpars);
        }

        public void onStartTrackingTouch(SeekBar seekBar)
        {

        }

        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
        {


                brightness = progress;

                float perc = (brightness /(float)255)*100;
                txtPerc.setText((int)perc +" %");
        }
    });     

    popDialog.setNeutralButton("Close",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }

            });

    popDialog.create();
    popDialog.show();

}
}

问题是,当我点击此偏好时,应用程序崩溃了!这是logcat

  

11-11 18:03:57.788:E / AndroidRuntime(18672):致命异常:主要   11-11 18:03:57.788:E / AndroidRuntime(18672):   java.lang.NullPointerException 11-11 18:03:57.788:   E / AndroidRuntime(18672):at   android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)   11-11 18:03:57.788:E / AndroidRuntime(18672):at   com.packagename.Brightness.ShowDialog(Brightness.java:39)11-11   18:03:57.788:E / AndroidRuntime(18672):at   com.packagename.Settings $ 1.onPreferenceClick(Settings.java:48)11-11   18:03:57.788:E / AndroidRuntime(18672):at   android.preference.Preference.performClick(Preference.java:952)11-11   18:03:57.788:E / AndroidRuntime(18672):at   android.preference.PreferenceScreen.onItemClick(PreferenceScreen.java:215)   11-11 18:03:57.788:E / AndroidRuntime(18672):at   android.widget.AdapterView.performItemClick(AdapterView.java:297)   11-11 18:03:57.788:E / AndroidRuntime(18672):at   android.widget.AbsListView.performItemClick(AbsListView.java:1100)   11-11 18:03:57.788:E / AndroidRuntime(18672):at   android.widget.AbsListView $ PerformClick.run(AbsListView.java:2788)   11-11 18:03:57.788:E / AndroidRuntime(18672):at   android.widget.AbsListView $ 1.run(AbsListView.java:3463)11-11   18:03:57.788:E / AndroidRuntime(18672):at   android.os.Handler.handleCallback(Handler.java:730)11-11   18:03:57.788:E / AndroidRuntime(18672):at   android.os.Handler.dispatchMessage(Handler.java:92)11-11   18:03:57.788:E / AndroidRuntime(18672):at   android.os.Looper.loop(Looper.java:137)11-11 18:03:57.788:   E / AndroidRuntime(18672):at   android.app.ActivityThread.main(ActivityThread.java:5289)11-11   18:03:57.788:E / AndroidRuntime(18672):at   java.lang.reflect.Method.invokeNative(Native Method)11-11   18:03:57.788:E / AndroidRuntime(18672):at   java.lang.reflect.Method.invoke(Method.java:525)11-11 18:03:57.788:   E / AndroidRuntime(18672):at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:739)   11-11 18:03:57.788:E / AndroidRuntime(18672):at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)11-11   18:03:57.788:E / AndroidRuntime(18672):at   dalvik.system.NativeStart.main(原生方法)

2 个答案:

答案 0 :(得分:0)

要显示对话框,您必须从对话框或片段扩展亮度,而不是活动。

答案 1 :(得分:0)

此处活动代码:

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MyDialog dialog = new MyDialog();
        Bundle arguments = new Bundle();
        arguments.putString(MyDialog.ARGS, "Message");
        dialog.setArguments(arguments);
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(dialog, dialog.getTag());
        ft.commitAllowingStateLoss();                       
    }
}

和对话框:

public class MyDialog extends DialogFragment {
    public static final String ARGS = "ARGS";

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        Bundle args = getArguments();
        String message;
        if (args.containsKey(ARGS)) message = args.getString(ARGS); else message = "Sorry";
           builder.setTitle("Title")
           .setMessage(message)
           .setNeutralButton("OK", null);
            return builder.create();
    }
}

如何将它与PrefernceActivity一起使用,你只需要思考一下。