我有两个java类,在第一个活动中我创建了一个数据库,并在sqlite DB中存储了一些数据。在第二个java类中,将检索检查当前日期和时间的数据。但是第二堂课没有开始。它在logcat中显示空指针异常。第一堂课是:
package com.example.eventmanager;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import com.example.eventmanager.R;
import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;
public class MainActivity extends Activity {
MediaPlayer mp;
EditText name;
EditText venue;
public SQLiteDatabase eventsDB;
private ScheduleClient scheduleClient;
private DatePicker picker;
private TimePicker timePic;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scheduleClient = new ScheduleClient(this);
scheduleClient.doBindService();
picker = (DatePicker) findViewById(R.id.scheduleTimePicker);
timePic = (TimePicker) findViewById(R.id.timePicer);
name = (EditText) findViewById(R.id.ename);
venue = (EditText) findViewById(R.id.venue);
}
public void onDateSelectedButtonClick(View v){
eventsDB = this.openOrCreateDatabase("eventsDB", 0, null);
eventsDB.setVersion(2);
eventsDB.setLocale(Locale.getDefault());
eventsDB.execSQL("CREATE TABLE IF NOT EXISTS " +
" events " +
" (name VARCHAR, date VARCHAR,time VARCHAR, venue VARCHAR);");
int day = picker.getDayOfMonth();
int month = picker.getMonth();
int year = picker.getYear();
Calendar c = Calendar.getInstance();
c.set(year, month, day);
c.set(Calendar.HOUR_OF_DAY, timePic.getCurrentHour());
c.set(Calendar.MINUTE, timePic.getCurrentMinute());
c.set(Calendar.SECOND, 0);
String eday = String.valueOf(day);
SimpleDateFormat month_date = new SimpleDateFormat("MMM"); // or "MMM" for short month name
String emonth = month_date.format(c.getTime());
// String emonth = String.valueOf(month);
String eyear = String.valueOf(year);
String edate = eday+","+emonth+" "+eyear;
String ehour = String.valueOf(timePic.getCurrentHour());
String eminute = String.valueOf(timePic.getCurrentMinute());
String etime = ehour+" : "+eminute;
eventsDB.execSQL("INSERT INTO " +
"events" +
" Values ('"+name+"','"+edate+"','"+etime+"','"+venue+"');");
scheduleClient.setAlarmForNotification(c);
Toast.makeText(this, "The event for: "+ day +"/"+ (month+1) +"/"+ year+" Has been set", Toast.LENGTH_SHORT).show();
}
@Override
protected void onStop() {
if(scheduleClient != null)
scheduleClient.doUnbindService();
super.onStop();
}
}
,第二堂课是:
package com.example.eventmanager;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
public class AlertActivity extends Activity {
TextView test;
public SQLiteDatabase eventsDB;
private DatePicker picker;
private TimePicker timePic;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alert);
test = (TextView) findViewById(R.id.textView1);
Calendar c = Calendar.getInstance();
int day = c.get(Calendar.DAY_OF_MONTH);
//int month = c.get(Calendar.MONTH);
SimpleDateFormat month_date = new SimpleDateFormat("MMM"); // or "MMM" for short month name
String month = month_date.format(c.getTime());
int year = c.get(Calendar.YEAR);
String eday = String.valueOf(day);
String emonth = String.valueOf(month);
String eyear = String.valueOf(year);
String edate = eday+","+emonth+" "+eyear;
String ehour = String.valueOf(timePic.getCurrentHour());
String eminute = String.valueOf(timePic.getCurrentMinute());
String etime = ehour+" : "+eminute;
String q="Select * from events where date='" + edate + "' and time='" + etime+ "'";
Cursor m = eventsDB.rawQuery(q, null);
if(edate.matches("") || etime.matches("") ){
//Toast.makeText(getApplicationContext(), "Please insert a student ID", Toast.LENGTH_LONG).show();
}
else {
if(m.moveToFirst() )
{
String curevent = m.getString(m.getColumnIndex("name"));
String curdate = m.getString(m.getColumnIndex("edate"));
test.setText(curevent);
}
else {
test.setText("Not found");
}
}
}
}
logcat在这里:
09-14 19:55:29.954: D/GalleryCacheReady(2754): Receive action.ACTION_MEDIA_SCANNER_FINISHED
09-14 19:55:29.959: E/AndroidRuntime(1266): FATAL EXCEPTION: main
09-14 19:55:29.959: E/AndroidRuntime(1266): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.eventmanager/com.example.eventmanager.AlertActivity}: java.lang.NullPointerException
09-14 19:55:29.959: E/AndroidRuntime(1266): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
09-14 19:55:29.959: E/AndroidRuntime(1266): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
09-14 19:55:29.959: E/AndroidRuntime(1266): at android.app.ActivityThread.access$600(ActivityThread.java:140)
09-14 19:55:29.959: E/AndroidRuntime(1266): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
09-14 19:55:29.959: E/AndroidRuntime(1266): at android.os.Handler.dispatchMessage(Handler.java:99)
09-14 19:55:29.959: E/AndroidRuntime(1266): at android.os.Looper.loop(Looper.java:137)
09-14 19:55:29.959: E/AndroidRuntime(1266): at android.app.ActivityThread.main(ActivityThread.java:4898)
09-14 19:55:29.959: E/AndroidRuntime(1266): at java.lang.reflect.Method.invokeNative(Native Method)
09-14 19:55:29.959: E/AndroidRuntime(1266): at java.lang.reflect.Method.invoke(Method.java:511)
09-14 19:55:29.959: E/AndroidRuntime(1266): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
09-14 19:55:29.959: E/AndroidRuntime(1266): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
09-14 19:55:29.959: E/AndroidRuntime(1266): at dalvik.system.NativeStart.main(Native Method)
09-14 19:55:29.959: E/AndroidRuntime(1266): Caused by: java.lang.NullPointerException
09-14 19:55:29.959: E/AndroidRuntime(1266): at com.example.eventmanager.AlertActivity.onCreate(AlertActivity.java:47)
09-14 19:55:29.959: E/AndroidRuntime(1266): at android.app.Activity.performCreate(Activity.java:5206)
09-14 19:55:29.959: E/AndroidRuntime(1266): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
09-14 19:55:29.959: E/AndroidRuntime(1266): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
09-14 19:55:29.959: E/AndroidRuntime(1266): ... 11 more
09-14 19:55:30.009: D/dalvikvm(1266): GC_CONCURRENT freed 204K, 6% free 13512K/14343K, paused 17ms+5ms, total 37ms
请帮我找出错误,因为我是Android应用程序开发的新手。
答案 0 :(得分:0)
未初始化的对象。例如,eventsDB
中的AlertActivity.onCreate()
为空。你还没有初始化它。其他答案也指出了其他问题。
建议:
学习阅读堆栈跟踪。他们精确定位出现问题的确切源线。在这种情况下,AlertActivity.java:47
。
学习使用调试器。通过一次单步执行一行代码,您可以检查变量和程序状态,并说明它的行为原因。
答案 1 :(得分:0)
问题在于String ehour = String.valueOf(timePic.getCurrentHour());
,您永远不会初始化timePic
!另请注意,eventsDB
也为空。
根据评论,您可以使用String ehour = String.valueOf(c.get(Calendar.HOUR);
获取小时
分钟:String eminute = String.valueOf(c.get(Calendar.MINUTE);
答案 2 :(得分:0)
您不仅可以复制/粘贴logcat输出,还可以在发布问题之前阅读它。
09-14 19:55:29.959: E/AndroidRuntime(1266): Caused by: java.lang.NullPointerException
09-14 19:55:29.959: E/AndroidRuntime(1266): at com.example.eventmanager.AlertActivity.onCreate(AlertActivity.java:47)
这不是故障点吗? String ehour = String.valueOf(timePic.getCurrentHour());
是那里的代码行,所以也许你应该检查一下timePic
。似乎没有对此变量进行任何初始化,这与您的崩溃非常一致。