我有这两个班:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DatabaseHelper helper = new DatabaseHelper(this);
String[] temp = new String[] { "40", "35", "28" };
String[] hum = new String[] { "50%", "30%", "80%" };
for (int i = 0; i < temp.length; i++) {
helper.insertRecord(temp[i], hum[i]);
}
Cursor c = helper.getData();
String res = "";
if(c.moveToFirst()) {
while(!c.isAfterLast()) {
res+=c.getString(0)+" "+c.getString(1)+"\n";
c.moveToNext();
}
c.close();
}
TextView tv = (TextView) findViewById(R.id.tv);
tv.setText(res);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "database_name";
private static final String TABLE_NAME = "my_table";
private static final String COL_TEMPERATURA = "temperatura";
private static final String COL_UMIDITA = "umidita";
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, 33);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
db.execSQL("CREATE TABLE " + TABLE_NAME + " (" + COL_TEMPERATURA
+ " TEXT, " + COL_UMIDITA + " TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
public void insertRecord(String temperatura, String umidita) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COL_TEMPERATURA, temperatura);
cv.put(COL_UMIDITA, umidita);
db.insert(TABLE_NAME, null, cv);
db.close();
}
public Cursor getData() {
SQLiteDatabase db = this.getReadableDatabase();
String[] cols = new String[]{COL_TEMPERATURA, COL_UMIDITA};
Cursor res = db.query(TABLE_NAME, cols, null, null, null, null, null);
return res;
}
}
但是当我执行我的代码时,我在调用时在MainActivity上有一个nullpointer异常 tv.setText(RES);
为什么?
日志:
09-08 19:25:16.249: E/AndroidRuntime(5911): FATAL EXCEPTION: main
09-08 19:25:16.249: E/AndroidRuntime(5911): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.database/com.example.database.MainActivity}: java.lang.NullPointerException
09-08 19:25:16.249: E/AndroidRuntime(5911): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
09-08 19:25:16.249: E/AndroidRuntime(5911): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
09-08 19:25:16.249: E/AndroidRuntime(5911): at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-08 19:25:16.249: E/AndroidRuntime(5911): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
09-08 19:25:16.249: E/AndroidRuntime(5911): at android.os.Handler.dispatchMessage(Handler.java:99)
09-08 19:25:16.249: E/AndroidRuntime(5911): at android.os.Looper.loop(Looper.java:137)
09-08 19:25:16.249: E/AndroidRuntime(5911): at android.app.ActivityThread.main(ActivityThread.java:5103)
09-08 19:25:16.249: E/AndroidRuntime(5911): at java.lang.reflect.Method.invokeNative(Native Method)
09-08 19:25:16.249: E/AndroidRuntime(5911): at java.lang.reflect.Method.invoke(Method.java:525)
09-08 19:25:16.249: E/AndroidRuntime(5911): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-08 19:25:16.249: E/AndroidRuntime(5911): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-08 19:25:16.249: E/AndroidRuntime(5911): at dalvik.system.NativeStart.main(Native Method)
09-08 19:25:16.249: E/AndroidRuntime(5911): Caused by: java.lang.NullPointerException
09-08 19:25:16.249: E/AndroidRuntime(5911): at com.example.database.MainActivity.onCreate(MainActivity.java:31)
09-08 19:25:16.249: E/AndroidRuntime(5911): at android.app.Activity.performCreate(Activity.java:5133)
09-08 19:25:16.249: E/AndroidRuntime(5911): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-08 19:25:16.249: E/AndroidRuntime(5911): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
09-08 19:25:16.249: E/AndroidRuntime(5911): ... 11 more
答案 0 :(得分:0)
你必须测试光标c是否为空:
Cursor c = helper.getData();
String res = "";
if(c.moveToFirst() && c.getCount() != 0) {
while(!c.isAfterLast()) {
res+=c.getString(0)+" "+c.getString(1)+"\n";
c.moveToNext();
}
c.close();
}
答案 1 :(得分:0)
我建议你在线设置断点:
if(c.moveToFirst()) {
看一下步骤结束后变量“c”和“res”会发生什么。这应该为您提供足够的信息来确定发生了什么。
要完全确定“tv”已初始化,请尝试设置:
tv.setText("test");
只是为了确保“tv”不为空。