SELECT TIME按钮工作正常,但SELECT DATE按钮显示错误“遗憾的是应用程序已停止”..我发现DatePickerFragment的代码正确..plzz帮助..这是用于呼叫提醒的程序..
public class MainActivity extends Activity {
EditText text;
EditText text2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (EditText)findViewById(R.id.editText1);
text2 = (EditText)findViewById(R.id.editText2);
Button btn = (Button)findViewById(R.id.button3);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
//User enters the number of seconds
EditText text = (EditText) findViewById(R.id.editText1);
int i = Integer.parseInt(text.getText().toString());
//Creating a PendingIntent
Intent intent = new Intent(MainActivity.this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
MainActivity.this, 234324243, intent, 0);
//Creating an AlarmManager and <span id="IL_AD9" class="IL_AD">setting</span> the pendingIntent to him
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ (i * 1000), pendingIntent);
}});
}
public static class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
//long millis=c.getTimeInMillis();
// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute,DateFormat.is24HourFormat(getActivity()));
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// Do something with the time chosen by the user
}
}
public void showTimePickerDialog(View v) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getFragmentManager(),"timePicker");
}
public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
//long millis=c.getTimeInMillis();
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
}
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(),"datePicker");
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="28dp"
android:layout_toLeftOf="@+id/editText1"
android:text="NAME" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/button2"
android:layout_below="@+id/textView1"
android:ems="10" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/editText1"
android:text="Phone" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_marginTop="14dp"
android:layout_toRightOf="@+id/textView1"
android:ems="10"
android:inputType="phone" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_alignTop="@+id/button1"
android:text="Set Reminder Time" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="16dp"
android:onClick="showTimePickerDialog"
android:text="Select Time" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/button1"
android:layout_marginTop="41dp"
android:text="Set Reminder Date" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/textView4"
android:onClick="showDatePickerDialog"
android:text="Select Date" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button2"
android:layout_centerHorizontal="true"
android:layout_marginTop="21dp"
android:text="Submit" />
</RelativeLayout>
logcat的
05-04 15:37:11.585: D/dalvikvm(617): Not late-enabling CheckJNI (already on)
05-04 15:37:12.804: E/Trace(617): error opening trace file: No such file or directory (2)
05-04 15:37:14.124: D/gralloc_goldfish(617): Emulator without GPU emulation detected.
05-04 15:37:16.585: I/Choreographer(617): Skipped 62 frames! The application may be doing too much work on its main thread.
05-04 15:37:16.895: I/Choreographer(617): Skipped 64 frames! The application may be doing too much work on its main thread.
05-04 15:37:17.215: I/Choreographer(617): Skipped 31 frames! The application may be doing too much work on its main thread.
05-04 15:37:19.295: I/Choreographer(617): Skipped 38 frames! The application may be doing too much work on its main thread.
05-04 15:37:20.137: I/Choreographer(617): Skipped 114 frames! The application may be doing too much work on its main thread.
05-04 15:37:21.254: D/AndroidRuntime(617): Shutting down VM
05-04 15:37:21.254: W/dalvikvm(617): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
05-04 15:37:21.334: E/AndroidRuntime(617): FATAL EXCEPTION: main
05-04 15:37:21.334: E/AndroidRuntime(617): java.lang.IllegalStateException: Could not find a method showDatePickerDialog(View) in the activity class com.example.reminder4.MainActivity for onClick handler on view class android.widget.Button with id 'button2'
05-04 15:37:21.334: E/AndroidRuntime(617): at android.view.View$1.onClick(View.java:3578)
05-04 15:37:21.334: E/AndroidRuntime(617): at android.view.View.performClick(View.java:4084)
05-04 15:37:21.334: E/AndroidRuntime(617): at android.view.View$PerformClick.run(View.java:16966)
05-04 15:37:21.334: E/AndroidRuntime(617): at android.os.Handler.handleCallback(Handler.java:615)
05-04 15:37:21.334: E/AndroidRuntime(617): at android.os.Handler.dispatchMessage(Handler.java:92)
05-04 15:37:21.334: E/AndroidRuntime(617): at android.os.Looper.loop(Looper.java:137)
05-04 15:37:21.334: E/AndroidRuntime(617): at android.app.ActivityThread.main(ActivityThread.java:4745)
05-04 15:37:21.334: E/AndroidRuntime(617): at java.lang.reflect.Method.invokeNative(Native Method)
05-04 15:37:21.334: E/AndroidRuntime(617): at java.lang.reflect.Method.invoke(Method.java:511)
05-04 15:37:21.334: E/AndroidRuntime(617): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-04 15:37:21.334: E/AndroidRuntime(617): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-04 15:37:21.334: E/AndroidRuntime(617): at dalvik.system.NativeStart.main(Native Method)
05-04 15:37:21.334: E/AndroidRuntime(617): Caused by: java.lang.NoSuchMethodException: showDatePickerDialog [class android.view.View]
05-04 15:37:21.334: E/AndroidRuntime(617): at java.lang.Class.getConstructorOrMethod(Class.java:460)
05-04 15:37:21.334: E/AndroidRuntime(617): at java.lang.Class.getMethod(Class.java:915)
05-04 15:37:21.334: E/AndroidRuntime(617): at android.view.View$1.onClick(View.java:3571)
05-04 15:37:21.334: E/AndroidRuntime(617): ... 11 more
05-04 15:37:25.745: I/Process(617): Sending signal. PID: 617 SIG: 9
答案 0 :(得分:1)
你把你的
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(),"datePicker");
}
在对话框Fragment类中,但您在mainActivity Layout中定义了onclick事件。 Logcat告诉你,它的主类中没有showDatePickerDialog来提供你的点击事件