我已经创建了一个基本程序来帮助学习SQLite& Android系统。该应用程序共有2项活动:
活动1:用户输入名字和姓氏,按下提交按钮。提交时,此数据将添加到数据库中,并调用活动#2。
活动2:此活动只是从数据库中获取最近的输入并将其显示在屏幕上。
问题是,我在运行代码时遇到NullPointer异常。我已经为这两个活动和我的数据库代码添加了相关代码,希望有人能告诉我出错的地方。感谢您给予的任何帮助。
活动1:
public class DBExampleActivity extends Activity implements OnClickListener{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Declare the database object and open.
MyDBManager db = new MyDBManager(this);
db.open();
// Take names from EditTexts and store in string.
first_name = fname.getText().toString();
last_name = lname.getText().toString();
}
@Override
public void onClick(View view) {
// In the onclick method, add values to database, call next activity
db.insertData(first_name, last_name);
Intent intent = new Intent(this, Display.class);
startActivity(intent);
}}
活动2:
public class Display extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Cursor myCursor = db.getDetails();
// Get relevant data from columns in cursor.
username = c.getString(c.getColumnIndex("first_name"));
password = c.getString(c.getColumnIndex("last_name"));
// set textview values.
fname.setText(first_name);
lname.setText(last_name);
}}
数据库代码:
public class MyDBManager {
public static final String KEY_ROWID = "id";
public static final String KEY_USERNAME = "firstname";
public static final String KEY_PASSWORD = "lastname";
private static final String DATABASE_NAME = "User_Information";
private static final String DATABASE_TABLE = "User_name";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE =
"create table " + DATABASE_TABLE +
" (_id integer primary key autoincrement, " +
"username text not null, " +
"password text not null); " ;
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public MyDBManager(Context context){
this.context = context;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper{
DatabaseHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creates the database
@Override
public void onCreate(SQLiteDatabase db){
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
// on triggered if the database version has increased.
// used if you want to change structure of column: eg add new column.
}
} // end helper class
// Methods used to access and change data in DB:
public MyDBManager open() throws SQLException{
db = DBHelper.getWritableDatabase();
return this;
}
public void close(){
DBHelper.close();
}
public long insertData(String username, String password){
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_USERNAME, username);
initialValues.put(KEY_PASSWORD, password);
return db.insert(DATABASE_TABLE, null, initialValues);
}
public Cursor getDetails(long rowId){
Cursor c = db.query(true, DATABASE_TABLE, new String[]{
KEY_ROWID,
KEY_USERNAME,
KEY_PASSWORD},
KEY_ROWID + "=" + rowId,
null,
null,
null,
null,
null);
if(c != null){
c.moveToFirst();
}
return c;
}}// End myDBManager Class.
编辑:添加了LOGCAT
02-07 16:36:59.376:E / AndroidRuntime(7138):致命异常:主要 02-07 16:36:59.376:E / AndroidRuntime(7138):java.lang.RuntimeException:无法启动活动ComponentInfo {com.android.dbex / com.android.dbex.Display}:java.lang.NullPointerException 02-07 16:36:59.376:E / AndroidRuntime(7138):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2787) 02-07 16:36:59.376:E / AndroidRuntime(7138):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803) 02-07 16:36:59.376:E / AndroidRuntime(7138):在android.app.ActivityThread.access $ 2300(ActivityThread.java:135) 02-07 16:36:59.376:E / AndroidRuntime(7138):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2136) 02-07 16:36:59.376:E / AndroidRuntime(7138):在android.os.Handler.dispatchMessage(Handler.java:99) 02-07 16:36:59.376:E / AndroidRuntime(7138):在android.os.Looper.loop(Looper.java:144) 02-07 16:36:59.376:E / AndroidRuntime(7138):在android.app.ActivityThread.main(ActivityThread.java:4937) 02-07 16:36:59.376:E / AndroidRuntime(7138):at java.lang.reflect.Method.invokeNative(Native Method) 02-07 16:36:59.376:E / AndroidRuntime(7138):at java.lang.reflect.Method.invoke(Method.java:521) 02-07 16:36:59.376:E / AndroidRuntime(7138):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868) 02-07 16:36:59.376:E / AndroidRuntime(7138):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 02-07 16:36:59.376:E / AndroidRuntime(7138):at dalvik.system.NativeStart.main(Native Method) 02-07 16:36:59.376:E / AndroidRuntime(7138):引起:java.lang.NullPointerException 02-07 16:36:59.376:E / AndroidRuntime(7138):at com.android.dbex.Display.onCreate(Display.java:26) 02-07 16:36:59.376:E / AndroidRuntime(7138):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069) 02-07 16:36:59.376:E / AndroidRuntime(7138):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751) 02-07 16:36:59.376:E / AndroidRuntime(7138):... 11 more
答案 0 :(得分:0)
您尚未在第二项活动中实例化您的观点:
TextView fname = (TextView) findViewById(R.id.mytextview1);
TextView lname = (TextView) findViewById(R.id.mytextview2);
// set textview values.
fname.setText(first_name);
lname.setText(last_name);