findViewByID方法不接受listview

时间:2018-02-20 17:33:47

标签: java android listview

我曾尝试编写一个简短的Android应用程序,当我在我的Activity(扩展ListActivity)中实现ListView时,我目前失败了。我发现的所有解决方案都不起作用。

findViewByID 方法不接受 R.id.listview 作为参数,错误消息为:

  

“错误:找不到符号变量listview”

然而,这是每个教程和示例执行此简单步骤的方式。我已经以我能想象的各种方式更改了xml文件中的ID,现在将其恢复为“android:id =”@ android:id / list“”

  

现在这里是活动:

package com.example.roman.romansrecipes;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends ListActivity implements 
AdapterView.OnItemClickListener {

ListView l;
String[] recipes ={"R1", "R2", "R2"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    l = (ListView) findViewById(android.R.id.listview);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_2, recipes);
    l.setAdapter(adapter);
    l.setOnItemClickListener(this);



}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    TextView temp = (TextView) view;
    Toast.makeText(this, temp.getText(), Toast.LENGTH_SHORT).show();
}
}
  

这是xml文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.roman.romansrecipes.MainActivity">


    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
  

我哪里错了?

3 个答案:

答案 0 :(得分:3)

因为android.R.id.xxx仅适用于Android框架中的预定义值,而listView不是其中之一。对于您创建的ID,请使用不带android的R.id.xxx

答案 1 :(得分:1)

首先,您不能将ID命名为您的工作方式,其次,您应该在活动中使用相同的ID名称。

id更改为:

android:id="@+id/myListView"

然后:

l = (ListView) findViewById(R.id.myListView);

答案 2 :(得分:1)

您不必在ListView中初始化ListViewActivityListViewActivity会自动初始化ListView,因为您使用了ID android:id/list。要将适配器设置为Listview,您只需拨打setListAdapter(adapter);即可。如果您想访问ListView,只需通过调用ListView方法获取getListView()

更新您的onCreate方法

@Override
 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
    //l = (ListView) findViewById(android.R.id.listview);
   ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_2, recipes);
   //l.setAdapter(adapter);
   setListAdapter(adapter);
   //l.setOnItemClickListener(this);
   getListView().setOnItemClickListener(this);

}

查看ListActivity