我是Android Studio的新手(也是Java的新手),我正在尝试创建一个应用程序,允许用户输入时间并让计时器在该时间发生时开始倒计时。在研究之后,似乎使用意图是最好的方法,但我在理解如何使用它时遇到了一些问题。现在,我的主活动文件中有以下代码(称为倒计时):
public class Countdown extends AppCompatActivity {
EditText mHours;
EditText mMinutes;
TextView mDisplay;
public static long finalTime = 0;
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
Context context;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_countdown);
mHours = (EditText) findViewById(R.id.text1);
mMinutes = (EditText) findViewById(R.id.text2);
mDisplay = (TextView) findViewById(R.id.display);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 11);
calendar.set(Calendar.MINUTE, 22);
Intent intent = new Intent(context, Countdown.class);
alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
alarmMgr.set(AlarmManager.RTC, calendar.getTimeInMillis(), alarmIntent);
final long hourInput = Long.parseLong(mHours.getText().toString());
final long minuteInput = Long.parseLong(mMinutes.getText().toString());
finalTime = (hourInput * 3600000) + (minuteInput * 60000);
}
} );}}
在我的接收器文件中,我有:
public class CountdownStarter {
TextView mDisplay;
public void onRecieve() {
final CountDownTimer aCounter = new CountDownTimer(Countdown.finalTime, 1000) {
public void onTick(long millisUntilFinished) {
mDisplay.setText("Seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mDisplay.setText("Finished");
}
};
aCounter.start();
}
}
现在,运行调试器我收到此消息:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.timerexample, PID: 32276
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.content.ComponentName.<init>(ComponentName.java:128)
at android.content.Intent.<init>(Intent.java:5283)
at com.example.android.timerexample.Countdown$1.onClick(Countdown.java:42)
at android.view.View.performClick(View.java:5702)
at android.widget.TextView.performClick(TextView.java:10888)
at android.view.View$PerformClick.run(View.java:22533)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
我试图调查它,但我不明白为什么我会得到NullPointerException或如何解决它 - 我肯定在这里做了一些错误但我无法弄清楚是什么下一步是。任何帮助都会非常有帮助!
答案 0 :(得分:1)
在代码段中没有启动上下文,如上所述,您不需要活动类中的上下文对象。
您可以替换此代码
Intent intent = new Intent(context, Countdown.class);
alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
带
Intent intent = new Intent(this, Countdown.class);
alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
答案 1 :(得分:0)
Activity
是Context
,因此Context context
不是必需的,您可以在此使用Countdown.this
而不是context
变量。
Intent intent = new Intent(context, Countdown.class); // here
alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0); // and here
或者您需要实际分配context
private Context;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_countdown);
context = this; // Here