LinearLayout in a ListView
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): 开发者_如何学Go 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'
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:
- Presenting your data in a ListView
- Transitioning to ListActivity
- Beautify your List: Get it working
- Beautify your List: Rigth way to do it
DISCLOSURE: I'm the author of the above mentioned videos.
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.
you have Listview
not ListView
(large V) in you layout file ....
E/AndroidRuntime( 1662): Caused by: java.lang.ClassNotFoundException: android.view.Listview
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.
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);
精彩评论