我是Android的新手,我正在制作闹钟应用。但是当我运行它时,我得到一个NullPointerException
。任何人都可以帮助我吗?这是我的代码:
public class MainActivity extends ActionBarActivity {
private Calendar c;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView textView=(TextView)findViewById(R.id.textview1);
final Button setbtn=(Button)findViewById(R.id.setclock);
final Button unsetbtn=(Button)findViewById(R.id.unsetclock);
setbtn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
long current=System.currentTimeMillis();
c.setTimeInMillis(current);
int mHour=c.get(Calendar.HOUR_OF_DAY);
int mMinute=c.get(Calendar.MINUTE);
new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
c.setTimeInMillis(System.currentTimeMillis());
c.set(Calendar.HOUR_OF_DAY,hourOfDay);
c.set(Calendar.MINUTE, minute);
c.set(Calendar.SECOND,0);
c.set(Calendar.MILLISECOND,0);
Intent intent=new Intent(MainActivity.this, CallAlarm.class);
PendingIntent sender= PendingIntent.getBroadcast(MainActivity.this,
1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am=(AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),sender);
String tmpS= format(hourOfDay)+""+format(minute);
textView.setText(tmpS);
Toast.makeText(MainActivity.this, "The alarm seted: "+tmpS,
Toast.LENGTH_LONG).show();
}
},mHour,mMinute,true).show();
}
});
unsetbtn.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Intent intent= new Intent(MainActivity.this, CallAlarm.class);
PendingIntent sender=PendingIntent.getBroadcast(MainActivity.this,0, intent, 0);
AlarmManager am=(AlarmManager)getSystemService(ALARM_SERVICE);
am.cancel(sender);
textView.setText("Current No Alarm!");
Toast.makeText(MainActivity.this, "Alarm Canceled", Toast.LENGTH_LONG).show();
}
});
}
private String format(int x)
{
String s=""+x;
if(s.length()==1)
s="0"+s;
return s;
}
}
我在logcat中得到了这些:
01-12 14:18:16.620: D/AndroidRuntime(2092): Shutting down VM
01-12 14:18:16.628: E/AndroidRuntime(2092): FATAL EXCEPTION: main
01-12 14:18:16.628: E/AndroidRuntime(2092): Process: com.example.alarm1, PID: 2092
**01-12 14:18:16.628: E/AndroidRuntime(2092): java.lang.NullPointerException: Attempt to invoke virtual method 'void java.util.Calendar.setTimeInMillis(long)' on a null object reference**
01-12 14:18:16.628: E/AndroidRuntime(2092): at com.example.alarm1.MainActivity$1.onClick(MainActivity.java:32)
01-12 14:18:16.628: E/AndroidRuntime(2092): at android.view.View.performClick(View.java:4756)
01-12 14:18:16.628: E/AndroidRuntime(2092): at android.view.View$PerformClick.run(View.java:19749)
01-12 14:18:16.628: E/AndroidRuntime(2092): at android.os.Handler.handleCallback(Handler.java:739)
01-12 14:18:16.628: E/AndroidRuntime(2092): at android.os.Handler.dispatchMessage(Handler.java:95)
01-12 14:18:16.628: E/AndroidRuntime(2092): at android.os.Looper.loop(Looper.java:135)
01-12 14:18:16.628: E/AndroidRuntime(2092): at android.app.ActivityThread.main(ActivityThread.java:5221)
01-12 14:18:16.628: E/AndroidRuntime(2092): at java.lang.reflect.Method.invoke(Native Method)
01-12 14:18:16.628: E/AndroidRuntime(2092): at java.lang.reflect.Method.invoke(Method.java:372)
01-12 14:18:16.628: E/AndroidRuntime(2092): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
01-12 14:18:16.628: E/AndroidRuntime(2092): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
我不知道为什么会出现空指针异常。任何人都可以帮我吗? 非常感谢你!!!
答案 0 :(得分:10)
您未初始化日历变量c。尝试:
private Calendar c = Calendar.getInstance();
建议之一:不要使用单字符变量名,除非它是迭代变量。