Java 警报获取错误android
我正试图在某个时间出现警报。我能够做到这一点,但当我按下snooze按钮时,它工作了,我得到另一个警报,但我也得到一些错误。 查找以下错误:Java 警报获取错误android,java,android,alert,alarmmanager,android-alertdialog,Java,Android,Alert,Alarmmanager,Android Alertdialog,我正试图在某个时间出现警报。我能够做到这一点,但当我按下snooze按钮时,它工作了,我得到另一个警报,但我也得到一些错误。 查找以下错误: 11-19 12:50:26.755: E/AndroidRuntime(452): FATAL EXCEPTION: main 11-19 12:50:26.755: E/AndroidRuntime(452): java.lang.NullPointerException 11-19 12:50:26.755: E/AndroidRuntime(452
11-19 12:50:26.755: E/AndroidRuntime(452): FATAL EXCEPTION: main
11-19 12:50:26.755: E/AndroidRuntime(452): java.lang.NullPointerException
11-19 12:50:26.755: E/AndroidRuntime(452): at android.app.PendingIntent.getActivity(PendingIntent.java:191)
11-19 12:50:26.755: E/AndroidRuntime(452): at com.example.servicealarmdemo2.MainActivity.repeat(MainActivity.java:124)
11-19 12:50:26.755: E/AndroidRuntime(452): at com.example.servicealarmdemo2.AlertDemo$2.onClick(AlertDemo.java:58)
11-19 12:50:26.755: E/AndroidRuntime(452): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:159)
11-19 12:50:26.755: E/AndroidRuntime(452): at android.os.Handler.dispatchMessage(Handler.java:99)
11-19 12:50:26.755: E/AndroidRuntime(452): at android.os.Looper.loop(Looper.java:123)
11-19 12:50:26.755: E/AndroidRuntime(452): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-19 12:50:26.755: E/AndroidRuntime(452): at java.lang.reflect.Method.invokeNative(Native Method)
11-19 12:50:26.755: E/AndroidRuntime(452): at java.lang.reflect.Method.invoke(Method.java:507)
11-19 12:50:26.755: E/AndroidRuntime(452): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-19 12:50:26.755: E/AndroidRuntime(452): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-19 12:50:26.755: E/AndroidRuntime(452): at dalvik.system.NativeStart.main(Native Method)
重复警报的函数位于MainActivity.java中
public void repeat() {
Intent i = new Intent("com.example.servicealarmdemo2.demoactivity");
PendingIntent operation = PendingIntent.getActivity(getBaseContext(), 0, i, Intent.FLAG_ACTIVITY_NEW_TASK);
AlarmManager alarmManager = (AlarmManager) getBaseContext().getSystemService(ALARM_SERVICE);
long time= System.currentTimeMillis();
EditText text=(EditText)findViewById(R.id.editText1);
String str = text.getText().toString();
long t=Long.parseLong(str);
alarmManager.set(AlarmManager.RTC_WAKEUP, time+( t*60*1000), operation);
}
package com.example.servicealarmdemo2;
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");
}
}
以下两类用于警报:
AlertDemo.java
package com.example.servicealarmdemo2;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Intent;
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;
import android.widget.EditText;
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("Take", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
/** Exit application on click OK */
getActivity().finish();
}
});
builder.setNegativeButton("Snooze", new OnClickListener() {
// @Override
public void onClick(DialogInterface dialog, int which) {
/** Exit application on click OK */
//getActivity().finish();
// repeat();
System.out.println("REPEAT");
new MainActivity().repeat();
//System.out.println("Activities: "+m);
}
});
/** 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();
}
// public void repeat() {
// MainActivity.repeat();
// }
// public void repeat() {
// Intent i = new Intent("com.example.servicealarmdemo2.demoactivity");
//// PendingIntent operation = PendingIntent.getActivity(getBaseContext(), 0, i, Intent.FLAG_ACTIVITY_NEW_TASK);
// PendingIntent operation = PendingIntent.getActivity(getActivity().getBaseContext(), 0, i, Intent.FLAG_ACTIVITY_NEW_TASK);
// AlarmManager alarmManager = (AlarmManager) getActivity().getBaseContext().getSystemService(getActivity().ALARM_SERVICE);
// long time= System.currentTimeMillis();
// EditText text=(EditText)getActivity().findViewById(R.id.editText1);
// String str = text.getText().toString();
// long t=Long.parseLong(str);
// alarmManager.set(AlarmManager.RTC_WAKEUP, time+( t*60*1000), operation);
//
// }
}
和DemoActivity.java
public void repeat() {
Intent i = new Intent("com.example.servicealarmdemo2.demoactivity");
PendingIntent operation = PendingIntent.getActivity(getBaseContext(), 0, i, Intent.FLAG_ACTIVITY_NEW_TASK);
AlarmManager alarmManager = (AlarmManager) getBaseContext().getSystemService(ALARM_SERVICE);
long time= System.currentTimeMillis();
EditText text=(EditText)findViewById(R.id.editText1);
String str = text.getText().toString();
long t=Long.parseLong(str);
alarmManager.set(AlarmManager.RTC_WAKEUP, time+( t*60*1000), operation);
}
package com.example.servicealarmdemo2;
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");
}
}
切勿仅通过Intent
使用new
实例化活动
NPE的原因是代码试图将该活动用作上下文
,但尚未对其进行适当初始化以供使用
从技术上讲,
getBaseContext()
返回null
并且getActivity()
无条件地在传入的上下文上调用方法。您编写代码的方式看起来不太好,空指针是因为错误的context
PendingEvent操作=PendingEvent.getActivity(getBaseContext(),0,i,Intent.FLAG_ACTIVITY_NEW_TASK)
调用repeat()
函数时,尝试传递活动上下文
新建MainActivity()。重复(活动上下文)//在这里传递活动上下文
但这不是一个好办法 更换
新的MainActivity()。重复()代码>与((MainActivity)getActivity())。重复()
(您永远不应该实例化活动
)@Houcine我尝试了您的方法,我得到无法从FragmentActivity转换为MainActivity
将repeat()方法转换为DemoActivity,并使用DemoActivity更改上面注释中代码中的MainActivity,这并没有解决我的问题。NPE的相同错误再次出现!!但是我如何才能从意图中获取活动,方法repeat()
在MainActivity
@user3008437中,我看不出为什么repeat()
应该是MainActivity
的非静态成员函数。如果我尝试将其作为静态方法,我会在使用findViewById()时出错
和getBaseContext()
我添加了repeat函数的参数,如上所述。当使用newmainActivity().repeat(getActivity().getBaseContext())
你是说活动上下文这个getActivity().getBaseContext()
我注意到你的意思是函数的参数类型为activity
,所以我也这样做了,但仍然得到了错误