gpt4 book ai didi

android - 布局作为 ListView 标题

转载 作者:搜寻专家 更新时间:2023-11-01 07:40:23 25 4
gpt4 key购买 nike

我有一个包含两个列表的布局。由于您不能将列表放在 ScrollView 中,我正在尝试使用 ExpandableListView 和自定义 SimpleExpandableListAdapter,根据 this question .
列表布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<ExpandableListView android:id="@+id/android:list"
android:layout_width="fill_parent" android:layout_height="fill_parent" />
</LinearLayout>

现在,当我尝试添加一些内容作为标题时,麻烦就来了。如果我从代码创建布局,它会起作用,如下所示:

LinearLayout ll = new LinearLayout( this );
ll.setOrientation( LinearLayout.VERTICAL );
TextView t = new TextView( this );
t.setText( "some text" );
ll.addView( t );
ImageView i = new ImageView( this );
i.setImageResource( R.drawable.icon );
ll.addView( i );
getExpandableListView( ).addHeaderView( ll );

如果我使用相同的布局,甚至比代码中的布局更简单,应用程序甚至在显示在屏幕上之前就崩溃了。这是我想作为标题添加到上面列表中的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical" android:id="@+id/ll">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="some text"/>
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:src="@drawable/icon"/>
</LinearLayout>

还有代码:

LinearLayout ll = (LinearLayout)findViewById( R.id.ll );
getExpandableListView( ).addHeaderView( ll );

我试图调整布局中所有组件的 layout_width 或 height,但它不起作用。最让我感到困惑的是,当它崩溃时,在 DDMS 上调用 setAdapter 时显示 NullPointerException(当我使用代码中的布局时不会出现这种情况,它会很好地添加适配器):

01-16 01:17:27.447: ERROR/AndroidRuntime(21314): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.stephan.test/org.stephan.test.ExpandableListViewTest}: java.lang.NullPointerException
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2833)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2854)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): at android.app.ActivityThread.access$2300(ActivityThread.java:136)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2179)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): at android.os.Handler.dispatchMessage(Handler.java:99)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): at android.os.Looper.loop(Looper.java:143)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): at android.app.ActivityThread.main(ActivityThread.java:5068)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): at java.lang.reflect.Method.invokeNative(Native Method)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): at java.lang.reflect.Method.invoke(Method.java:521)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): at dalvik.system.NativeStart.main(Native Method)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): Caused by: java.lang.NullPointerException
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): at android.widget.ListView.clearRecycledState(ListView.java:506)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): at android.widget.ListView.resetList(ListView.java:492)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): at android.widget.ListView.setAdapter(ListView.java:424)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): at android.widget.ExpandableListView.setAdapter(ExpandableListView.java:475)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): at android.app.ExpandableListActivity.setListAdapter(ExpandableListActivity.java:246)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): at org.stephan.test.ExpandableListViewTest.onCreate(ExpandableListViewTest.java:80)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1066)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2797)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): ... 11 more

所以,我的问题是:我在这里做错了什么?并且,以防万一,关于我如何做得更好的任何建议?

最佳答案

我找到了解决方案。而不是使用
创建新布局LinearLayout ll = (LinearLayout)findViewById( R.id.ll );
你需要给它充气!以下是诀窍:

LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService( Context.LAYOUT_INFLATER_SERVICE );  
LinearLayout ll = (LinearLayout)layoutInflater.inflate( R.layout.ll, null, false );
getExpandableListView( ).addHeaderView( ll );

其中 ll.xml 是您打算作为标题附加到列表的布局。希望这可以帮助别人! :)

关于android - 布局作为 ListView 标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4703017/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com