有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java findViewByID方法不接受listview

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

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

"error: cannot find symbol variable listview"

然而,每个教程和示例都是这样做的。我已经用我能想象到的任何方式修改了xml文件中的ID,现在将其返回到“安卓:ID=“@安卓:ID/list”“

Now here is the Activity:

package com.example.roman.romansrecipes;

import 安卓.app.ListActivity;
import 安卓.os.Bundle;
import 安卓.view.View;
import 安卓.widget.AdapterView;
import 安卓.widget.ArrayAdapter;
import 安卓.widget.ListView;
import 安卓.widget.TextView;
import 安卓.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(安卓.R.id.listview);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 安卓.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();
}
}

and here is the xml file

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


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

Where am I going wrong?


共 (3) 个答案

  1. # 1 楼答案

    您不必在ListViewActivity中初始化ListViewListViewActivity将自动初始化ListView,就像您使用了id android:id/list。要将适配器设置为Listview,只需调用setListAdapter(adapter);。如果你想访问ListView,只需通过调用getListView()方法获取ListView

    更新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

  2. # 2 楼答案

    首先,你不能像现在这样给id命名,其次,你应该在活动中使用相同的id名称

    改变你的id比如:

    android:id="@+id/myListView"
    

    然后:

    l = (ListView) findViewById(R.id.myListView);
    
  3. # 3 楼答案

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