我已经从 SQLite 实现了填充列表视图。当我运行我的应用程序时,我在创建表中遇到了一些错误,例如语法错误。我不知道问题出在哪里。
这是我的 DataBaseHelper 类:
public class DataBaseHelper extends SQLiteOpenHelper
{
// private String TABLE_NEW_LEAD;
public DataBaseHelper(Context context, String name,CursorFactory factory, int version)
{
super(context, name, factory, version);
}
// Called when no database exists in disk and the helper class needs
// to create a new one.
@Override
public void onCreate(SQLiteDatabase _db)
{
_db.execSQL(DataBase_Adapter.DATABASE_CREATE_LOGIN);
//_db.execSQL(DataBase_Adapter.CREATE_NEW_LEAD_TABLE);
_db.execSQL(DataBase_Adapter.DATABASE_CREATE_DEFINE_PRODUCT);
}
// Called when there is a database version mismatch meaning that the version
// of the database on disk needs to be upgraded to the current version.
@Override
public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion)
{
// Log the version upgrade.
Log.w("TaskDBAdapter", "Upgrading from version " +_oldVersion + " to " +_newVersion + ", which will destroy all old data");
_db.execSQL("DROP TABLE IF EXISTS " + "TEMPLATE");
_db.execSQL("DROP TABLE IF EXISTS " + DataBase_Adapter.TABLE_DEFINE_PRODUCT);
// Create a new one.
onCreate(_db);
}
}
这是 DataBaseAdapter 类
public class DataBase_Adapter
{
//Database NAme
static final String DATABASE_NAME = "lead_management.db";
//Database Version
static final int DATABASE_VERSION = 1;
// Variable to hold the database instance
public SQLiteDatabase db;
// Context of the application using the database.
private final Context context;
// Database open/upgrade helper
private DataBaseHelper dbHelper;
public DataBase_Adapter(Context _context)
{
context = _context;
dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public DataBase_Adapter open() throws SQLException
{
db = dbHelper.getWritableDatabase();
return this;
}
public void close()
{
db.close();
}
public SQLiteDatabase getDatabaseInstance()
{
return db;
}
//Table Name
static final String TABLE_DEFINE_PRODUCT="define_product";
//Creating Define Product Table Column
static final String KEY_DEFINE_PRODUCT_ID="id";
static final String KEY_DEFINE_PRODUCT_NAME="name";
static final String KEY_DEFINE_PRODUCT_DESCRIPTION="description";
//// SQL Statement to create a Product define Database.
static final String DATABASE_CREATE_DEFINE_PRODUCT = "CREATE TABLE"+TABLE_DEFINE_PRODUCT+"("+ KEY_DEFINE_PRODUCT_ID +" INTEGER PRIMARY KEY,"+ KEY_DEFINE_PRODUCT_NAME + "TEXT,"+ KEY_DEFINE_PRODUCT_DESCRIPTION + "TEXT,"+")";
//Insert Products
@SuppressWarnings("unused")
private final ArrayList<Define_Products_BeanClass> defin_Products_list = new ArrayList<Define_Products_BeanClass>();
public void insert_Product(Define_Products_BeanClass definProductBeanClass)
{
SQLiteDatabase product_db = dbHelper.getWritableDatabase();
ContentValues contentDefinProduct_Val=new ContentValues();
contentDefinProduct_Val.put(KEY_DEFINE_PRODUCT_NAME, definProductBeanClass.get_ProductName());
contentDefinProduct_Val.put(KEY_DEFINE_PRODUCT_DESCRIPTION, definProductBeanClass.get_ProductDescription());
product_db.insert(TABLE_DEFINE_PRODUCT, null, contentDefinProduct_Val);
//Close the Database connection
product_db.close();
}
这是产品列表适配器类:
public class Product_List_Adapter extends BaseAdapter
{
@SuppressWarnings("unused")
private Context mContext;
private ArrayList<String> Productid_ArrayList;
private ArrayList<String> ProductName_ArrayList;
private ArrayList<String> ProductDescription_ArrayList;
public Product_List_Adapter(Context mContext,
ArrayList<String> productid_ArrayList,
ArrayList<String> productName_ArrayList,
ArrayList<String> productDescription_ArrayList)
{
super();
this.mContext = mContext;
Productid_ArrayList = productid_ArrayList;
ProductName_ArrayList = productName_ArrayList;
ProductDescription_ArrayList = productDescription_ArrayList;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return Productid_ArrayList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int pos, View child, ViewGroup parent) {
// TODO Auto-generated method stub
Holder mHolder;
LayoutInflater layoutInflater;
if(child == null)
{
layoutInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
child = layoutInflater .inflate(R.layout.define_products_listrow, null);
mHolder = new Holder();
mHolder.txt_product_id = (TextView)child.findViewById(R.id.txt_ProductId);
mHolder.txt_product_name = (TextView)child.findViewById(R.id.txt_ProductName);
mHolder.txt_product_description = (TextView)child.findViewById(R.id.txt_ProductDescr);
child.setTag(mHolder);
}
else
{
mHolder = (Holder) child.getTag();
}
mHolder.txt_product_id.setText(Productid_ArrayList.get(pos));
mHolder.txt_product_name.setText(ProductName_ArrayList.get(pos));
mHolder.txt_product_description.setText(ProductDescription_ArrayList.get(pos));
return child;
}
public class Holder {
TextView txt_product_id;
TextView txt_product_name;
TextView txt_product_description;
}
}
这是 Product_Activity 类:
public class DefineProducts_Activity6 extends Activity
{
Button btnAdd_Products;
Button btnCancel_Products;
EditText edTxt_ProductName,edTxt_ProductDescription;
Dialog myDialog;
private DataBase_Adapter dbAdapter;
private SQLiteDatabase dataBase;
private DataBaseHelper dbHelper;
private ArrayList<String> productId = new ArrayList<String>();
private ArrayList<String> productName = new ArrayList<String>();
private ArrayList<String> productDescription = new ArrayList<String>();
private ListView productsList;
private AlertDialog.Builder build;
@SuppressWarnings("unused")
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.define_products_listview);
dbAdapter=new DataBase_Adapter(this);
dbAdapter=dbAdapter.open();
productsList = (ListView)findViewById(R.id.products_List);
/**__________________Button Add_______________________________________**/
//Add Products In Listview
btnAdd_Products=(Button)findViewById(R.id.btnAdd_DefineProduct);
btnAdd_Products.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
myDialog=new Dialog(DefineProducts_Activity6.this);
myDialog.setContentView(R.layout.define_product_details);
myDialog.setTitle("Defin Product Deatail");
myDialog.setCancelable(false);
edTxt_ProductName=(EditText)findViewById(R.id.editText_ProductName);
edTxt_ProductDescription=(EditText)findViewById(R.id.editText_ProductDescription);
/**_______________________________Button Save________________________________________**/
Button btn_DefineProduct=(Button)myDialog.findViewById(R.id.btn_Define);
btn_DefineProduct.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String strProductsName = edTxt_ProductName.getText().toString().trim();
String strProductDescription = edTxt_ProductDescription.getText().toString().trim();
if(strProductsName.length()>0 && strProductDescription.length()>0)
{
dbAdapter.insert_Product(new Define_Products_BeanClass (strProductsName,strProductDescription));
Toast.makeText(DefineProducts_Activity6.this, "Ur Product Is Define", Toast.LENGTH_LONG).show();
}
}
});
/**_______________________________Button Cancel ________________________________________**/
btnCancel_Products=(Button)myDialog.findViewById(R.id.btn_cancel_Define);
btnCancel_Products.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myDialog.dismiss();
}
});
myDialog.show();
}
});
/**_______________________________________________________________________**/
}
// displays data from SQLite
@SuppressWarnings("unused")
private void displayData()
{
dataBase = dbHelper . getWritableDatabase();
Cursor mCursor = dataBase.rawQuery("SELECT * FROM " + DataBase_Adapter.TABLE_DEFINE_PRODUCT , null);
productId.clear();
productName.clear();
productDescription.clear();
if(mCursor.moveToFirst())
{
do
{
productId.add(mCursor.getString(mCursor.getColumnIndex(DataBase_Adapter.KEY_DEFINE_PRODUCT_ID)));
productName.add(mCursor.getString(mCursor.getColumnIndex(DataBase_Adapter.KEY_DEFINE_PRODUCT_NAME)));
productDescription.add(mCursor.getString(mCursor.getColumnIndex(DataBase_Adapter.KEY_DEFINE_PRODUCT_DESCRIPTION)));
}while (mCursor.moveToNext());
}
Product_List_Adapter productAdapter = new Product_List_Adapter(DefineProducts_Activity6.this ,productId ,productName , productDescription);
productsList.setAdapter(productAdapter);
mCursor.close();
}
@Override
protected void onResume() {
displayData();
super.onResume();
}
}
这是我的 Log Cat,为什么我无法恢复活动?
.11-06 12:57:32.389: W/dalvikvm(11676): threadid=1: thread exiting with uncaught exception (group=0x40015560)
11-06 12:57:32.399: E/AndroidRuntime(11676): FATAL EXCEPTION: main
11-06 12:57:32.399: E/AndroidRuntime(11676): java.lang.RuntimeException: Unable to resume activity {com.leadmgmt/com.leadmgmt.DefineProducts_Activity6}: java.lang.NullPointerException
11-06 12:57:32.399: E/AndroidRuntime(11676): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2120)
11-06 12:57:32.399: E/AndroidRuntime(11676): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2135)
11-06 12:57:32.399: E/AndroidRuntime(11676): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1668)
11-06 12:57:32.399: E/AndroidRuntime(11676): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-06 12:57:32.399: E/AndroidRuntime(11676): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
11-06 12:57:32.399: E/AndroidRuntime(11676): at android.os.Handler.dispatchMessage(Handler.java:99)
11-06 12:57:32.399: E/AndroidRuntime(11676): at android.os.Looper.loop(Looper.java:123)
11-06 12:57:32.399: E/AndroidRuntime(11676): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-06 12:57:32.399: E/AndroidRuntime(11676): at java.lang.reflect.Method.invokeNative(Native Method)
11-06 12:57:32.399: E/AndroidRuntime(11676): at java.lang.reflect.Method.invoke(Method.java:507)
11-06 12:57:32.399: E/AndroidRuntime(11676): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-06 12:57:32.399: E/AndroidRuntime(11676): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-06 12:57:32.399: E/AndroidRuntime(11676): at dalvik.system.NativeStart.main(Native Method)
11-06 12:57:32.399: E/AndroidRuntime(11676): Caused by: java.lang.NullPointerException
11-06 12:57:32.399: E/AndroidRuntime(11676): at com.leadmgmt.DefineProducts_Activity6.displayData(DefineProducts_Activity6.java:80)
11-06 12:57:32.399: E/AndroidRuntime(11676): at com.leadmgmt.DefineProducts_Activity6.onResume(DefineProducts_Activity6.java:74)
11-06 12:57:32.399: E/AndroidRuntime(11676): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150)
11-06 12:57:32.399: E/AndroidRuntime(11676): at android.app.Activity.performResume(Activity.java:3832)
11-06 12:57:32.399: E/AndroidRuntime(11676): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2110)
11-06 12:57:32.399: E/AndroidRuntime(11676): ... 12 more