有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Android中SQLite的java运行时异常

我正在构建一个包含listview的应用程序,listview的数据是从SQlite db填充的。我总共插入了11-20列。我一直试图从数据库中填充listview,但遇到运行时异常

下面是代码。我试过很多教程,但没有成功

这是我的数据库代码

public class DataBaseHelper extends SQLiteOpenHelper
{
 public DataBaseHelper(Context context, String name,CursorFactory factory, int version) 
 {
           super(context, name, factory, version);
}

@Override
public void onCreate(SQLiteDatabase _db) 
{

       _db.execSQL(DataBase_Adapter.CREATE_NEW_LEAD_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion) 
{

        _db.execSQL("DROP TABLE IF EXISTS " + DataBase_Adapter.TABLE_NEW_LEAD);

        onCreate(_db);
}

}


public class DataBase_Adapter 
{

        //Database NAme
        static final String DATABASE_NAME = "lead_management.db";

        //Database Version
        static final int DATABASE_VERSION = 4;


        // 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
        public static  String TABLE_NEW_LEAD="new_lead";

        //Creating New Lead Table Columns
        public static final String KEY_NEW_LEAD_ID ="id"; 
        public static final String KEY_NEW_LEAD_NAME ="name"; 
        public static final String KEY_NEW_LEAD_EMAIL ="email";
        public static final String KEY_NEW_LEAD_MOBILE="mobile";
        public static final String KEY_NEW_LEAD_Product="define_products";
        public static final String KEY_NEW_LEAD_BUDGET="budget";
        public static final String KEY_NEW_LEAD_PRIORITY="priority";
        public static final String KEY_NEW_LEAD_STATUS="status";
        public static final String KEY_NEW_LEAD_NOTES="notes";
        public static final String KEY_NEW_LEAD_REMINDER_DATE="reminder_date";
        public static final String KEY_NEW_LEAD_REMINDER_TIME="reminder_time";
        public static final String KEY_NEW_LEAD_ADDtoCONTACTS="add_to_contacts";


        //// SQL Statement to create a New Lead Database.

       static final String  CREATE_NEW_LEAD_TABLE = "CREATE TABLE "+ TABLE_NEW_LEAD + "("
                                        + KEY_NEW_LEAD_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," 
                                        + KEY_NEW_LEAD_NAME + " TEXT,"
                                        + KEY_NEW_LEAD_EMAIL + " TEXT,"
                                        + KEY_NEW_LEAD_MOBILE+ " TEXT,"
                                        + KEY_NEW_LEAD_Product + " TEXT,"
                                        + KEY_NEW_LEAD_BUDGET + " TEXT,"
                                        + KEY_NEW_LEAD_PRIORITY +" TEXT,"
                                        + KEY_NEW_LEAD_STATUS + " TEXT,"
                                        + KEY_NEW_LEAD_NOTES + " TEXT,"
                                        + KEY_NEW_LEAD_REMINDER_DATE + " TEXT,"
                                        + KEY_NEW_LEAD_REMINDER_TIME + " TEXT,"
                                        + KEY_NEW_LEAD_ADDtoCONTACTS + " TEXT"+");"; 
     //");";
        //Insert New Record In New Lead Table

      public void insert_NewLead_Entry(New_Lead_BeanClass newLead_BeanClass)
      {
          SQLiteDatabase sdb = dbHelper.getWritableDatabase();

          ContentValues contentNewLead_Val=new ContentValues();
          contentNewLead_Val.put(KEY_NEW_LEAD_NAME, newLead_BeanClass.get_Name());
          contentNewLead_Val.put(KEY_NEW_LEAD_EMAIL, newLead_BeanClass.get_Email());
          contentNewLead_Val.put(KEY_NEW_LEAD_MOBILE, newLead_BeanClass.get_MobileNo());
          contentNewLead_Val.put(KEY_NEW_LEAD_Product, newLead_BeanClass.get_Product());
          contentNewLead_Val.put(KEY_NEW_LEAD_BUDGET, newLead_BeanClass.get_Budget());
          contentNewLead_Val.put(KEY_NEW_LEAD_PRIORITY, newLead_BeanClass.get_Priority());
          contentNewLead_Val.put(KEY_NEW_LEAD_STATUS, newLead_BeanClass.get_Status());
          contentNewLead_Val.put(KEY_NEW_LEAD_NOTES, newLead_BeanClass.get_Notes());
          contentNewLead_Val.put(KEY_NEW_LEAD_REMINDER_DATE, newLead_BeanClass.get_Reminder_Date());
          contentNewLead_Val.put(KEY_NEW_LEAD_REMINDER_TIME, newLead_BeanClass.get_Reminder_Time());
          contentNewLead_Val.put(KEY_NEW_LEAD_ADDtoCONTACTS, newLead_BeanClass.get_AddtoContact());

          sdb.insert(TABLE_NEW_LEAD , null , contentNewLead_Val );

          //Close The Database Connection
          sdb.close();

      }


      public ArrayList<HashMap<String,String>> getAllUserData()
      {
          ArrayList<HashMap<String,String>> newLeadDat_Listl;
          newLeadDat_Listl = new ArrayList<HashMap<String,String>>();
          SQLiteDatabase sdatabase = dbHelper.getWritableDatabase();
          String selectQuery= "SELECT  * FROM" +  TABLE_NEW_LEAD ;

          Cursor cursor = sdatabase.rawQuery(selectQuery, null);

          if (cursor.moveToFirst()) 
          {
                do 
                {

                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put(KEY_NEW_LEAD_ID, cursor.getString(0));
                    map.put(KEY_NEW_LEAD_NAME, cursor.getString(1));
                    map.put(KEY_NEW_LEAD_EMAIL, cursor.getString(2));
                    map.put(KEY_NEW_LEAD_MOBILE, cursor.getString(3));
                    map.put(KEY_NEW_LEAD_Product, cursor.getString(4));
                    map.put(KEY_NEW_LEAD_BUDGET, cursor.getString(5));
                    map.put(KEY_NEW_LEAD_PRIORITY, cursor.getString(6));
                    map.put(KEY_NEW_LEAD_STATUS, cursor.getString(7));
                    map.put(KEY_NEW_LEAD_NOTES, cursor.getString(8));
                    map.put(KEY_NEW_LEAD_REMINDER_DATE, cursor.getString(9));
                    map.put(KEY_NEW_LEAD_REMINDER_TIME, cursor.getString(10));
                    map.put(KEY_NEW_LEAD_ADDtoCONTACTS, cursor.getString(11));

                    newLeadDat_Listl.add(map);
                } 
                while (cursor.moveToNext());
            }

        return newLeadDat_Listl;

      }


}

这是我的活动代码

public class Serach_Data extends Activity
{
    private static ArrayList<String> arrayList_newLead_Id = new ArrayList<String>();
    private static ArrayList<String> arrayList_newLead_Name = new ArrayList<String>();
    private static ArrayList<String> arrayList_newLead_Email = new ArrayList<String>();
    private static ArrayList<String> arrayList_newLead_Mobile = new ArrayList<String>();
    private static ArrayList<String> arrayList_newLead_Products = new ArrayList<String>();
    private static ArrayList<String> arrayList_newLead_Budget = new ArrayList<String>();
    private static ArrayList<String> arrayList_newLead_Priority = new ArrayList<String>();
    private static ArrayList<String> arrayList_newLead_Status = new ArrayList<String>();
    private static ArrayList<String> arrayList_newLead_Notes = new ArrayList<String>();
    private static ArrayList<String> arrayList_newLead_Reminder_Date = new ArrayList<String>();
    private static ArrayList<String> arrayList_newLead_Reminder_Time = new ArrayList<String>();
    private static ArrayList<String> arrayList_newLead_AddToContact = new ArrayList<String>();

    New_Lead_List_Adapter new_Lead_List_Adapter;
    private SQLiteDatabase dataBase;
    private DataBaseHelper dbHelper;
    DataBase_Adapter dbAdapter;
    private ListView newLeadDat_List;
    Context context;



    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_data);

        newLeadDat_List = (ListView)findViewById(R.id.listView_DisplayData);



        System.out.println("Data Displayed Succesfully!!!!!!!!!");
    }

    @Override
    protected void onResume() {
        displayNewLeadData();
        super.onResume();
    }

    public void displayNewLeadData() 
    {

        dataBase = dbHelper.getWritableDatabase();

        Cursor mCursor = dataBase.rawQuery("SELECT * FROM " + DataBase_Adapter.TABLE_NEW_LEAD, null);

        arrayList_newLead_Id.clear();
        arrayList_newLead_Name.clear();
        arrayList_newLead_Email.clear();
        arrayList_newLead_Mobile.clear();
        arrayList_newLead_Products.clear();
        arrayList_newLead_Budget.clear();
        arrayList_newLead_Priority.clear();
        arrayList_newLead_Status.clear();
        arrayList_newLead_Notes.clear();
        arrayList_newLead_Reminder_Date.clear();
        arrayList_newLead_Reminder_Time.clear();
        arrayList_newLead_AddToContact.clear();



        {
            do 
            {
                arrayList_newLead_Id.add(mCursor.getString(mCursor.getColumnIndex(DataBase_Adapter.KEY_NEW_LEAD_ID)));
                arrayList_newLead_Name.add(mCursor.getString(mCursor.getColumnIndex(DataBase_Adapter.KEY_NEW_LEAD_NAME)));
                arrayList_newLead_Email.add(mCursor.getString(mCursor.getColumnIndex(DataBase_Adapter.KEY_NEW_LEAD_EMAIL)));
                arrayList_newLead_Mobile.add(mCursor.getString(mCursor.getColumnIndex(DataBase_Adapter.KEY_NEW_LEAD_MOBILE)));
                arrayList_newLead_Products.add(mCursor.getString(mCursor.getColumnIndex(DataBase_Adapter.KEY_NEW_LEAD_Product)));
                arrayList_newLead_Budget.add(mCursor.getString(mCursor.getColumnIndex(DataBase_Adapter.KEY_NEW_LEAD_BUDGET)));
                arrayList_newLead_Priority.add(mCursor.getString(mCursor.getColumnIndex(DataBase_Adapter.KEY_NEW_LEAD_PRIORITY)));
                arrayList_newLead_Status.add(mCursor.getString(mCursor.getColumnIndex(DataBase_Adapter.KEY_NEW_LEAD_STATUS)));
                arrayList_newLead_Notes.add(mCursor.getString(mCursor.getColumnIndex(DataBase_Adapter.KEY_NEW_LEAD_NOTES)));
                arrayList_newLead_Reminder_Date.add(mCursor.getString(mCursor.getColumnIndex(DataBase_Adapter.KEY_NEW_LEAD_REMINDER_DATE)));
                arrayList_newLead_Reminder_Time.add(mCursor.getString(mCursor.getColumnIndex(DataBase_Adapter.KEY_NEW_LEAD_REMINDER_TIME)));
                arrayList_newLead_AddToContact.add(mCursor.getString(mCursor.getColumnIndex(DataBase_Adapter.KEY_NEW_LEAD_ADDtoCONTACTS)));



            } while (mCursor.moveToNext());
        }

        new_Lead_List_Adapter = new New_Lead_List_Adapter(Serach_Data.this , arrayList_newLead_Id ,
                                                        arrayList_newLead_Name , arrayList_newLead_Email,
                                                        arrayList_newLead_Mobile , arrayList_newLead_Products,
                                                        arrayList_newLead_Budget , arrayList_newLead_Priority ,
                                                        arrayList_newLead_Status , arrayList_newLead_Notes ,
                                                        arrayList_newLead_Reminder_Date , arrayList_newLead_Reminder_Time ,
                                                        arrayList_newLead_AddToContact );

        newLeadDat_List.setAdapter(new_Lead_List_Adapter);
        new_Lead_List_Adapter.notifyDataSetChanged();
        mCursor.close();
        System.out.println("Data will Be Display.");

    }


}

这是我的日志猫堆栈跟踪

12-02 17:46:15.301: E/AndroidRuntime(333): FATAL EXCEPTION: main
12-02 17:46:15.301: E/AndroidRuntime(333): java.lang.RuntimeException: Unable to resume activity {com.lead_management_project/com.lead_management_project.Serach_Data}: java.lang.NullPointerException
12-02 17:46:15.301: E/AndroidRuntime(333):  at 安卓.app.ActivityThread.performResumeActivity(ActivityThread.java:2120)
12-02 17:46:15.301: E/AndroidRuntime(333):  at 安卓.app.ActivityThread.handleResumeActivity(ActivityThread.java:2135)
12-02 17:46:15.301: E/AndroidRuntime(333):  at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1668)
12-02 17:46:15.301: E/AndroidRuntime(333):  at 安卓.app.ActivityThread.access$1500(ActivityThread.java:117)
12-02 17:46:15.301: E/AndroidRuntime(333):  at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-02 17:46:15.301: E/AndroidRuntime(333):  at 安卓.os.Handler.dispatchMessage(Handler.java:99)
12-02 17:46:15.301: E/AndroidRuntime(333):  at 安卓.os.Looper.loop(Looper.java:123)
12-02 17:46:15.301: E/AndroidRuntime(333):  at 安卓.app.ActivityThread.main(ActivityThread.java:3683)
12-02 17:46:15.301: E/AndroidRuntime(333):  at java.lang.reflect.Method.invokeNative(Native Method)
12-02 17:46:15.301: E/AndroidRuntime(333):  at java.lang.reflect.Method.invoke(Method.java:507)
12-02 17:46:15.301: E/AndroidRuntime(333):  at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-02 17:46:15.301: E/AndroidRuntime(333):  at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-02 17:46:15.301: E/AndroidRuntime(333):  at dalvik.system.NativeStart.main(Native Method)
12-02 17:46:15.301: E/AndroidRuntime(333): Caused by: java.lang.NullPointerException
12-02 17:46:15.301: E/AndroidRuntime(333):  at com.lead_management_project.Serach_Data.displayNewLeadData(Serach_Data.java:57)
12-02 17:46:15.301: E/AndroidRuntime(333):  at com.lead_management_project.Serach_Data.onResume(Serach_Data.java:50)
12-02 17:46:15.301: E/AndroidRuntime(333):  at 安卓.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150)
12-02 17:46:15.301: E/AndroidRuntime(333):  at 安卓.app.Activity.performResume(Activity.java:3832)
12-02 17:46:15.301: E/AndroidRuntime(333):  at 安卓.app.ActivityThread.performResumeActivity(ActivityThread.java:2110)
12-02 17:46:15.301: E/AndroidRuntime(333):  ... 12 more

共 (2) 个答案

  1. # 1 楼答案

    如果您的活动需要内存,Android可能会丢弃并重新创建它,并且您没有在活动的onCreate方法中设置dbHelper,因此当您尝试读取它时,它是null。事实上,我看不到你设置的任何地方。你是想用DataBase_Adapter来代替吗

  2. # 2 楼答案

    替换displayNewLeadData()如下所示

    public void displayNewLeadData() 
        {
            dbAdapter=new DataBase_Adapter(Serach_Data.this).open();
            dataBase = dbAdapter.getDatabaseInstance();
    
            Cursor mCursor = dataBase.rawQuery("SELECT * FROM " + DataBase_Adapter.TABLE_NEW_LEAD, null);
            mCursor.moveToFirst();
            arrayList_newLead_Id.clear();
            arrayList_newLead_Name.clear();
            arrayList_newLead_Email.clear();
            arrayList_newLead_Mobile.clear();
            arrayList_newLead_Products.clear();
            arrayList_newLead_Budget.clear();
            arrayList_newLead_Priority.clear();
            arrayList_newLead_Status.clear();
            arrayList_newLead_Notes.clear();
            arrayList_newLead_Reminder_Date.clear();
            arrayList_newLead_Reminder_Time.clear();
            arrayList_newLead_AddToContact.clear();
    
    
    
    
                do 
                {
                    arrayList_newLead_Id.add(mCursor.getString(mCursor.getColumnIndex(DataBase_Adapter.KEY_NEW_LEAD_ID)));
                    arrayList_newLead_Name.add(mCursor.getString(mCursor.getColumnIndex(DataBase_Adapter.KEY_NEW_LEAD_NAME)));
                    arrayList_newLead_Email.add(mCursor.getString(mCursor.getColumnIndex(DataBase_Adapter.KEY_NEW_LEAD_EMAIL)));
                    arrayList_newLead_Mobile.add(mCursor.getString(mCursor.getColumnIndex(DataBase_Adapter.KEY_NEW_LEAD_MOBILE)));
                    arrayList_newLead_Products.add(mCursor.getString(mCursor.getColumnIndex(DataBase_Adapter.KEY_NEW_LEAD_Product)));
                    arrayList_newLead_Budget.add(mCursor.getString(mCursor.getColumnIndex(DataBase_Adapter.KEY_NEW_LEAD_BUDGET)));
                    arrayList_newLead_Priority.add(mCursor.getString(mCursor.getColumnIndex(DataBase_Adapter.KEY_NEW_LEAD_PRIORITY)));
                    arrayList_newLead_Status.add(mCursor.getString(mCursor.getColumnIndex(DataBase_Adapter.KEY_NEW_LEAD_STATUS)));
                    arrayList_newLead_Notes.add(mCursor.getString(mCursor.getColumnIndex(DataBase_Adapter.KEY_NEW_LEAD_NOTES)));
                    arrayList_newLead_Reminder_Date.add(mCursor.getString(mCursor.getColumnIndex(DataBase_Adapter.KEY_NEW_LEAD_REMINDER_DATE)));
                    arrayList_newLead_Reminder_Time.add(mCursor.getString(mCursor.getColumnIndex(DataBase_Adapter.KEY_NEW_LEAD_REMINDER_TIME)));
                    arrayList_newLead_AddToContact.add(mCursor.getString(mCursor.getColumnIndex(DataBase_Adapter.KEY_NEW_LEAD_ADDtoCONTACTS)));
    
    
    
                } while (mCursor.moveToNext());
    
    
            new_Lead_List_Adapter = new New_Lead_List_Adapter(Serach_Data.this , arrayList_newLead_Id ,
                                                            arrayList_newLead_Name , arrayList_newLead_Email,
                                                            arrayList_newLead_Mobile , arrayList_newLead_Products,
                                                            arrayList_newLead_Budget , arrayList_newLead_Priority ,
                                                            arrayList_newLead_Status , arrayList_newLead_Notes ,
                                                            arrayList_newLead_Reminder_Date , arrayList_newLead_Reminder_Time ,
                                                            arrayList_newLead_AddToContact );
    
            newLeadDat_List.setAdapter(new_Lead_List_Adapter);
            new_Lead_List_Adapter.notifyDataSetChanged();
            mCursor.close();
            System.out.println("Data will Be Display.");
    
        }