- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试从按钮的 onClick 中添加一个未决 Intent 以实现警报,但我在“android:app.PendingIntent.getActivity(PendingIntent.java:257)”和“at android:app”处收到空指针异常.PendingIntent.getActivity(PendingIntent.java:224)”。
这是我的代码,点击“添加”按钮应该会创建待处理的 Intent。
public class DisplayAlertMessage extends Activity {
private int mYear, mMonth, mDay, mHour, mMinute;
private long alarm_time;
private int rem = 0;
private String date,time;
private String lists1[] = new String[10];
static final int DATE_DIALOG_ID = 999;
@SuppressLint("InflateParams")
public void displayAlertDialog(LayoutInflater inflater,final MainActivity obj,final MainActivity obj1,final Context con) {
/** This intent invokes the activity DemoActivity, which in turn opens the AlertDialog window */
final Intent in = new Intent("com.example.todotry_1.demoactivity");
final View alertLayout = inflater.inflate(R.layout.layout_custom_dialog, null);
final EditText Note = (EditText) alertLayout.findViewById(R.id.Note);
final EditText Lists = (EditText) alertLayout.findViewById(R.id.Lists);
final TextView Remind = (TextView) alertLayout.findViewById(R.id.Remind);
final TextView Time = (TextView) alertLayout.findViewById(R.id.Time);
final TextView Date = (TextView) alertLayout.findViewById(R.id.Date);
final TextView Timeval = (TextView) alertLayout.findViewById(R.id.Timeval);
final TextView Dateval = (TextView) alertLayout.findViewById(R.id.Dateval);
final ImageView iv=(ImageView) alertLayout.findViewById(R.id.img);
i=iv;
AlertDialog.Builder alert = new AlertDialog.Builder(obj,R.style.DialogTheme);
Remind.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("onCLICK","REMIND");
Toast.makeText(con, "REMIND CLICKED", Toast.LENGTH_SHORT).show();
Remind.setVisibility(View.GONE);
Time.setVisibility(View.VISIBLE);
Date.setVisibility(View.VISIBLE);
rem = 1;
Time.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
// Launch Time Picker Dialog
TimePickerDialog tpd = new TimePickerDialog(obj,new TimePickerDialog.OnTimeSetListener()
{
@Override
public void onTimeSet(TimePicker view, int hourOfDay,int minute) {
// Display Selected time in textbox
Timeval.setVisibility(View.VISIBLE);
Timeval.setText(hourOfDay + ":" + minute);
}
}, mHour, mMinute, false);
tpd.show();
time = mHour+":"+mMinute;
}
});
Date.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
// Launch Date Picker Dialog
DatePickerDialog dpd = new DatePickerDialog(obj,new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// Display Selected date in textbox
Dateval.setVisibility(View.VISIBLE);
Dateval.setText(dayOfMonth + "-"+ (monthOfYear + 1) + "-" + year);
}
}, mYear, mMonth, mDay);
dpd.show();
date = mDay+"-"+mMonth+"-"+mYear;
}
});
GregorianCalendar calendar = new GregorianCalendar(mYear,mMonth,mDay, mHour, mMinute);
alarm_time = calendar.getTimeInMillis();
}
});
alert.setTitle("ADD NOTE");
alert.setView(alertLayout);
alert.setCancelable(false);
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(con, "Cancel clicked", Toast.LENGTH_SHORT).show();
}
});
alert.setPositiveButton("ADD", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// code for matching password
String note = Note.getText().toString();
String lists = Lists.getText().toString() + '\n';
int l=lists.length();
int i=0;
String one="";
char q;
String main1="";
int count = 0,j=0;
for (i=0;i<l;i++)
{
q=lists.charAt(i);
if(q!='\n' )
one += q;
else
{
Log.d("the string",":" + one);
//Toast.makeText(getBaseContext(), "Words " + one , Toast.LENGTH_SHORT).show();
main1 += count + " : " + one;
lists1[j++] = one;
one="";
}
count++;
}
Log.d("DISplay","111before finish add");
if((date == null)^(time == null))
{
Toast.makeText(con, "Enter both date and time to set alarm", Toast.LENGTH_SHORT).show();
}
else{
TaskDBHelper ob = new TaskDBHelper(obj1);
SQLiteDatabase sqlDB = ob.getWritableDatabase();
if((date != null)&(time != null))
{
String f1 = String.format("INSERT INTO prioritised (task,date,time,ringtone,done,Hidden) VALUES('"+note+"','"+date+"','"+time+"','"+""+"',0,"+MainActivity.getStat()+")");
sqlDB.execSQL(f1);
if((j!=0)&(main1!="\n") )
{
int id=0;
Cursor cursor = sqlDB.rawQuery("SELECT _id from prioritised where task = '"+note+"'",null);
if (cursor != null) {
try {
if (cursor.moveToFirst()) {
id = cursor.getInt(0);
}
} finally {
cursor.close();
}
}
Log.d("Prio"," "+id);
String add;
for(int k=0;k<j;k++)
{
add = String.format("INSERT INTO sublistprioritised (_id1,sublist,done,hidden) VALUES("+id+",'"+lists1[k]+"',0,"+MainActivity.getStat()+")");
sqlDB.execSQL(add);
}
}
/** Creating a Pending Intent */
Context x=getBaseContext();
int requestID = (int) System.currentTimeMillis();
PendingIntent operation = PendingIntent.getActivity(x, requestID, in,Intent.FLAG_ACTIVITY_NEW_TASK);
/** Getting a reference to the System Service ALARM_SERVICE */
AlarmManager alarmManager = (AlarmManager) x.getSystemService(ALARM_SERVICE);
/** Setting an alarm, which invokes the operation at alart_time */
alarmManager.set(AlarmManager.RTC_WAKEUP , alarm_time , operation);
/** Alert is set successfully */
Toast.makeText(x, "Alarm is set successfully",Toast.LENGTH_SHORT).show();
}
else{
String f2 = String.format("INSERT INTO unprioritised (task,done,Hidden) VALUES('"+note+"',0,"+MainActivity.getStat()+")");
sqlDB.execSQL(f2);
if((j!=0)&(main1!="\n") )
{
int id=0;
Cursor cursor = sqlDB.rawQuery("SELECT _id from unprioritised where task = '"+note+"'",null);
if (cursor != null) {
try {
if (cursor.moveToFirst()) {
id = cursor.getInt(0);
}
} finally {
cursor.close();
}
}
String add;
for(int k=0;k<j;k++)
{
add = String.format("INSERT INTO sublistunprioritised (_id1,sublist,done,Hidden) VALUES("+id+",'"+lists1[k]+"',0,"+MainActivity.getStat()+")");
sqlDB.execSQL(add);
}
}
}
}
}
});
AlertDialog dialog = alert.create();
dialog.show();
}
}
谢谢。感谢您的帮助。
这是 Logcat
04-02 13:38:03.922: E/AndroidRuntime(2120): FATAL EXCEPTION: main
04-02 13:38:03.922: E/AndroidRuntime(2120): Process: com.example.todotry_1, PID: 2120
04-02 13:38:03.922: E/AndroidRuntime(2120): java.lang.NullPointerException
04-02 13:38:03.922: E/AndroidRuntime(2120): at android.app.PendingIntent.getActivity(PendingIntent.java:257)
04-02 13:38:03.922: E/AndroidRuntime(2120): at android.app.PendingIntent.getActivity(PendingIntent.java:224)
04-02 13:38:03.922: E/AndroidRuntime(2120): at com.example.todotry_1.DisplayAlertMessage$4.onClick(DisplayAlertMessage.java:244)
04-02 13:38:03.922: E/AndroidRuntime(2120): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
04-02 13:38:03.922: E/AndroidRuntime(2120): at android.os.Handler.dispatchMessage(Handler.java:102)
04-02 13:38:03.922: E/AndroidRuntime(2120): at android.os.Looper.loop(Looper.java:136)
04-02 13:38:03.922: E/AndroidRuntime(2120): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-02 13:38:03.922: E/AndroidRuntime(2120): at java.lang.reflect.Method.invokeNative(Native Method)
04-02 13:38:03.922: E/AndroidRuntime(2120): at java.lang.reflect.Method.invoke(Method.java:515)
04-02 13:38:03.922: E/AndroidRuntime(2120): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-02 13:38:03.922: E/AndroidRuntime(2120): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-02 13:38:03.922: E/AndroidRuntime(2120): at dalvik.system.NativeStart.main(Native Method)
更新日志:
04-02 15:59:03.140: E/AndroidRuntime(1880): android.database.sqlite.SQLiteConstraintException: columns _id1, sublist are not unique (code 19)
04-02 15:59:03.140: E/AndroidRuntime(1880): at android.database.sqlite.SQLiteConnection.nativeExecuteForChangedRowCount(Native Method)
04-02 15:59:03.140: E/AndroidRuntime(1880): at android.database.sqlite.SQLiteConnection.executeForChangedRowCount(SQLiteConnection.java:734)
04-02 15:59:03.140: E/AndroidRuntime(1880): at android.database.sqlite.SQLiteSession.executeForChangedRowCount(SQLiteSession.java:754)
04-02 15:59:03.140: E/AndroidRuntime(1880): at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:64)
04-02 15:59:03.140: E/AndroidRuntime(1880): at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1674)
04-02 15:59:03.140: E/AndroidRuntime(1880): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1603)
04-02 15:59:03.140: E/AndroidRuntime(1880): at com.example.todotry_1.DisplayAlertMessage$4.onClick(DisplayAlertMessage.java:236)
04-02 15:59:03.140: E/AndroidRuntime(1880): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
04-02 15:59:03.140: E/AndroidRuntime(1880): at android.os.Handler.dispatchMessage(Handler.java:102)
04-02 15:59:03.140: E/AndroidRuntime(1880): at android.os.Looper.loop(Looper.java:136)
04-02 15:59:03.140: E/AndroidRuntime(1880): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-02 15:59:03.140: E/AndroidRuntime(1880): at java.lang.reflect.Method.invokeNative(Native Method)
04-02 15:59:03.140: E/AndroidRuntime(1880): at java.lang.reflect.Method.invoke(Method.java:515)
04-02 15:59:03.140: E/AndroidRuntime(1880): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-02 15:59:03.140: E/AndroidRuntime(1880): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-02 15:59:03.140: E/AndroidRuntime(1880): at dalvik.system.NativeStart.main(Native Method)
最佳答案
看起来这里没有必要调用getBaseContext()
,而且你可能不想使用currentTimeMillis()
作为requestCode,以防万一曾经需要取消PendingIntent
:
/** Creating a Pending Intent */
//Context x=getBaseContext();
//int requestID = (int) System.currentTimeMillis();
PendingIntent operation = null;
//Make sure nothing is null for debugging purposes
if (con == null){
Log.e("onCLICK","con is null");
}
else if (in == null){
Log.e("onCLICK","in is null");
}
else{
operation = PendingIntent.getActivity(con, 0, in,PendingIntent.FLAG_UPDATE_CURRENT);
}
if (operation != null){
/** Getting a reference to the System Service ALARM_SERVICE */
AlarmManager alarmManager = (AlarmManager) con.getSystemService(ALARM_SERVICE);
/** Setting an alarm, which invokes the operation at alart_time */
alarmManager.set(AlarmManager.RTC_WAKEUP , alarm_time , operation);
/** Alert is set successfully */
Toast.makeText(con, "Alarm is set successfully",Toast.LENGTH_SHORT).show();
}
也请查看此帖子以获取示例:PendingIntent works correctly for the first notification but incorrectly for the rest
编辑:我刚刚将 PendingIntent 代码放入 AndroidStudio,它给了我这条消息:
Must be one or more of: PendingIntent.FLAG_ONE_SHOT, PendingIntent.FLAG_NO_CREATE, PendingIntent.FLAG_CANCEL_CURRENT, PendingIntent.FLAG_UPDATE_CURRENT, Intent.FILL_IN_ACTION, Intent.FILL_IN_DATA, Intent.FILL_IN_CATEGORIES, Intent.FILL_IN_COMPONENT, Intent.FILL_IN_PACKAGE, Intent.FILL_IN_SOURCE_BOUNDS, Intent.FILL_IN_SELECTOR, Intent.FILL_IN_CLIP_DATA less... (Ctrl+F1) Reports two types of problems: Supplying the wrong type of resource identifier. For example, when calling Resources.getString(int id), you should be passing R.string.something, not R.drawable.something. Passing the wrong constant to a method which expects one of a specific set of constants. For example, when calling View#setLayoutDirection, the parameter must be android.view.View.LAYOUT_DIRECTION_LTR or android.view.View.LAYOUT_DIRECTION_RTL.
看看The Documentation有关要在此处使用的有效标志的更多信息。
关于android - 从 PendingIntent.getActivity Android 获取空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29419632/
我最近在/ drawable中添加了一些.gifs,以便可以将它们与按钮一起使用。这个工作正常(没有错误)。现在,当我重建/运行我的应用程序时,出现以下错误: Error: Gradle: Execu
Android 中有返回内部存储数据路径的方法吗? 我有 2 部 Android 智能手机(Samsung s2 和 s7 edge),我在其中安装了一个应用程序。我想使用位于这条路径中的 sqlit
这个问题在这里已经有了答案: What's the difference between "?android:" and "@android:" in an android layout xml f
我只想知道 android 开发手机、android 普通手机和 android root 手机之间的实际区别。 我们不能从实体店或除 android marketplace 以外的其他地方购买开发手
自Gradle更新以来,我正在努力使这个项目达到标准。这是一个团队项目,它使用的是android-apt插件。我已经进行了必要的语法更改(编译->实现和apt->注释处理器),但是编译器仍在告诉我存在
我是android和kotlin的新手,所以请原谅要解决的一个非常简单的问题! 我已经使用导航体系结构组件创建了一个基本应用程序,使用了底部的导航栏和三个导航选项。每个导航选项都指向一个专用片段,该片
我目前正在使用 Facebook official SDK for Android . 我现在正在使用高级示例应用程序,但我不知道如何让它获取应用程序墙/流/状态而不是登录的用户。 这可能吗?在那种情
我在下载文件时遇到问题, 我可以在模拟器中下载文件,但无法在手机上使用。我已经定义了上网和写入 SD 卡的权限。 我在服务器上有一个 doc 文件,如果用户单击下载。它下载文件。这在模拟器中工作正常但
这个问题在这里已经有了答案: What is the difference between gravity and layout_gravity in Android? (22 个答案) 关闭 9
任何人都可以告诉我什么是 android 缓存和应用程序缓存,因为当我们谈论缓存清理应用程序时,它的作用是,缓存清理概念是清理应用程序缓存还是像内存管理一样主存储、RAM、缓存是不同的并且据我所知,缓
假设应用程序 Foo 和 Eggs 在同一台 Android 设备上。任一应用程序都可以获取设备上所有应用程序的列表。一个应用程序是否有可能知道另一个应用程序是否已经运行以及运行了多长时间? 最佳答案
我有点困惑,我只看到了从 android 到 pc 或者从 android 到 pc 的例子。我需要制作一个从两部手机 (android) 连接的 android 应用程序进行视频聊天。我在想,我知道
用于使用 Android 以编程方式锁定屏幕。我从 Stackoverflow 之前关于此的问题中得到了一些好主意,并且我做得很好,但是当我运行该代码时,没有异常和错误。而且,屏幕没有锁定。请在这段代
文档说: android:layout_alignParentStart If true, makes the start edge of this view match the start edge
我不知道这两个属性和高度之间的区别。 以一个TextView为例,如果我将它的layout_width设置为wrap_content,并将它的width设置为50 dip,会发生什么情况? 最佳答案
这两个属性有什么关系?如果我有 android:noHistory="true",那么有 android:finishOnTaskLaunch="true" 有什么意义吗? 最佳答案 假设您的应用中有
我是新手,正在尝试理解以下 XML 代码: 查看 developer.android.com 上的文档,它说“starStyle”是 R.attr 中的常量, public static final
在下面的代码中,为什么当我设置时单选按钮的外观会发生变化 android:layout_width="fill_parent" 和 android:width="fill_parent" 我说的是
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 9
假设我有一个函数 fun myFunction(name:String, email:String){},当我调用这个函数时 myFunction('Ali', 'ali@test.com ') 如何
我是一名优秀的程序员,十分优秀!