0

我试图在数组列表中显示我的 sqlite 数据库的数据,但代码有问题,我找不到它。这是我的应用程序的代码:

public class Event extends Fragment implements OnClickListener {
Intent intent;
TextView eventId;
EventController controller = new EventController(getActivity());
Button show;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.event, container, false);

    ArrayList<HashMap<String, String>> eventList = controller
            .getAllEvents();
    if (eventList.size() != 0) {
        ListView lv = (ListView) rootView.findViewById(R.id.list);
        lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                eventId = (TextView) view.findViewById(R.id.eventId);
                String valEventId = eventId.getText().toString();
                Intent objIndent = new Intent(getActivity(),
                        EditEvent.class);
                objIndent.putExtra("eventId", valEventId);
                startActivity(objIndent);
            }
        });
        ListAdapter adapter = new SimpleAdapter(getActivity(), eventList,
                R.layout.view_event_entry, new String[] { "eventId",
                        "eventName" }, new int[] { R.id.eventId,
                        R.id.eventName });
        lv.setAdapter(adapter);
    }
    show = (Button)rootView.findViewById(R.id.button1);
    show.setOnClickListener(this);

    return rootView;
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent objIntent = new Intent(getActivity(), AddEvent.class);
    startActivity(objIntent);
}

而 LogCat ,也许问题是空指针异常:

10-27 03:37:20.418: E/AndroidRuntime(29577): FATAL EXCEPTION: main
10-27 03:37:20.418: E/AndroidRuntime(29577): java.lang.NullPointerException
10-27 03:37:20.418: E/AndroidRuntime(29577):    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
10-27 03:37:20.418: E/AndroidRuntime(29577):    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
10-27 03:37:20.418: E/AndroidRuntime(29577):    at com.example.hapshare.EventController.getAllEvents(EventController.java:70)
10-27 03:37:20.418: E/AndroidRuntime(29577):    at com.example.hapshare.Event.onCreateView(Event.java:41)
10-27 03:37:20.418: E/AndroidRuntime(29577):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
10-27 03:37:20.418: E/AndroidRuntime(29577):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
10-27 03:37:20.418: E/AndroidRuntime(29577):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
10-27 03:37:20.418: E/AndroidRuntime(29577):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
10-27 03:37:20.418: E/AndroidRuntime(29577):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
10-27 03:37:20.418: E/AndroidRuntime(29577):    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
10-27 03:37:20.418: E/AndroidRuntime(29577):    at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)

这是我的数据库 EventController 的代码:

    public class EventController extends SQLiteOpenHelper {
private static final String LOGCAT = null;

public EventController(Context applicationcontext) {
    super(applicationcontext, "androidsqlite.db", null, 1);
    Log.d(LOGCAT,"Created");
}

@Override
//create table for the database
public void onCreate(SQLiteDatabase database) {
    String query;
    query = "CREATE TABLE events ( eventId INTEGER PRIMARY KEY, eventName TEXT)";
    database.execSQL(query);    
    Log.d(LOGCAT,"events Created");
}
@Override
//drop the database and reset if required
public void onUpgrade(SQLiteDatabase database, int version_old, int current_version) {
    String query;
    query = "DROP TABLE IF EXISTS events";
    database.execSQL(query);
    onCreate(database);
}

public void insertEvent(HashMap<String, String> queryValues) {
    SQLiteDatabase database = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("eventName", queryValues.get("eventName"));
    database.insert("events", null, values);
    database.close();
}

public int updateEvent(HashMap<String, String> queryValues) {
    SQLiteDatabase database = this.getWritableDatabase();    
    ContentValues values = new ContentValues();
    values.put("eventName", queryValues.get("eventName"));
    return database.update("events", values, "eventId" + " = ?", new String[] { queryValues.get("eventId") });
    //String updateQuery = "Update  words set txtWord='"+word+"' where txtWord='"+ oldWord +"'";
    //Log.d(LOGCAT,updateQuery);
    //database.rawQuery(updateQuery, null);
    //return database.update("words", values, "txtWord  = ?", new String[] { word });
}

public void deleteEvent(String id) {
    Log.d(LOGCAT,"delete");
    SQLiteDatabase database = this.getWritableDatabase();    
    String deleteQuery = "DELETE FROM  events where eventId='"+ id +"'";
    Log.d("query",deleteQuery);     
    database.execSQL(deleteQuery);
}

public ArrayList<HashMap<String, String>> getAllEvents() {
    ArrayList<HashMap<String, String>> wordList;
    wordList = new ArrayList<HashMap<String, String>>();
    String selectQuery = "SELECT  * FROM events";
    SQLiteDatabase database = this.getWritableDatabase();
    Cursor cursor = database.rawQuery(selectQuery, null);
    if (cursor.moveToFirst()) {
        do {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("eventId", cursor.getString(0));
            map.put("eventName", cursor.getString(1));
            wordList.add(map);
        } while (cursor.moveToNext());
    }

    // return contact list
    return wordList;
}

public HashMap<String, String> getEventInfo(String id) {
    HashMap<String, String> wordList = new HashMap<String, String>();
    SQLiteDatabase database = this.getReadableDatabase();
    String selectQuery = "SELECT * FROM events where eventId='"+id+"'";
    Cursor cursor = database.rawQuery(selectQuery, null);
    if (cursor.moveToFirst()) {
        do {
                //HashMap<String, String> map = new HashMap<String, String>();
            wordList.put("eventName", cursor.getString(1));
               //wordList.add(map);
        } while (cursor.moveToNext());
    }                   
return wordList;
}   

有没有人知道我做错了什么?谢谢

4

1 回答 1

0

使用完数据库和在数据库上工作的游标后,您需要关闭它们。

cursor.close();最后添加getAllEvents()方法。这似乎导致了这个问题。

同样,您应该在所有其他地方做同样的事情。看看这个教程,看看游标是如何在不同的查询方法结束时关闭的。

于 2013-10-28T07:29:51.937 回答