我的代码中的一行上的Null Pointer Exception出现错误,我不知道如何修复它.它基本上只是另一种方法的副本,所以我不知道它为什么会给我错误.任何帮助,将不胜感激:
import java.util.Calendar;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
public class CurrentLesson extends Activity {
TabHost th;
TabSpec specs;
TimingsDb tDb = new TimingsDb(this);
Calendar c;
String lesson;
String week;
String day;
TextView tvTab1;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.current_lesson_tabs);
c = Calendar.getInstance();
tvTab1 = (TextView) findViewById(R.id.textViewCLW1tv1);
setupTabs();
int h = c.get(Calendar.HOUR_OF_DAY);
int m = c.get(Calendar.MINUTE);
int w = c.get(Calendar.DAY_OF_WEEK);
if ((h == 9) && (m <= 50)) {
lesson = "1";
} else if (((h == 9)&&((m > 50)&&(m <=59))) || ((h == 10)&&(m<=40))) {
lesson = "2";
} else if ((h == 11)&&(m<=50)) {
lesson = "3";
} else if (((h == 12)&&((m > 50)&&(m <=59))) || ((h == 13)&&(m<=40))) {
lesson = "4";
} else if (((h == 13)&&((m > 40)&&(m <=59))) || ((h == 14)&&(m<=30))) {
lesson = "5";
} else if (((h == 14)&&((m > 30)&&(m <=59))) || ((h == 15)&&(m<=20))) {
lesson = "6";
} else {
lesson = "0";
}
switch(w){
case 2:
day = "Monday";
break;
case 3:
day = "Tuesday";
break;
case 4:
day = "Wednesday";
break;
case 5:
day = "Thursday";
break;
case 6:
day = "Friday";
break;
default:
day = "";
}//end switch
week = "1";
String temp;
temp = tDb.getCurrent(day,week,lesson); //THIS IS WHERE I GET THE EXCEPTION
if(!(temp == null)){
tvTab1.setText(temp);
}
}// end onCreate
public void setupTabs() {
th = (TabHost) findViewById(R.id.tabhost2);
th.setup();
specs = th.newTabSpec("tag1");
specs.setContent(R.id.tabCLWeek1);
specs.setIndicator("Week One");
th.addTab(specs);
specs = th.newTabSpec("tag2");
specs.setContent(R.id.tabCLWeek2);
specs.setIndicator("Week Two");
th.addTab(specs);
}
}// endClass
这是我认为导致错误的getCurrent()方法:
public String getCurrent(String day, String week, String lesson) {
// TODO Auto-generated method stub
String[] column = new String[] { KEY_SUBJECT };
String selection = KEY_DAY + " = ? AND " + KEY_WEEK + " = ? AND "
+ KEY_LESSON + " = ?";
String[] selectionArgs = new String[] { day, week, lesson };
Cursor c = ourDatabase.query(DATABASE_TABLE, column, selection,
selectionArgs, null, null, null);
;
String result = "";
if (c.moveToFirst()) {
int iSubject = c.getColumnIndex(KEY_SUBJECT);
result = c.getString(iSubject);
}
c.close();
if (result == null) {
result = "";
}
return result;
}
这是logcat:
03-18 09:46:06.259: E/AndroidRuntime(20000): FATAL EXCEPTION: main
03-18 09:46:06.259: E/AndroidRuntime(20000): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.shearer.schooltimetable/com.shearer.schooltimetable.CurrentLesson}: java.lang.NullPointerException
03-18 09:46:06.259: E/AndroidRuntime(20000): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205)
03-18 09:46:06.259: E/AndroidRuntime(20000): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2255)
03-18 09:46:06.259: E/AndroidRuntime(20000): at android.app.ActivityThread.access$600(ActivityThread.java:146)
03-18 09:46:06.259: E/AndroidRuntime(20000): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1240)
03-18 09:46:06.259: E/AndroidRuntime(20000): at android.os.Handler.dispatchMessage(Handler.java:99)
03-18 09:46:06.259: E/AndroidRuntime(20000): at android.os.Looper.loop(Looper.java:137)
03-18 09:46:06.259: E/AndroidRuntime(20000): at android.app.ActivityThread.main(ActivityThread.java:5171)
03-18 09:46:06.259: E/AndroidRuntime(20000): at java.lang.reflect.Method.invokeNative(Native Method)
03-18 09:46:06.259: E/AndroidRuntime(20000): at java.lang.reflect.Method.invoke(Method.java:511)
03-18 09:46:06.259: E/AndroidRuntime(20000): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
03-18 09:46:06.259: E/AndroidRuntime(20000): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:564)
03-18 09:46:06.259: E/AndroidRuntime(20000): at dalvik.system.NativeStart.main(Native Method)
03-18 09:46:06.259: E/AndroidRuntime(20000): Caused by: java.lang.NullPointerException
03-18 09:46:06.259: E/AndroidRuntime(20000): at com.shearer.schooltimetable.TimingsDb.getCurrent(TimingsDb.java:132)
03-18 09:46:06.259: E/AndroidRuntime(20000): at com.shearer.schooltimetable.CurrentLesson.onCreate(CurrentLesson.java:77)
03-18 09:46:06.259: E/AndroidRuntime(20000): at android.app.Activity.performCreate(Activity.java:5200)
03-18 09:46:06.259: E/AndroidRuntime(20000): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-18 09:46:06.259: E/AndroidRuntime(20000): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
03-18 09:46:06.259: E/AndroidRuntime(20000): ... 11 more
解决方法:
似乎ourDatabase为null.请确保正确启动它.或者您可以尝试替换它(假设TimingsDb是打开助手的包装器):
Cursor c = ourDatabase.query(DATABASE_TABLE, column, selection,
selectionArgs, null, null, null);
有了这个:
Cursor c = this.getWritableDatabase().query(DATABASE_TABLE, column, selection,
selectionArgs, null, null, null);