这是我的代码有问题。我想在输入一个事件后在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):at com.example.hapshare.EventController.getAllEvents(EventController.java:70) 10-26 08:02:29.141:E / AndroidRuntime(22646):at 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):at 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):at 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):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1083) 10-26 08:02:29.141:E / AndroidRuntime(22646):at 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)
答案 0 :(得分:3)
有些事情会在代码中引发危险信号。这引出了我的第一个问题:你使用的是eclipse还是intellij?因为看起来它甚至不会编译给我。也许你在这里编辑了这个,但现在帮助你实现它。甚至不使用onActivityCreated
初始化片段中的ListView
(安德鲁也正确地提出了你甚至没有正确使用它的观点,这就是为什么我甚至惊讶它甚至编译如果它是)。你应该在onCreateView
中这样做。现在我将发布我编辑过的代码。我没有测试它,所以它可以作为一个更好的指南,它应该是什么样的(并希望它编译)。
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;
}
...
答案 1 :(得分:2)
这有一些问题。首先,您错误地覆盖了onActivityCreated()
。而不是你拥有的,它应该有以下标题:
public void onActivityCreated(Bundle savedInstanceStates)
因为您使用了错误的标题,所以永远不会调用它。
其次,在rootView
方法中使用onActivityCreated()
可能会出现一些错误,因为它未在该方法中声明。请考虑调用getView()
,因为它会为您返回rootView。