这是我的代码有问题。我正在尝试ListView
在输入事件后更新我的 sqlite 数据库数据,但ListView
没有显示,而且我不擅长使用片段,谁能指导我在这里做错了什么?对不起,我是 android 应用程序开发的新手。谢谢你。
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.event, container, false);
show = (Button)rootView.findViewById(R.id.button1);
show.setOnClickListener(this);
return rootView;
}
public void onActivityCreated(int requestCode, int resultCode, Intent data) {
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);
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent objIntent = new Intent(getActivity(), AddEvent.class);
startActivity(objIntent);
}
这是我的xml代码:
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/addevents"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
<RelativeLayout
android:id="@id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="@+id/relativeLayout1"
android:orientation="vertical"
android:layout_marginTop="40dp">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true">
</ListView>
</RelativeLayout>
</RelativeLayout>
这是设置我的数据库的代码。我想知道这是否是问题发生的地方。你们可以在这里查看。
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;
}
这是我得到的错误:
10-26 08:02:29.141:E/AndroidRuntime(22646):致命异常:主要 10-26 08:02:29.141:E/AndroidRuntime(22646):java.lang.NullPointerException 10-26 08:02:29.141: E/AndroidRuntime(22646): 在 android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224) 10-26 08:02:29.141: E/AndroidRuntime(22646): 在 android.database.sqlite.SQLiteOpenHelper.getWritableDatabase (SQLiteOpenHelper.java:164) 10-26 08:02:29.141: E/AndroidRuntime(22646): 在 com.example.hapshare.EventController.getAllEvents(EventController.java:70) 10-26 08:02:29.141: E /AndroidRuntime(22646): 在 com.example.hapshare.Event.onCreateView(Event.java:42) 10-26 08:02:29.141: E/AndroidRuntime(22646): 在 android.support.v4.app.Fragment。 performCreateView(Fragment.java:1478) 10-26 08:02:29.141: E/AndroidRuntime(22646): 在 android.support.v4.app。FragmentManagerImpl.moveToState(FragmentManager.java:927) 10-26 08:02:29.141: E/AndroidRuntime(22646): 在 android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104) 10-26 08: 02:29.141: E/AndroidRuntime(22646): 在 android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) 10-26 08:02:29.141: E/AndroidRuntime(22646): 在 android.support .v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460) 10-26 08:02:29.141: E/AndroidRuntime(22646): 在 android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472) 10-26 08:02:29.141: E/AndroidRuntime(22646): 在 android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141) 10-26 08:02:29.141: E/AndroidRuntime(22646) : 在 android.support.v4.view.ViewPager.populate(ViewPager.java:1068) 10-26 08:02:29.141: E/AndroidRuntime(22646): 在 android.support.v4.view.ViewPager.populate(ViewPager.java:914) 10-26 08:02:29.141: E/AndroidRuntime( 22646): 在 android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436) 10-26 08:02:29.141: E/AndroidRuntime(22646): 在 android.view.View.measure(View.java :15696) 10-26 08:02:29.141: E/AndroidRuntime(22646): 在 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4851) 10-26 08:02:29.141: E/AndroidRuntime(22646):在 android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 10-26 08:02:29.141: E/AndroidRuntime(22646): 在 android.view.View.measure(View.java:15696) 10-26 08 :02:29.141: E/AndroidRuntime(22646): 在 android.widget.LinearLayout.measureVertical(LinearLayout.java:847) 10-26 08:02:29.141: E/AndroidRuntime(22646): 在 android.widget。LinearLayout.onMeasure(LinearLayout.java:588) 10-26 08:02:29.141: E/AndroidRuntime(22646): 在 android.view.View.measure(View.java:15696) 10-26 08:02:29.141: E/AndroidRuntime(22646): 在 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4851) 10-26 08:02:29.141: E/AndroidRuntime(22646): 在 android.widget.FrameLayout.onMeasure(FrameLayout.java :310) 10-26 08:02:29.141: E/AndroidRuntime(22646): 在 com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2261) 10-26 08:02:29.141 : E/AndroidRuntime(22646): 在 android.view.View.measure(View.java:15696) 10-26 08:02:29.141: E/AndroidRuntime(22646): 在 android.view.ViewRootImpl.performMeasure(ViewRootImpl. java:2221) 10-26 08:02:29.141: E/AndroidRuntime(22646): 在 android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1287) 10-26 08:02:29.141: E/AndroidRuntime(22646): 在 android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1524) 10-26 08:02:29.141: E/AndroidRuntime(22646): 在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1187) 10-26 08:02:29.141: E/AndroidRuntime(22646): 在 android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4855) 10-26 08:02:29.141: E/AndroidRuntime(22646): 在 android.view.Choreographer$CallbackRecord.run(Choreographer.java:766) 10-26 08:02:29.141: E/AndroidRuntime(22646): 在 android.view .Choreographer.doCallbacks(Choreographer.java:575) 10-26 08:02:29.141: E/AndroidRuntime(22646): 在 android.view.Choreographer.doFrame(Choreographer.java:542) 10-26 08:02:29.141 : E/AndroidRuntime(22646): 在 android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:751) 10-26 08:02:29.141: E/AndroidRuntime(22646): 在 android.os.Handler.handleCallback(Handler.java:725) 10-26 08:02:29.141: E/AndroidRuntime(22646): 在android.os.Handler.dispatchMessage(Handler.java:92) 10-26 08:02:29.141: E/AndroidRuntime(22646): 在 android.os.Looper.loop(Looper.java:158) 10-26 08: 02:29.141: E/AndroidRuntime(22646): 在 android.app.ActivityThread.main(ActivityThread.java:5777) 10-26 08:02:29.141: E/AndroidRuntime(22646): 在 java.lang.reflect.Method .invokeNative(Native Method) 10-26 08:02:29.141: E/AndroidRuntime(22646): at java.lang.reflect.Method.invoke(Method.java:511) 10-26 08:02:29.141: E/ AndroidRuntime(22646): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083) 10-26 08:02:29.141: E/AndroidRuntime(22646): 在 com.android.internal.os .ZygoteInit。main(ZygoteInit.java:850) 10-26 08:02:29.141: E/AndroidRuntime(22646): at dalvik.system.NativeStart.main(Native Method)