android - ListView 中的线性布局

标签 android listview android-linearlayout

因此,我试图将一个具有两个 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>

如何在 ListView 中获取它?当我尝试将它放入 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'

最佳答案

我假设您正在尝试自定义列表项。您应该编写自己的自定义适配器来执行此操作。试试这个 tutorial或者您可以查看这些视频:

  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

披露:我是上述视频的作者。

关于android - ListView 中的线性布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6860409/

相关文章:

android - ScrollView 不环绕其内容

java - 在 LinearLayout 中居中多个 Framelayout

android - 启动应用程序时 react native android 错误

android - Eclipse调试崩溃问题

android - 在Android中为SQLite数据库构建LoaderManager

android - 将元素定位在线性布局的不同方向

安卓编程 : Background color not fully covering fragments

java - Android saxparser CDATA 无知

android - 下拉刷新并加载到 ListView 中

java - 在ListView中显示外部SD卡中的音乐文件列表