ListView 中的 LinearLayout

发布于 2024-11-26 13:59:41 字数 4766 浏览 1 评论 0原文

所以我试图将一个有两个 TextView 的 LinearLayout 放在 ListView 中,但我遇到了麻烦,并且程序不断崩溃。 ListView 只有一个元素。我的线性布局的 xml 如下:

<ListView>
<LinearLayout
    android:orientation="vertical" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:gravity="center_vertical"
    android:drawablePadding="14dip"
    android:paddingLeft="15dip"
    android:paddingRight="15dip">
        <TextView 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"/>
        <TextView 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"/>
</LinearLayout>
</ListView>

How do I get this inside of a listview?当我尝试将其放入列表视图时,我在手机上收到一条错误消息“抱歉!”应用程序意外停止。请再试一次。堆栈跟踪如下。

I/dalvikvm( 1692): Debugger thread not active, ignoring DDM send (t=0x41504e4d l=38)
I/dalvikvm( 1692): Debugger thread not active, ignoring DDM send (t=0x41504e4d l=56)
D/AndroidRuntime( 1692): Shutting down VM
W/dalvikvm( 1692): threadid=3: thread exiting with uncaught exception (group=0x4001b170)
E/AndroidRuntime( 1692): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime( 1692): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.acme.activeisclickable/com.acme.activeisclickable.ActiveIsClickable}: java.lang.RuntimeException: Binary XML file line #14: You must supply a layout_width attribute.
E/AndroidRuntime( 1692):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
E/AndroidRuntime( 1692):        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
E/AndroidRuntime( 1692):        at android.app.ActivityThread.access$2200(ActivityThread.java:119)
E/AndroidRuntime( 1692):        at  android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
E/AndroidRuntime( 1692):        at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 1692):        at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 1692):        at android.app.ActivityThread.main(ActivityThread.java:4363)
E/AndroidRuntime( 1692):        at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 1692):        at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 1692):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
E/AndroidRuntime( 1692):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
E/AndroidRuntime( 1692):        at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 1692): Caused by: java.lang.RuntimeException: Binary XML file line #14: You must supply a layout_width attribute.
E/AndroidRuntime( 1692):        at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:438)
E/AndroidRuntime( 1692):        at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:3463)
E/AndroidRuntime( 1692):        at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:3543)
E/AndroidRuntime( 1692):        at android.widget.LinearLayout$LayoutParams.<init>(LinearLayout.java:1265)
E/AndroidRuntime( 1692):        at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:1191)
E/AndroidRuntime( 1692):        at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:45)
E/AndroidRuntime( 1692):        at android.view.LayoutInflater.rInflate(LayoutInflater.java:620)
E/AndroidRuntime( 1692):        at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
E/AndroidRuntime( 1692):        at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
E/AndroidRuntime( 1692):        at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
E/AndroidRuntime( 1692):        at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
E/AndroidRuntime( 1692):        at android.app.Activity.setContentView(Activity.java:1622)
E/AndroidRuntime( 1692):        at com.acme.activeisclickable.ActiveIsClickable.onCreate(ActiveIsClickable.java:35)
E/AndroidRuntime( 1692):        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime( 1692):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
E/AndroidRuntime( 1692):        ... 11 more
I/Process ( 1275): Sending signal. PID: 1692 SIG: 3
I/dalvikvm( 1692): threadid=7: reacting to signal 3
I/dalvikvm( 1692): Wrote stack trace to '/data/anr/traces.txt'

So I'm trying to put a LinearLayout that has two TextViews inside of a ListView but I'm having trouble and the program keeps crashing with what I've tried. The ListView would just have one element. The xml for my Linear Layout is as follows:

<ListView>
<LinearLayout
    android:orientation="vertical" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:gravity="center_vertical"
    android:drawablePadding="14dip"
    android:paddingLeft="15dip"
    android:paddingRight="15dip">
        <TextView 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"/>
        <TextView 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"/>
</LinearLayout>
</ListView>

How do I get this inside of a listview? When I try putting it in a listview, I get an error on the phone saying Sorry! The application has stopped unexpectedly. Please try again. The stacktrace is below.

I/dalvikvm( 1692): Debugger thread not active, ignoring DDM send (t=0x41504e4d l=38)
I/dalvikvm( 1692): Debugger thread not active, ignoring DDM send (t=0x41504e4d l=56)
D/AndroidRuntime( 1692): Shutting down VM
W/dalvikvm( 1692): threadid=3: thread exiting with uncaught exception (group=0x4001b170)
E/AndroidRuntime( 1692): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime( 1692): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.acme.activeisclickable/com.acme.activeisclickable.ActiveIsClickable}: java.lang.RuntimeException: Binary XML file line #14: You must supply a layout_width attribute.
E/AndroidRuntime( 1692):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
E/AndroidRuntime( 1692):        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
E/AndroidRuntime( 1692):        at android.app.ActivityThread.access$2200(ActivityThread.java:119)
E/AndroidRuntime( 1692):        at  android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
E/AndroidRuntime( 1692):        at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 1692):        at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 1692):        at android.app.ActivityThread.main(ActivityThread.java:4363)
E/AndroidRuntime( 1692):        at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 1692):        at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 1692):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
E/AndroidRuntime( 1692):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
E/AndroidRuntime( 1692):        at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 1692): Caused by: java.lang.RuntimeException: Binary XML file line #14: You must supply a layout_width attribute.
E/AndroidRuntime( 1692):        at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:438)
E/AndroidRuntime( 1692):        at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:3463)
E/AndroidRuntime( 1692):        at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:3543)
E/AndroidRuntime( 1692):        at android.widget.LinearLayout$LayoutParams.<init>(LinearLayout.java:1265)
E/AndroidRuntime( 1692):        at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:1191)
E/AndroidRuntime( 1692):        at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:45)
E/AndroidRuntime( 1692):        at android.view.LayoutInflater.rInflate(LayoutInflater.java:620)
E/AndroidRuntime( 1692):        at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
E/AndroidRuntime( 1692):        at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
E/AndroidRuntime( 1692):        at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
E/AndroidRuntime( 1692):        at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
E/AndroidRuntime( 1692):        at android.app.Activity.setContentView(Activity.java:1622)
E/AndroidRuntime( 1692):        at com.acme.activeisclickable.ActiveIsClickable.onCreate(ActiveIsClickable.java:35)
E/AndroidRuntime( 1692):        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime( 1692):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
E/AndroidRuntime( 1692):        ... 11 more
I/Process ( 1275): Sending signal. PID: 1692 SIG: 3
I/dalvikvm( 1692): threadid=7: reacting to signal 3
I/dalvikvm( 1692): Wrote stack trace to '/data/anr/traces.txt'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

桜花祭 2024-12-03 13:59:41

我假设您正在尝试自定义列表项。您应该编写自己的自定义适配器来做到这一点。尝试这个教程或者您可以观看这些视频:

  1. 在 ListView 中呈现数据
  2. 过渡到ListActivity
  3. 美化您的列表:让它发挥作用
  4. 美化您的列表:正确的方法

披露:我是上面提到的视频。

I assume you are trying to customize the list items. You should write your own custom adapter to do that. Try this tutorial or you can check out these videos:

  1. Presenting your data in a ListView
  2. Transitioning to ListActivity
  3. Beautify your List: Get it working
  4. Beautify your List: Rigth way to do it

DISCLOSURE: I'm the author of the above mentioned videos.

东北女汉子 2024-12-03 13:59:41

老实说,这个错误对于我的眼睛来说是痛苦的。但通常情况下
您应该能够创建一个包含一个子元素的列表视图。

我的意思是:

<ListView>
 <LinLayout>
  <TextView/>
  <TextView/>
 </LinLayout>
</ListView>

这应该可行,因为 ListView 只允许一个孩子。但是你的 LinearLayout 可以有任意数量的。如果出现错误,则可能是您的布局充气器。

That error is painful for my eyes to decipher to be honest. but normally
you should be able to create a listview with one child element.

what I'm saying is:

<ListView>
 <LinLayout>
  <TextView/>
  <TextView/>
 </LinLayout>
</ListView>

this should work, since ListView only allows one child. But your LinearLayout can have as many as you want. If there's an error, it might be your layoutinflater.

命比纸薄 2024-12-03 13:59:41

你的布局文件中有 Listview 而不是 ListView (大 V)....

E/AndroidRuntime(1662): 原因为:java.lang.ClassNotFoundException: android.view.Listview

you have Listview not ListView (large V) in you layout file ....

E/AndroidRuntime( 1662): Caused by: java.lang.ClassNotFoundException: android.view.Listview

独木成林 2024-12-03 13:59:41

您必须将 ListView 与 ListAdatper 一起使用。

创建一个实现 ListAdapter 的类,您需要实现的方法之一是:

public abstract View getView (int position, View convertView, ViewGroup parent);

使用该方法,您将为列表中的每个元素返回一个视图,并指定所需的布局。

您还应该记住重用这些视图。
有关如何正确使用 ListView 和 ListAdapter 的详细信息:

http ://dl.google.com/googleio/2010/android-world-of-listview-android.pdf

或视频

http://www.google.com/events/io /2010/sessions/world-of-listview-android.html

干杯,
弗朗西斯科.

You have to use the ListView with a ListAdatper.

Create a class that implements ListAdapter, once of the method that you need to implement is:

public abstract View getView (int position, View convertView, ViewGroup parent);

With that method you will return a view for each of the elements of your list, and specify the layout that you want.

Also you should remember to reuse those views.
For more information about how to use properly ListViews and ListAdapters:

http://dl.google.com/googleio/2010/android-world-of-listview-android.pdf

or the video

http://www.google.com/events/io/2010/sessions/world-of-listview-android.html

Cheers,
Francisco.

找回味觉 2024-12-03 13:59:41

你可以试试这个

  final View layout = getLayoutInflater().inflate(R.layout.add_layout, null, false);
              TextView vr_name = lLayout.findViewById(R.id.name);
              vr_name.setText(v_name);
              linearListView.addView(layout);

You can try this

  final View layout = getLayoutInflater().inflate(R.layout.add_layout, null, false);
              TextView vr_name = lLayout.findViewById(R.id.name);
              vr_name.setText(v_name);
              linearListView.addView(layout);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文