设置ListView适配器时,Android应用程序会强行关闭

时间:2012-05-20 13:29:39

标签: java android

我正试图抓住Android开发的一席之地,但我遇到了麻烦。我有一个ListView,我不想用一些随机项填充它。但是当我尝试设置listview的适配器时,应用程序强制关闭。

main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

   <ListView
       android:id="@+id/accountList"
       android:layout_width="match_parent"
       android:layout_height="wrap_content" >

   </ListView>

</LinearLayout>

MainActivity.java:

package com.testapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

    static final String[] TEXTS = new String[] {"test1","test2"};

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ListView lv = (ListView) this.findViewById(R.id.accountList);
        lv.setAdapter(new ArrayAdapter<String>(this, R.id.accountList, TEXTS));            
    }
}

我做错了什么?

logcat的:

05-20 13:44:28.154: E/AndroidRuntime(585): FATAL EXCEPTION: main
05-20 13:44:28.154: E/AndroidRuntime(585): android.content.res.Resources$NotFoundException: Resource ID #0x7f060000 type #0x12 is not valid
05-20 13:44:28.154: E/AndroidRuntime(585):  at android.content.res.Resources.loadXmlResourceParser(Resources.java:2110)
05-20 13:44:28.154: E/AndroidRuntime(585):  at android.content.res.Resources.getLayout(Resources.java:857)
05-20 13:44:28.154: E/AndroidRuntime(585):  at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
05-20 13:44:28.154: E/AndroidRuntime(585):  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:371)
05-20 13:44:28.154: E/AndroidRuntime(585):  at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
05-20 13:44:28.154: E/AndroidRuntime(585):  at android.widget.AbsListView.obtainView(AbsListView.java:2033)
05-20 13:44:28.154: E/AndroidRuntime(585):  at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
05-20 13:44:28.154: E/AndroidRuntime(585):  at android.widget.ListView.onMeasure(ListView.java:1155)
05-20 13:44:28.154: E/AndroidRuntime(585):  at android.view.View.measure(View.java:12723)
05-20 13:44:28.154: E/AndroidRuntime(585):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
05-20 13:44:28.154: E/AndroidRuntime(585):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
05-20 13:44:28.154: E/AndroidRuntime(585):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
05-20 13:44:28.154: E/AndroidRuntime(585):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
05-20 13:44:28.154: E/AndroidRuntime(585):  at android.view.View.measure(View.java:12723)
05-20 13:44:28.154: E/AndroidRuntime(585):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
05-20 13:44:28.154: E/AndroidRuntime(585):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
05-20 13:44:28.154: E/AndroidRuntime(585):  at android.view.View.measure(View.java:12723)
05-20 13:44:28.154: E/AndroidRuntime(585):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
05-20 13:44:28.154: E/AndroidRuntime(585):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
05-20 13:44:28.154: E/AndroidRuntime(585):  at android.view.View.measure(View.java:12723)
05-20 13:44:28.154: E/AndroidRuntime(585):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
05-20 13:44:28.154: E/AndroidRuntime(585):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
05-20 13:44:28.154: E/AndroidRuntime(585):  at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092)
05-20 13:44:28.154: E/AndroidRuntime(585):  at android.view.View.measure(View.java:12723)
05-20 13:44:28.154: E/AndroidRuntime(585):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
05-20 13:44:28.154: E/AndroidRuntime(585):  at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
05-20 13:44:28.154: E/AndroidRuntime(585):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-20 13:44:28.154: E/AndroidRuntime(585):  at android.os.Looper.loop(Looper.java:137)
05-20 13:44:28.154: E/AndroidRuntime(585):  at android.app.ActivityThread.main(ActivityThread.java:4424)
05-20 13:44:28.154: E/AndroidRuntime(585):  at java.lang.reflect.Method.invokeNative(Native Method)
05-20 13:44:28.154: E/AndroidRuntime(585):  at java.lang.reflect.Method.invoke(Method.java:511)
05-20 13:44:28.154: E/AndroidRuntime(585):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-20 13:44:28.154: E/AndroidRuntime(585):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-20 13:44:28.154: E/AndroidRuntime(585):  at dalvik.system.NativeStart.main(Native Method)

4 个答案:

答案 0 :(得分:1)

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       setContentView(R.layout.main);

        ListView lv = (ListView)findViewById(R.id.accountList);
        lv.setAdapter(new ArrayAdapter<String>(this, R.id.accountList, TEXTS));


    }

答案 1 :(得分:1)

错误在线

lv.setAdapter(new ArrayAdapter<String>(this, R.id.accountList, TEXTS));   

第二个参数需要一个只有TextView的布局,如下所示:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

假设您已将其保存在布局文件夹中的文件list_row.xml中。

然后你做:

lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_row, TEXTS));  

答案 2 :(得分:0)

尝试将xml设置为:

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

另外,请粘贴堆栈跟踪。

答案 3 :(得分:0)

它正在抛出Resources$NotFoundException,这意味着R.id.accountList可能出现了问题。它找不到它。我不确定,但我认为适配器应该指向R.layout.main,因为那是id所在的位置。

lv.setAdapter(new ArrayAdapter<String>(this, R.layout.main, TEXTS));