0

我正在尝试掌握 android 开发的窍门,但我遇到了障碍。我有一个 ListView,我不想用一些随机项目填充它。但是当我尝试设置列表视图的适配器时,应用程序强制关闭。

主.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));            
    }
}

我究竟做错了什么?

日志猫:

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

4 回答 4

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));


    }
于 2012-05-20T13:34:23.127 回答
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));  
于 2012-05-20T14:33:47.097 回答
0

尝试将xml设置为:

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

另外,请粘贴您的堆栈跟踪。

于 2012-05-20T13:40:32.873 回答
0

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

lv.setAdapter(new ArrayAdapter<String>(this, R.layout.main, TEXTS)); 
于 2012-05-20T14:05:53.123 回答