Java 带ListAdapter布局的Android列表Android Studio

Java 带ListAdapter布局的Android列表Android Studio,java,android,list,layout,adapter,Java,Android,List,Layout,Adapter,我正在使用android studio,我有这个列表,但我无法让布局正常工作。(你会注意到,我有一个侦听器,当你点击一个项目时,它会打开另一个活动,而且效果很好)。 以下是我的activity_estoque.xml文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:o

我正在使用android studio,我有这个列表,但我无法让布局正常工作。(你会注意到,我有一个侦听器,当你点击一个项目时,它会打开另一个活动,而且效果很好)。 以下是我的activity_estoque.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
        <TextView
        android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="10dip"
        android:textSize="16sp"
        android:textStyle="bold" >
    </TextView>
</LinearLayout>
但是如果我这样做,我会得到以下错误,我不确定添加这个
setContentView…
是否有意义:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.asus.mingausfashionmoda/com.example.asus.mingausfashionmoda.estoque}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
这是:

at com.example.asus.mingausfashionmoda.estoque.onCreate(estoque.java:20)
这就是我在
estoque.java.20:

setContentView(R.layout.activity_estoque);

有人能帮忙吗?谢谢。

您正在扩展ListActivity,因此当您启动该活动时,它将使用默认布局。如果您尝试执行
setContentView(R.layout.activity\u estoque)在代码中,活动将期望您的布局具有id为“list”的列表视图

编辑-- 将布局更改为

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
        <TextView
        android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="10dip"
        android:textSize="16sp"
        android:textStyle="bold" >
    </TextView>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btnttt"
        android:text="This is a button"
        />
<ListView android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#00FF00"
        android:layout_weight="1"
        android:drawSelectorOnTop="false"/>
</LinearLayout>

然后更改this.setListAdapter(新的ArrayAdapter(this,R.layout.activity\u estoque,R.id.label,adobe\u产品))
this.setListAdapter(新的ArrayAdapter(this,R.layout.activity_test,R.id.label1,adobe_产品))

这里以示例的形式解释您想要实现的目标。上面的蓝色条实际上可以通过操作条实现。此操作栏可配置颜色、图标、标题、子标题和菜单(显示/不显示)。

在ListActivity中,必须声明ListView。而你没有这样做。在XML中添加一个默认的ListView,你是说,像这样<代码>受保护的void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);ListView l=(ListView)findViewById(R.id.list);//字符串que pega do query……。
你能发布你的
活动吗\u estoque.xml
?不,保留你的R.layout.estoque,但在xml中声明一个属性我可能做得不对,但错误仍然在estoque.java.20上(与我之前说过的错误相同)这是estoque.java
protected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_estoque);ListView l=(ListView)findViewById(R.id.list);
这是xml编辑(我保留了Textview):
(对于SaNtoRiaN,activity_estoque.xml已经存在)如果我删除
exetresistactivity
我就不能使用这个:
this.setListAdapter(新的ArrayAdapter(this,R.layout.activity\u estoque,R.id.label,adobe\u products));ListView lv=getListView()
@BrunoBrandãoBorges抱歉错过了您的第二个屏幕截图,答案已更新。如果我将xml文件更改为您的,并且不添加
setContentView(R.layout.activity_estoque)
,我会得到与图4相同的结果(带有多个按钮的一个)。如果我添加
setCont…
我在
estoque.java.20
上得到了与我之前所说的相同的错误…我必须对我在xml文件中添加的
estoque.java
类上的
ListView
做些什么吗?因为如果我在代码中不使用它,我认为添加它没有任何意义;谢谢!这已经奏效了!现在唯一的事情是缺少顶部的蓝色条,如果您知道如何使用它,我将不胜感激,因为我可以在设计调色板上看到它,但在运行应用程序时它不会显示…无论如何,谢谢:D@BrunoBrandãoBorges发布你正在使用的应用程序主题和样式。好吧,这是我需要的教程。如果我不能让它工作,我会拿b看得更清楚,谢谢!我觉得蓝色条奇怪的是,如果我删除所有这些列表代码,我就可以看到它。就像这个列表正在使用我的整个页面,并覆盖蓝色条。如果我可以将列表设置为低一些像素,我想我可以看到蓝色条并添加我想要的按钮。但是如果我在xml文件上添加任何内容它将与列表一起重复。。。
at com.example.asus.mingausfashionmoda.estoque.onCreate(estoque.java:20)
setContentView(R.layout.activity_estoque);
 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
        <TextView
        android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="10dip"
        android:textSize="16sp"
        android:textStyle="bold" >
    </TextView>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btnttt"
        android:text="This is a button"
        />
<ListView android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#00FF00"
        android:layout_weight="1"
        android:drawSelectorOnTop="false"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
<TextView
    android:id="@+id/label1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</LinearLayout>