0

我有一组单选按钮,如下面的代码按钮所示。当我单击每个单选按钮时,我想要一个不同的对话框,如下图所示。每个单选按钮一个。这些应该在对话框中。我该怎么做呢? 最后,我只需要生成这些自定义对话框。任何人都可以帮助我如何做到这一点?

自定义对话框视图

我也得到一个空指针异常。我该如何解决?

public class Availability extends Activity implements OnClickListener,
        OnCheckedChangeListener {
    ToggleButton override;
    TextView status;
    Button Overridestatus;
    CharSequence[] choice = { "RemoteOffice", "Office", "Personal mobile",
            "Home", "Mobile", "Temporary", "Do not Disturb",
            "Working Remotely", "Non work day", "Work Day" };
    RadioGroup timeperiod;
    TimePicker timePicker;
    int hour, minute;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.availability);
        override = (ToggleButton) findViewById(R.id.tbOver);
        override.setOnClickListener(this);
        status = (TextView) findViewById(R.id.tvstatus);
        Overridestatus = (Button) findViewById(R.id.bstatus);
        Overridestatus.setOnClickListener(this);
        timeperiod = (RadioGroup) findViewById(R.id.rgtimeperiod);
        timeperiod.setOnCheckedChangeListener(this);
        timePicker = (TimePicker) findViewById(R.id.timePicker);
        timePicker.setIs24HourView(true);

    }

    public void onClick(View view) {
        // TODO Auto-generated method stub
        switch (view.getId()) {
        case R.id.tbOver:

            if (override.isChecked()) {

                status.setText("Override");
                Overridestatus.setText("Personal Mobile");

            } else {
                status.setText("Currently");
                Overridestatus.setText("Use Schedule - Do Not Disturb");

            }
            break;

        case R.id.bstatus:

        {
            if (override.isChecked()) {
                showDialog(0);
            }
        }

            break;
        case R.id.rhrandmin:
        }
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        // TODO Auto-generated method stub
        switch (id) {
        case 0:
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Select System Server");
            builder.setIcon(R.drawable.ic_btn_round_more_normal);
            builder.setPositiveButton("OK",
                    new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int id) {
                            // TODO Auto-generated method stub
                            Intent menu = new Intent(Availability.this,
                                    MenuScreen.class);
                            startActivity(menu);

                        }
                    });
            builder.setNegativeButton("CANCEL",
                    new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub

                        }
                    });

            builder.setSingleChoiceItems(choice, -1,
                    new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int item) {
                            // TODO Auto-generated method stub

                        }
                    });

            builder.create();
            builder.show();
            break;
        case 2:
             new TimePickerDialog(this, mTimeSetListener, hour, minute,false);
            break;
        }

        return null;

    }

    private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
        public void onTimeSet(TimePicker view, int hourOfDay, int minuteOfHour) {
            hour = hourOfDay;
            minute = minuteOfHour;
            Toast.makeText(getBaseContext(),
                    "You have selected : " + hour + ":" + minute,
                    Toast.LENGTH_SHORT).show();
        }
    };

    public void onCheckedChanged(RadioGroup group, int checkedId) {
        // TODO Auto-generated method stub
        switch (checkedId) {
        case R.id.ridefinate:

            break;

        case R.id.rdays:

            break;
        case R.id.rhrandmin:
        //  showDialog(2);
            break;
        case R.id.rtilldate:
             showDialog(1);
            break;
        }
    }

}

日志猫

10-16 19:16:16.567: E/AndroidRuntime(1137): FATAL EXCEPTION: main
10-16 19:16:16.567: E/AndroidRuntime(1137): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.avst.callxpressmobile/com.example.avst.callxpressmobile.Availability}: java.lang.NullPointerException
10-16 19:16:16.567: E/AndroidRuntime(1137):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
10-16 19:16:16.567: E/AndroidRuntime(1137):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-16 19:16:16.567: E/AndroidRuntime(1137):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-16 19:16:16.567: E/AndroidRuntime(1137):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-16 19:16:16.567: E/AndroidRuntime(1137):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-16 19:16:16.567: E/AndroidRuntime(1137):     at android.os.Looper.loop(Looper.java:137)
10-16 19:16:16.567: E/AndroidRuntime(1137):     at android.app.ActivityThread.main(ActivityThread.java:4745)
10-16 19:16:16.567: E/AndroidRuntime(1137):     at java.lang.reflect.Method.invokeNative(Native Method)
10-16 19:16:16.567: E/AndroidRuntime(1137):     at java.lang.reflect.Method.invoke(Method.java:511)
10-16 19:16:16.567: E/AndroidRuntime(1137):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-16 19:16:16.567: E/AndroidRuntime(1137):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-16 19:16:16.567: E/AndroidRuntime(1137):     at dalvik.system.NativeStart.main(Native Method)
10-16 19:16:16.567: E/AndroidRuntime(1137): Caused by: java.lang.NullPointerException
10-16 19:16:16.567: E/AndroidRuntime(1137):     at com.example.avst.callxpressmobile.Availability.onCreate(Availability.java:47)
10-16 19:16:16.567: E/AndroidRuntime(1137):     at android.app.Activity.performCreate(Activity.java:5008)
10-16 19:16:16.567: E/AndroidRuntime(1137):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-16 19:16:16.567: E/AndroidRuntime(1137):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
10-16 19:16:16.567: E/AndroidRuntime(1137):     ... 11 more
4

1 回答 1

0

Android 提供了两个标准对话框。DatePickerDialogTimePickerDialog。如果要创建自定义对话框,请参阅指南。

于 2012-10-16T07:17:29.043 回答