TimePickerDialog事件OnTimeSetListener导致应用程序关闭

时间:2014-02-21 08:44:32

标签: java android listener

为什么点击"Edittext"框后我的应用会关闭?

TimePickerDialog是要显示的,但是因为我添加了OnTimeSetListener,我的应用程序会中断。

感谢您的帮助。

package com.example.d;

import java.util.Calendar;

import android.os.Bundle;
import android.app.Activity;
import android.app.TimePickerDialog;
import android.view.View;
import android.widget.EditText;
import android.widget.TimePicker;
public class MainActivity extends Activity {

     /** Called when the activity is first created. */
        int hour,min;
        static final int TIME_DIALOG_ID=0;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);





        EditText etOne = (EditText) findViewById(R.id.editText1);
        etOne.setOnClickListener(new EditText.OnClickListener() {
            public void onClick(View v) {
                    //Do stuff here

                Calendar c=Calendar.getInstance();
                int hour=c.get(Calendar.HOUR);
                int min=c.get(Calendar.MINUTE);



                showTimeDialog(v, hour, min);
            }
        });

        }

        public void showTimeDialog(View v, int hour, int min)
        {
            (new TimePickerDialog(getBaseContext(), timeSetListener, hour, min, true)).show();
        }

        public TimePickerDialog.OnTimeSetListener timeSetListener=new TimePickerDialog.OnTimeSetListener() {

            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                // TODO Auto-generated method stub
                hour=hourOfDay;
                min=minute;
                EditText et=(EditText)findViewById(R.id.editText1);
                et.setText(hour+" : "+min);

            }
        };
    }

1 个答案:

答案 0 :(得分:0)

replace getBaseContext() with activity.this or getApplicationContext()


    public void showTimeDialog(View v, int hour, int min)
    {
        (new TimePickerDialog(MainActivity.this, timeSetListener, hour, min, true)).show();
    }