由于ParseQuery适配器的问题,应用程序崩溃

时间:2015-04-15 13:00:27

标签: android parse-platform

任何人都可以帮助我吗?我对Android和Parse都很陌生,所以无法理解我的错误。我的logcat没有显示任何内容,所以我不确定问题出在哪里。

以下是崩溃应用程序的类的代码。它应显示所有UpcomingEvents Parse Objects的列表。

public class UpcomingEvent  extends ListActivity {

private ParseQueryAdapter<UpcomingEvents> allEventsAdapter;
private ListView listView;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getListView().setClickable(false);
    setContentView(R.layout.activity_upcoming_events);

    ParseQueryAdapter.QueryFactory<UpcomingEvents> allEvents =
            new ParseQueryAdapter.QueryFactory<UpcomingEvents>() {
                public ParseQuery<UpcomingEvents> create() {
                    ParseQuery<UpcomingEvents> query = UpcomingEvents.getQuery();
                    query.orderByDescending("createdAt");
                    return query;
                }
            };
    // Set up the query adapter
    allEventsAdapter = new ParseQueryAdapter<UpcomingEvents>(this, allEvents) {
        @Override
        public View getItemView(UpcomingEvents upcomingEvents, View view, ViewGroup parent) {
            if (view == null) {
                view = View.inflate(getContext(), R.layout.item_view_events, null);
            }
            TextView nameTV = (TextView) view.findViewById(R.id.item_nameTxt);
            nameTV.setText(upcomingEvents.getName());
            TextView typeTV = (TextView) view
                    .findViewById(R.id.item_typeTxt);
            typeTV.setText(upcomingEvents.getType());
            TextView dateTV = (TextView) view.findViewById(R.id.item_dateTxt);
            dateTV.setText(upcomingEvents.getDate());
            TextView venueTV = (TextView) view.findViewById(R.id.item_venueTxt);
            venueTV.setText(upcomingEvents.getVenue());
            TextView creatorTV = (TextView) view.findViewById(R.id.item_creatorTxt);
            //creatorTV.setText((CharSequence) upcomingEvents.getUser());
            creatorTV.setText(upcomingEvents.getUser().getUsername());
            return view;
        }
    };

    allEventsAdapter.setAutoload(false);

    allEventsAdapter.setPaginationEnabled(false);

    // Attach the query adapter to the view
    ListView eventsListView = (ListView) findViewById(R.id.EventListView);
    eventsListView.setAdapter(allEventsAdapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_event_list, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

        case R.id.action_refresh: {
            updateEventList();
            break;
        }

        case R.id.action_new: {
            newEvent();
            break;
        }
    }
    return super.onOptionsItemSelected(item);
}

private void updateEventList() {
    allEventsAdapter.loadObjects();
    setListAdapter(allEventsAdapter);
}

private void newEvent() {
    Intent i = new Intent(this, AddEvent.class);
    startActivityForResult(i, 0);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK) {
        // If a new post has been added, update
        // the list of posts
        updateEventList();
    }
}
}

Logcat输出:

04-15 09:20:18.653    4309-4309/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: ie.wit.nicola.pasedb, PID: 4309
java.lang.RuntimeException: Unable to start activity ComponentInfo{ie.wit.nicola.pasedb/ie.wit.nicola.pasedb.UpcomingEvent}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
        at android.app.ListActivity.onContentChanged(ListActivity.java:243)
        at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:293)
        at android.app.Activity.setContentView(Activity.java:1929)
        at ie.wit.nicola.pasedb.UpcomingEvent.onCreate(UpcomingEvent.java:31)
        at android.app.Activity.performCreate(Activity.java:5231)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)

activity_upcoming_events.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:background="@drawable/backrepeat">

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="@dimen/inner_margin"
    android:paddingRight="@dimen/inner_margin"
    android:paddingTop="@dimen/inner_margin"
    android:paddingBottom="@dimen/inner_margin"
    android:background="#fff6e5">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Upcoming Events"
        android:textAppearance="?android:textAppearanceLarge"
        android:id="@+id/UpcomingEventtextView"/>


    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/list"
        android:layout_below="@+id/UpcomingEventtextView"/>


</RelativeLayout>
</LinearLayout>

2 个答案:

答案 0 :(得分:0)

logcat中的

Your content must have a ListView whose id attribute is 'android.R.id.list'

将Listview添加到ID为list

的XML中

例如,在activity_upcoming_events.xml添加:

<ListView

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@android:id/list"
/>

答案 1 :(得分:-1)

使用

  

扩展活动

而不是

  

扩展ListActivity

ListActivity不需要setContentView(),它已经有一个listView,其id为android.R.id.list