我已经制作了 DatePicker 和 TimePicker。有了它们,我可以设置对话框必须出现的时间。当我按下按钮设置警报时,一切都很好,但是当必须出现对话框时,会出现另一个对话框“不幸的是程序已停止”。有人可以告诉我我的代码哪里有问题吗?
主要活动:
package in.wptrafficanalyzer.servicealarmdemo;
import java.util.GregorianCalendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OnClickListener setClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
/** This intent invokes the activity DemoActivity, which in turn opens the AlertDialog window */
Intent i = new Intent("in.wptrafficanalyzer.servicealarmdemo.demoactivity");
/** Creating a Pending Intent */
PendingIntent operation = PendingIntent.getActivity(getBaseContext(), 0, i, Intent.FLAG_ACTIVITY_NEW_TASK);
/** Getting a reference to the System Service ALARM_SERVICE */
AlarmManager alarmManager = (AlarmManager) getBaseContext().getSystemService(ALARM_SERVICE);
/** Getting a reference to DatePicker object available in the MainActivity */
DatePicker dpDate = (DatePicker) findViewById(R.id.dp_date);
/** Getting a reference to TimePicker object available in the MainActivity */
TimePicker tpTime = (TimePicker) findViewById(R.id.tp_time);
int year = dpDate.getYear();
int month = dpDate.getMonth();
int day = dpDate.getDayOfMonth();
int hour = tpTime.getCurrentHour();
int minute = tpTime.getCurrentMinute();
/** Creating a calendar object corresponding to the date and time set by the user */
GregorianCalendar calendar = new GregorianCalendar(year,month,day, hour, minute);
/** Converting the date and time in to milliseconds elapsed since epoch */
long alarm_time = calendar.getTimeInMillis();
/** Setting an alarm, which invokes the operation at alart_time */
alarmManager.set(AlarmManager.RTC_WAKEUP , alarm_time , operation);
/** Alert is set successfully */
Toast.makeText(getBaseContext(), "Alarm is set successfully",Toast.LENGTH_SHORT).show();
}
};
OnClickListener quitClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
};
Button btnSetAlarm = ( Button ) findViewById(R.id.btn_set_alarm);
btnSetAlarm.setOnClickListener(setClickListener);
Button btnQuitAlarm = ( Button ) findViewById(R.id.btn_quit_alarm);
btnQuitAlarm.setOnClickListener(quitClickListener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
警报演示:
package in.wptrafficanalyzer.servicealarmdemo;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.WindowManager.LayoutParams;
public class AlertDemo extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
/** Turn Screen On and Unlock the keypad when this alert dialog is displayed */
getActivity().getWindow().addFlags(LayoutParams.FLAG_TURN_SCREEN_ON | LayoutParams.FLAG_DISMISS_KEYGUARD);
/** Creating a alert dialog builder */
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
/** Setting title for the alert dialog */
builder.setTitle("Alarm");
/** Setting the content for the alert dialog */
builder.setMessage("An Alarm by AlarmManager");
/** Defining an OK button event listener */
builder.setPositiveButton("OK", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
/** Exit application on click OK */
getActivity().finish();
}
});
/** Creating the alert dialog window */
return builder.create();
}
/** The application should be exit, if the user presses the back button */
@Override
public void onDestroy() {
super.onDestroy();
getActivity().finish();
}
}
演示活动:
package in.wptrafficanalyzer.servicealarmdemo;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.WindowManager.LayoutParams;
import android.widget.Toast;
public class DemoActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/** Creating an Alert Dialog Window */
AlertDemo alert = new AlertDemo();
/** Opening the Alert Dialog Window */
alert.show(getSupportFragmentManager(), "AlertDemo");
}
}
日志猫:
07-18 15:01:40.740: E/AndroidRuntime(429): FATAL EXCEPTION: main
07-18 15:01:40.740: E/AndroidRuntime(429): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{in.wptrafficanalyzer.servicealarmdemo/in.wptrafficanalyzer.servicealarmdemo.DemoActivity}: java.lang.ClassNotFoundException: in.wptrafficanalyzer.servicealarmdemo.DemoActivity in loader dalvik.system.PathClassLoader[/data/app/in.wptrafficanalyzer.servicealarmdemo-1.apk]
07-18 15:01:40.740: E/AndroidRuntime(429): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
07-18 15:01:40.740: E/AndroidRuntime(429): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-18 15:01:40.740: E/AndroidRuntime(429): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-18 15:01:40.740: E/AndroidRuntime(429): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-18 15:01:40.740: E/AndroidRuntime(429): at android.os.Handler.dispatchMessage(Handler.java:99)
07-18 15:01:40.740: E/AndroidRuntime(429): at android.os.Looper.loop(Looper.java:123)
07-18 15:01:40.740: E/AndroidRuntime(429): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-18 15:01:40.740: E/AndroidRuntime(429): at java.lang.reflect.Method.invokeNative(Native Method)
07-18 15:01:40.740: E/AndroidRuntime(429): at java.lang.reflect.Method.invoke(Method.java:521)
07-18 15:01:40.740: E/AndroidRuntime(429): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-18 15:01:40.740: E/AndroidRuntime(429): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-18 15:01:40.740: E/AndroidRuntime(429): at dalvik.system.NativeStart.main(Native Method)
07-18 15:01:40.740: E/AndroidRuntime(429): Caused by: java.lang.ClassNotFoundException: in.wptrafficanalyzer.servicealarmdemo.DemoActivity in loader dalvik.system.PathClassLoader[/data/app/in.wptrafficanalyzer.servicealarmdemo-1.apk]
07-18 15:01:40.740: E/AndroidRuntime(429): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
07-18 15:01:40.740: E/AndroidRuntime(429): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
07-18 15:01:40.740: E/AndroidRuntime(429): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
07-18 15:01:40.740: E/AndroidRuntime(429): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
07-18 15:01:40.740: E/AndroidRuntime(429): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)