我使用此代码时,Android Studio活动已停止

时间:2017-04-20 04:03:23

标签: android

package nav.library.new23db;

public class aaaa extends AppCompatActivity {
    Button button_stpd;
    static final int DIALOG_ID = 0;
    int minute_x;
    int hour_x;

    @Override
    protected void onCreate(Bundle savedInstance) {
        super.onCreate(savedInstance);
        setContentView(R.layout.aaaa);
        showTimePickerDialog();
    }

    public void showTimePickerDialog() {
        button_stpd = (Button) findViewById(R.id.button);
        button_stpd.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showDialog(DIALOG_ID);
                }
            }
        );
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        if (id == DIALOG_ID)
            return new TimePickerDialog(aaaa.this, kTimePickerListner, hour_x, minute_x, false);
        return null;
    }

    protected TimePickerDialog.OnTimeSetListener kTimePickerListner =
        new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                hour_x = hourOfDay;
                minute_x = minute;
                Toast.makeText(aaaa.this, hour_x + " : " + minute_x, Toast.LENGTH_LONG).show();
            }
        };
}

当我运行应用程序并转到aaaaActivity时,应用程序会停止,这有什么问题?我在互联网上搜索,有人告诉我“返回null”有问题,但我不知道如何修改它。

2 个答案:

答案 0 :(得分:0)

    @Override
    protected Dialog onCreateDialog(int id) {
        if (id == DIALOG_ID){
            return new TimePickerDialog(aaaa.this, kTimePickerListner, hour_x, minute_x, false);
}
return null; 

    }

试试这种方式

答案 1 :(得分:0)

展示aaaa的{​​{1}}活动代码似乎TimePickerDialog

确保在OK文件中声明了aaaa活动。

<强>的AndroidManifest.xml

AndroidManifest.xml