简体   繁体   English

运行时异常:无法启动活动android listview

[英]Runtime Exception: Unable to start activity android listview

I am new to android. 我是Android新手。 I'm building an Events app and every time I press upcoming events button, app crashes and gives "Unfortunately **** has stopped" message. 我正在构建一个“事件”应用程序,每当我按下即将发生的事件按钮时,该应用程序就会崩溃并显示“不幸的是****已停止”消息。

logcat says it's Runtime Exception. logcat说这是运行时异常。

Please help me.Thanks in advance.. 请帮助我。谢谢。

This is my activity code; 这是我的活动代码;

package com.example.mrspegasus.eventsapp;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import java.util.List;

public class EventsUpComing extends ListActivity
{
    protected List<ParseObject> mEvents;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_events_up_coming);

        ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("EVENTS");
        query.findInBackground(new FindCallback<ParseObject>() 
        {
            @Override
            public void done(List<ParseObject> event, ParseException e)
            {
                if(e == null)
                {
                    mEvents = event;
                    EventAdapter adapter = new EventAdapter(getListView().getContext(),mEvents);
                    setListAdapter(adapter);
                }
            }
        });
}
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_events_up_coming, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

This is the layout; 这是布局;

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mrspegasus.eventsapp.EventsUpComing"
android:background="@drawable/img4">

<Button
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:text="Choose A Date"
    android:id="@+id/btdate"
    android:layout_below="@+id/Titlebar"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="30dp"
    android:textStyle="bold" />

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/listViewUpcomingEvents"
    android:layout_below="@+id/btdate"
    android:layout_alignLeft="@+id/Logo"
    android:layout_alignStart="@+id/Logo"
    android:layout_marginTop="52dp" />

in logcat it says; 在logcat中说:

09-19 12:26:43.641  20313-20313/com.example.mrspegasus.eventsapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.mrspegasus.eventsapp, PID: 20313
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mrspegasus.eventsapp/com.example.mrspegasus.eventsapp.EventsUpComing}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
        at android.app.

you need to change your listview id as like this 您需要像这样更改您的listview ID

<ListView
    android:id="@android:id/list"    
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:layout_below="@+id/btdate"
    android:layout_alignLeft="@+id/Logo"
    android:layout_alignStart="@+id/Logo"
    android:layout_marginTop="52dp" />

You have extended your class with ListActivity so this ListActivity's prerequsite is you must have one listview whose id must be @android:id/list. 您已经使用ListActivity扩展了类,因此该ListActivity的先决条件是您必须具有一个ID必须为@android:id / list的listview。 Your listview in xml contains id @+id/listViewUpcomingEvents. 您在xml中的listview包含id @ + id / listViewUpcomingEvents。 Rename your listView's id to @android:id/list. 将您的listView的ID重命名为@android:id / list。 At the end you xml's ListView should looks like this. 最后,您的xml的ListView应该看起来像这样。

<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@android:id/list"
android:layout_below="@+id/btdate"
android:layout_alignLeft="@+id/Logo"
android:layout_alignStart="@+id/Logo"
android:layout_marginTop="52dp" />

If you want some guidence on that you can refer to this or this . 如果您需要一些指导,可以参考thisthis If this helps you please accept it as answer. 如果这对您有帮助,请接受它作为答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM