2015-04-15 103 views
0

任何人都可以幫助我嗎?我對Android非常陌生,爲了解析我怎麼會出錯。我的logcat沒有顯示任何內容,所以我不確定問題出在哪裏。由於ParseQuery適配器問題導致應用程序崩潰

下面是崩潰應用程序的類的代碼。它應該顯示所有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> 
+0

請把你的logcat –

+0

編輯顯示logcat – Nic

回答

0
在logcat的

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

一個ListView添加到您的XML ID爲list

例如,在你的activity_upcoming_events.xml加:

<ListView 

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

謝謝。我只是試過,但同樣的事情發生(應用程序崩潰),並且logcat錯誤仍然說同樣的事情。我不知道這是爲什麼...... – Nic

+0

嘗試清理您的項目:從建設>清潔項目 –

+0

只是試圖'乾淨',如果它的緩存存在問題 – Nic

-1

使用

extends Activity

,而不是

extends ListActivity

ListActivity不需要setContentView(),它已經有一個ListView id爲android.R.id.list

+0

已添加了xml代碼。重新啓動沒有幫助:( – Nic

+0

不工作,因爲然後我在getListView()和setListAdapter方法得到一個錯誤 – Nic

+0

ListActivity包含一個listview實例,所以你可以調用getListView().setClickable()和setListAdapter(),但Activity你應該使用listview變量並且調用listview。setClickable()和listview.setAdapter()。 – nightwish

相關問題