2012-06-20 29 views
1

主要活動:在Android中將微調器和其他文本值插入到sqlite數據庫時出現空指針異常?

private Button btnSubmit; 
private DataSource mDataSource; 
private Context mContext; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    final EditText first_name = (EditText) findViewById(R.id.first_name); 
    final Spinner relation = (Spinner) findViewById(R.id.relation); 
    final EditText address = (EditText) findViewById(R.id.address); 
    Button mSubmitButton = (Button) findViewById(R.id.btnSubmit); 
    relation.setOnItemSelectedListener(new CustomOnItemSelectedListener()); 
    mSubmitButton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       String fName = first_name.getText().toString(); 
       String re_lation = (String) relation.getSelectedItem(); 
       String add_ress = address.getText().toString(); 
       if ((fName.length() <=0) || (re_lation.length()<=0) || (add_ress.length()<=0)) { 
         Toast.makeText(MyAndroidAppActivity.this, "please fill the data", Toast.LENGTH_SHORT).show(); 
       } else{ 
         Log.i("name",fName); 
         Log.i("rel",re_lation); 
         Log.i("e",add_ress); 
         mDataSource.addUser(fName, re_lation, add_ress); 

       } 
      } 
    }); 
} 

DataSource.java

private SQLiteDatabase mSQLiteDatabase; 
private MySQLiteHelper mSQLiteHelper; 
private String[] mAllColumns = { 
     MySQLiteHelper.COLUMN_ID, MySQLiteHelper.COLUMN_FIRST_NAME, MySQLiteHelper.COLUMN_RELATION, MySQLiteHelper.COLUMN_ADDRESS 
}; 

public DataSource (Context context){ 
     mSQLiteHelper = new MySQLiteHelper(context); 
} 

public void open() throws SQLiteException { 
     mSQLiteDatabase = mSQLiteHelper.getWritableDatabase(); 
} 

public void close() { 
     mSQLiteHelper.close(); 
} 

public void addUser(String first_name, String relation, String address){ 
     ContentValues values = new ContentValues(); 
     Log.i("name",first_name); 
     values.put(MySQLiteHelper.COLUMN_FIRST_NAME, first_name); 
     values.put(MySQLiteHelper.COLUMN_RELATION, relation); 
     values.put(MySQLiteHelper.COLUMN_ADDRESS, address); 
     mSQLiteDatabase.insert(MySQLiteHelper.TABLE_USERS, null, values); 
     //Cursor cursor = mSQLiteDatabase.query(MySQLiteHelper.TABLE_USERS, allcolumns, selection, selectionArgs, groupBy, having, orderBy) 
} 

MySQLiteHelper.java

public class MySQLiteHelper extends SQLiteOpenHelper { 
     public static final String TABLE_USERS = "users"; 
     public static final String COLUMN_ID = "_id"; 
     public static final String COLUMN_FIRST_NAME = "first_name"; 
     public static final String COLUMN_RELATION = "relation"; 
     public static final String COLUMN_ADDRESS = "address"; 



     private static final String DATABASE_NAME = "users.db"; 
     private static final int DATABASE_VERSION = 1; 

     // Database creation sql statement 
     private static final String DATABASE_CREATE = "create table " 
       + TABLE_USERS + "(" + COLUMN_ID 
       + " integer primary key autoincrement, " + COLUMN_FIRST_NAME 
       + " text not null," + COLUMN_RELATION + " text not null," + 
       COLUMN_ADDRESS + " text not null);"; 



     public MySQLiteHelper(Context context) { 
      super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     } 

     @Override 
     public void onCreate(SQLiteDatabase db) { 
      db.execSQL(DATABASE_CREATE); 

     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
      db.execSQL("DROP TABLE IF EXISTS "+TABLE_USERS); 
      onCreate(db); 

     } 
} 

錯誤的logcat:

06-20 11:26:02.043: E/AndroidRuntime(342): FATAL EXCEPTION: main 
06-20 11:26:02.043: E/AndroidRuntime(342): java.lang.NullPointerException 
06-20 11:26:02.043: E/AndroidRuntime(342): at com.mkyong.android.MyAndroidAppActivity$1.onClick(MyAndroidAppActivity.java:62) 
06-20 11:26:02.043: E/AndroidRuntime(342): at android.view.View.performClick(View.java:2485) 
06-20 11:26:02.043: E/AndroidRuntime(342): at android.view.View$PerformClick.run(View.java:9080) 
06-20 11:26:02.043: E/AndroidRuntime(342): at android.os.Handler.handleCallback(Handler.java:587) 
06-20 11:26:02.043: E/AndroidRuntime(342): at android.os.Handler.dispatchMessage(Handler.java:92) 
06-20 11:26:02.043: E/AndroidRuntime(342): at android.os.Looper.loop(Looper.java:123) 
06-20 11:26:02.043: E/AndroidRuntime(342): at android.app.ActivityThread.main(ActivityThread.java:3683) 
06-20 11:26:02.043: E/AndroidRuntime(342): at java.lang.reflect.Method.invokeNative(Native Method) 
06-20 11:26:02.043: E/AndroidRuntime(342): at java.lang.reflect.Method.invoke(Method.java:507) 
06-20 11:26:02.043: E/AndroidRuntime(342): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
06-20 11:26:02.043: E/AndroidRuntime(342): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 

這裏我被T他null指針exeception同時插入微調數據庫中的微調數據庫中的微調框和其他文本值,在錯誤行mDataSource.addUser(fName, re_lation, add_ress);

我得到了三個值,但數據庫沒有創建here.i指出錯誤行,並提交值到數據庫中,它會拋出空指針異常。我的程序中有什麼錯誤,任何人都可以告訴我?

在此先感謝

+0

remove從View.onClickListener中刪除j ust嘗試onClickListener –

+0

onclicklisnter方法正在working.but mDataSource那行不在這裏調用。但我把正確的構造函數 – rajeshlawrance

回答

0

mDataSource尚未初始化

你應該初始化它在你的onCreate()回調

protected void onCreate(Bundle savedInstanceState) 
{ 

setContentView(R.layout.layout); 
mDataSource = new DataSource(getBaseContext()); 

// ... The Rest of your code 
} 

,並在您的數據源的構造函數中調用也是open()方法

public DataSource (Context context){ 

mSQLiteHelper = new MySQLiteHelper(context); 
open(); 
} 

public void open() throws SQLiteException { 
     mSQLiteDatabase = mSQLiteHelper.getWritableDatabase(); 
} 
+0

如何初始化mDataSource?我已經intialized上面oncreateInstance在這裏 – rajeshlawrance

+0

我看不到它。根據你的代碼.. – 2012-06-20 06:26:00

+0

ya.now它ok.but現在我得到另一個錯誤在行mSQLiteDatabase.insert(MySQLiteHelper.TABLE_USERS,null,值);在DataSource.java .... – rajeshlawrance

相關問題