2

在 Android 中,我试图制作简单的列表,但是每次我运行它时我的简单应用程序都会崩溃并出现相同的错误。这是我的主文件代码

package com.example.test;

import java.util.ArrayList;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.app.Activity;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView lv=(ListView)findViewById(R.id.listView1);

        ArrayList<String> country=new ArrayList<String>();
        country.add("aaaaa");
        country.add("aaaaa");
        country.add("aaaaa");

        ArrayAdapter<String> adapter=new ArrayAdapter<String>(getApplicationContext(),R.id.listView1,country);
        lv.setAdapter(adapter);

    }



}

我的xml文件就像

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="28dp"
        android:layout_marginTop="47dp" >
    </ListView>

</RelativeLayout>

现在我在 log cat 中得到的错误是。

07-15 18:09:22.235: E/AndroidRuntime(1202): FATAL EXCEPTION: main
07-15 18:09:22.235: E/AndroidRuntime(1202): android.content.res.Resources$NotFoundException: Resource ID #0x7f080000 type #0x12 is not valid
07-15 18:09:22.235: E/AndroidRuntime(1202):     at android.content.res.Resources.loadXmlResourceParser(Resources.java:2144)
07-15 18:09:22.235: E/AndroidRuntime(1202):     at android.content.res.Resources.getLayout(Resources.java:853)
07-15 18:09:22.235: E/AndroidRuntime(1202):     at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
07-15 18:09:22.235: E/AndroidRuntime(1202):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:371)
07-15 18:09:22.235: E/AndroidRuntime(1202):     at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
07-15 18:09:22.235: E/AndroidRuntime(1202):     at android.widget.AbsListView.obtainView(AbsListView.java:2159)
07-15 18:09:22.235: E/AndroidRuntime(1202):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
07-15 18:09:22.235: E/AndroidRuntime(1202):     at android.widget.ListView.onMeasure(ListView.java:1158)
07-15 18:09:22.235: E/AndroidRuntime(1202):     at android.view.View.measure(View.java:15518)
07-15 18:09:22.235: E/AndroidRuntime(1202):     at android.widget.RelativeLayout.measureChild(RelativeLayout.java:666)
07-15 18:09:22.235: E/AndroidRuntime(1202):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
07-15 18:09:22.235: E/AndroidRuntime(1202):     at android.view.View.measure(View.java:15518)
07-15 18:09:22.235: E/AndroidRuntime(1202):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
07-15 18:09:22.235: E/AndroidRuntime(1202):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-15 18:09:22.235: E/AndroidRuntime(1202):     at android.view.View.measure(View.java:15518)
07-15 18:09:22.235: E/AndroidRuntime(1202):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
07-15 18:09:22.235: E/AndroidRuntime(1202):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
07-15 18:09:22.235: E/AndroidRuntime(1202):     at android.view.View.measure(View.java:15518)
07-15 18:09:22.235: E/AndroidRuntime(1202):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
07-15 18:09:22.235: E/AndroidRuntime(1202):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-15 18:09:22.235: E/AndroidRuntime(1202):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
07-15 18:09:22.235: E/AndroidRuntime(1202):     at android.view.View.measure(View.java:15518)
07-15 18:09:22.235: E/AndroidRuntime(1202):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
07-15 18:09:22.235: E/AndroidRuntime(1202):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
07-15 18:09:22.235: E/AndroidRuntime(1202):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
07-15 18:09:22.235: E/AndroidRuntime(1202):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
07-15 18:09:22.235: E/AndroidRuntime(1202):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
07-15 18:09:22.235: E/AndroidRuntime(1202):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
07-15 18:09:22.235: E/AndroidRuntime(1202):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
07-15 18:09:22.235: E/AndroidRuntime(1202):     at android.view.Choreographer.doFrame(Choreographer.java:532)
07-15 18:09:22.235: E/AndroidRuntime(1202):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
07-15 18:09:22.235: E/AndroidRuntime(1202):     at android.os.Handler.handleCallback(Handler.java:725)
07-15 18:09:22.235: E/AndroidRuntime(1202):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-15 18:09:22.235: E/AndroidRuntime(1202):     at android.os.Looper.loop(Looper.java:137)
07-15 18:09:22.235: E/AndroidRuntime(1202):     at android.app.ActivityThread.main(ActivityThread.java:5041)
07-15 18:09:22.235: E/AndroidRuntime(1202):     at java.lang.reflect.Method.invokeNative(Native Method)
07-15 18:09:22.235: E/AndroidRuntime(1202):     at java.lang.reflect.Method.invoke(Method.java:511)
07-15 18:09:22.235: E/AndroidRuntime(1202):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-15 18:09:22.235: E/AndroidRuntime(1202):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-15 18:09:22.235: E/AndroidRuntime(1202):     at dalvik.system.NativeStart.main(Native Method)

我读了很多关于这个资源未找到错误的内容,并且做了所有的把戏,但没有奏效。我删除了 R 文件,清理了项目,创建了新项目但失败了。

4

2 回答 2

5

内部阵列适配器更改 R.id.listView1 android.R.layout.simple_list_item_1

ArrayAdapter<String> adapter=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,country);
于 2013-07-15T18:25:17.533 回答
0

将 ArrayAdapter 更改为:

ArrayAdapter<String> adapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,country);
lv.setAdapter(adapter);
于 2013-07-15T18:29:06.320 回答