2014-01-21 65 views
0

enter image description here我想加載到Android上的SQLite表的值。當我點擊按鈕時,出現空指針異常。請爲logcat.so找到附加的圖像以將值存儲到sqlite數據庫表中。android數據庫越來越空指針例外

public class MainActivity extends Activity { 


    EditText edt1,edt2; 
    DataAdapter adapter; 
    //String valueName,valueAddress; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

    // database =new DataAdapter(this);   
     edt1 =(EditText)findViewById(R.id.editText1); 
     edt2 =(EditText)findViewById(R.id.editText2); 
     // sdb = database.getWritableDatabase(); 

     Button button =(Button)findViewById(R.id.button1); 

     button.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 

       String valueName =edt1.getText().toString().trim(); 
       String valueAddress =edt2.getText().toString().trim(); 
      long value = adapter.insertData(valueName,valueAddress); 

      if (value < 0) { 

       Toast.makeText(getApplicationContext(), "Data not Added ",Toast.LENGTH_SHORT).show(); 
      } else { 
       Toast.makeText(getApplicationContext(), "Data Added",Toast.LENGTH_SHORT).show(); 
      }       
      }    
     }); 

    } 

} 


public class DataAdapter { 

     DataBase database; 
     SQLiteDatabase db;  

public DataAdapter(Context context) { 
    // TODO Auto-generated constructor stub 
    database =new DataBase(context);  
} 

public long insertData(String name ,String address){  

    SQLiteDatabase db= database.getWritableDatabase(); 
    ContentValues values = new ContentValues(); 
    values.put(DataBase.NAME, name); 
    values.put(DataBase.ADDRESS, address); 
    return db.insert(DataBase.TABLENAME, null,values); 
    } 

public static class DataBase extends SQLiteOpenHelper{ 

    public static final String DATABASE="DataBase"; 
    public static final String TABLENAME="TableName"; 
    public static final int VERSION = 5; 
    public static final String NAME="name"; 
    public static final String ADDRESS="address"; 
    public static final String ID="_id"; 

    public static final String CREATE_TABLE = "create table "+TABLENAME+"("+ID+"" + 
      "INTEGER PRIMARY KEY AUTOINCREMENT,"+NAME+" VARCHAR(255),"+ADDRESS+" VARCHAR(255));"; 

    private Context context; 

    public DataBase(Context context) { 
     super(context, DATABASE, null, VERSION);   
     this.context =context; 
     // TODO Auto-generated constructor stub  
     Toast.makeText(context, "onDataBase",Toast.LENGTH_SHORT).show(); 

    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     // TODO Auto-generated method stub 

     try { 
      Toast.makeText(context, "oncreate",Toast.LENGTH_SHORT).show(); 
      db.execSQL(CREATE_TABLE); 
     } catch (SQLException e) { 
      // TODO: handle exception 
     }  

    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // TODO Auto-generated method stub 
     try { 
      Toast.makeText(context, "oncUpgrade",Toast.LENGTH_SHORT).show(); 
      db.execSQL("DROP TABLE IF EXITS"+TABLENAME); 
      onCreate(db); 
     } catch (SQLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 

} 



}][1] 
+1

後錯誤日誌.... –

+0

是什麼MainActivity.java線42? – Raghunandan

+0

long value = adapter.insertData(valueName,valueAddress); – arunk

回答

3
DataAdapter adapter; 

是不是在你的代碼,用來初始化它初始化,然後use..like

adapter = new DataAdapter(this);