试图显示Listview

时间:2017-09-23 12:58:39

标签: android android-linearlayout

我是Android新手,我试图显示ListView,但每次启动我的应用程序时它都会停止。我已经试图解决我的问题几个小时,但我没有发现什么是错的。提前感谢任何能够解释我的问题的人。这是我的main_activity类:

public TextView textView;
public int count = 0;
public ListView listView;
public Context context;
public List<HashMap<String,String>> list = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    context = getApplicationContext();

    textView = (TextView)findViewById(R.id.text);
    listView = (ListView)findViewById(R.id.listView);

    Button button = (Button)findViewById(R.id.button);

    button.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            count++;
            addLine(count);
            textView.setText(String.valueOf(count));
        }
    });
}

void addLine(int counter){
    if(counter>0){
    HashMap<String,String> hashMap = new HashMap<String,String>();
    for(int i=0;i < counter;i++){
        hashMap.clear();
        hashMap.put(String.valueOf(i),"test" + String.valueOf(i));
        list.add(hashMap);
    }
    String[] from = {"first","second"};
    int[] to = {android.R.layout.simple_list_item_1, android.R.layout.simple_list_item_2 };
    SimpleAdapter simpleAdapter = new SimpleAdapter(context,list,R.id.listView, from, to );

    listView.setAdapter(simpleAdapter);
    }
}

Activity_main.xml和我的logcat:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/myLayout"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="8dp">

    <Button android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="10"
        android:text="@string/buttonName" />

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="20"
        android:text="test"/>


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="70"
        android:padding="8dp" >

        <ListView
            android:id="@+id/listView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="10" />

    </LinearLayout>


</LinearLayout>


09-23 11:01:36.418 13592-13592/com.example.cem.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
                                                                               Process: com.example.cem.myapplication, PID: 13592
                                                                               android.content.res.Resources$NotFoundException: Resource ID #0x7f0b0061 type #0x12 is not valid
                                                                                   at android.content.res.Resources.loadXmlResourceParser(Resources.java:2138)
                                                                                   at android.content.res.Resources.getLayout(Resources.java:1142)
                                                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:421)
                                                                                   at android.widget.SimpleAdapter.createViewFromResource(SimpleAdapter.java:128)
                                                                                   at android.widget.SimpleAdapter.getView(SimpleAdapter.java:121)
                                                                                   at android.widget.AbsListView.obtainView(AbsListView.java:2372)
                                                                                   at android.widget.ListView.measureHeightOfChildren(ListView.java:1408)
                                                                                   at android.widget.ListView.onMeasure(ListView.java:1315)
                                                                                   at android.view.View.measure(View.java:21998)
                                                                                   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
                                                                                   at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
                                                                                   at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
                                                                                   at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
                                                                                   at android.view.View.measure(View.java:21998)
                                                                                   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
                                                                                   at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
                                                                                   at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
                                                                                   at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
                                                                                   at android.view.View.measure(View.java:21998)
                                                                                   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
                                                                                   at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                                   at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
                                                                                   at android.view.View.measure(View.java:21998)
                                                                                   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
                                                                                   at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:393)
                                                                                   at android.view.View.measure(View.java:21998)
                                                                                   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
                                                                                   at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                                   at android.view.View.measure(View.java:21998)
                                                                                   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
                                                                                   at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
                                                                                   at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
                                                                                   at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
                                                                                   at android.view.View.measure(View.java:21998)
                                                                                   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
                                                                                   at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                                   at com.android.internal.policy.DecorView.onMeasure(DecorView.java:721)
                                                                                   at android.view.View.measure(View.java:21998)
                                                                                   at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2410)
                                                                                   at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1498)
                                                                                   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1751)
                                                                                   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1386)
                                                                                   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6733)
                                                                                   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
                                                                                   at android.view.Choreographer.doCallbacks(Choreographer.java:723)
                                                                                   at android.view.Choreographer.doFrame(Choreographer.java:658)
                                                                                   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
                                                                                   at android.os.Handler.handleCallback(Handler.java:789)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:98)
                                                                                   at android.os.Looper.loop(Looper.java:164)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                                   at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

1 个答案:

答案 0 :(得分:1)

您的to数组必须包含要在项目视图布局中绑定的视图ID(如果您使用Android资源中的简单项目布局,通常它们为android.R.id.text1android.R.id.text2),而不是一些其他布局ID。