所以我试图在ListView中放置一个有两个TextView的LinearLayout,但是我遇到了麻烦,程序一直在用我试过的东西崩溃。 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中获取此内容?当我尝试将其放入列表视图时,我在手机上收到错误提示抱歉!应用程序意外停止。请再试一次。堆栈跟踪如下。
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'
答案 0 :(得分:6)
我假设您正在尝试自定义列表项。您应该编写自己的自定义适配器来执行此操作。试试这个tutorial,或者你可以查看这些视频:
答案 1 :(得分:1)
这个错误让我的眼睛难以破译为诚实。但通常 您应该能够使用一个子元素创建列表视图。
我所说的是:
<ListView>
<LinLayout>
<TextView/>
<TextView/>
</LinLayout>
</ListView>
这应该可行,因为ListView只允许一个孩子。但是您的LinearLayout可以拥有您想要的任意数量。如果出现错误,可能是你的layoutinflater。
答案 2 :(得分:0)
您的布局文件中有Listview
而非ListView
(大V)....
E/AndroidRuntime( 1662): Caused by: java.lang.ClassNotFoundException: android.view.Listview
答案 3 :(得分:0)
您必须将ListView与ListAdatper一起使用。
创建一个实现ListAdapter的类,一个需要实现的方法是:
public abstract View getView (int position, View convertView, ViewGroup parent);
使用该方法,您将返回列表中每个元素的视图,并指定所需的布局。
此外,您应该记住重复使用这些视图。 有关如何正确使用ListViews和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
干杯, 旧金山。
答案 4 :(得分:0)
您必须在布局文件夹中的单独xml文件中描述LinearLayout。然后在运行时使用LayoutInflater类来获取LinearLayout的实例。
inflator.inflate(int resource, ViewGroup root)
或者您可以利用Android适配器轻松填充列表中的正确项目:
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
HashMap<String, String> h = new HashMap<String, String>();
h.put("param", "Nick");
h.put("value", "1-2-3");
h.put("_id", "1");
list.add(h);
h = new HashMap<String, String>();
h.put("param", "Alexander");
h.put("value", "4-5-6");
h.put("_id", "2");
list.add(h);
SimpleAdapter adapter = new SimpleAdapter(TaxiNewActivity.this, list, R.layout.row,
new String[] { "param", "value" }, new int[] { R.id.rowName, R.id.rowPhone });
ListView listView = (ListView) findViewById(R.id.layout);
listView.setAdapter(adapter);
答案 5 :(得分:0)
你可以试试这个
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);