我的活动出错,致命异常:主要

时间:2016-06-26 09:33:26

标签: java android android-studio

尝试在Android Studio中运行我的应用时出现问题。我得到了

FATAL EXCEPTION: main

更确切地说,似乎存在一个问题:

dancam.com.chords.ChordsListActivity.onCreate(ChordsListActivity.java:13)

是:

setContentView(R.layout.activity_chords_list);

这里是ChordsListActivity类:

import android.app.ListActivity;
import android.os.Bundle;

public class ChordsListActivity extends ListActivity {

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

    String[] accordi = new String[] {"Do maggiore", "Re maggiore", "Si maggiore"};
    String[] note = new String[] {"note: Do, Mi, Sol", "note: La, Re, Fa#", "note: Fa#, Si, Re#"};
    int[] immagini = new int[] {R.drawable.do_maggiore, R.drawable.re_maggiore, R.drawable.si_maggiore};

    CustomListAdapter adapter = new CustomListAdapter(getApplicationContext(), accordi, note, immagini);
    setListAdapter(adapter);
    }
}

我有点能够通过扩展Activity而不是ListActivity来解决它,但后来我无法使用setListAdapter()方法。

如果有人可以帮我修复代码,我真的很感激!

如果您需要我发布自定义适配器或任何其他文件,请告诉我。

编辑添加了整个堆栈跟踪

FATAL EXCEPTION: main
Process: dancam.com.chords, PID: 3080
java.lang.RuntimeException: Unable to start activity ComponentInfo{dancam.com.chords/dancam.com.chords.ChordsListActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
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.PhoneWindow.setContentView(PhoneWindow.java:398)
at android.app.Activity.setContentView(Activity.java:2166)
at dancam.com.chords.ChordsListActivity.onCreate(ChordsListActivity.java:13)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

2 个答案:

答案 0 :(得分:0)

从ListActivity扩展时,它已经有了ListView,但是Activity没有。您必须创建列表inbActivity的布局 - 更好地使用RecyclerView而不是List视图。在您的活动中创建RecyclerView参考,在onCreate中初始化它。为活动中的数据创建适配器(onCreate方法)。 Finnaly,将适配器设置为listview引用。要修改列表中的数据,请在适配器中设置新数据并在其上调用.notifyDatasetChange()。请注意,对于RecyclerView,您还需要LayoutManager(参见文档)

答案 1 :(得分:0)

这是ListView的问题。 转到R.layout.activity_chords_list并添加<ListView>并设置ID android:id="@android:id/list"

与Exception一样:&#39;您的内容必须有一个ListView,其id属性为&#39; android.R.id.list&#39;

如果其余代码没问题,它应该有效。