带Imageview和列表视图的LinearLayout
问题描述:
我正在开发一个应用程序,在其中我需要将徽标左对齐,然后选择右侧的类别列表。因此,我所拥有的是图像视图中的标志,它位于线性布局中,然后向LinearLayout添加一个ListView,以便它们可以在同一个活动上。当我尝试运行它时,出现意外停止的错误。带Imageview和列表视图的LinearLayout
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Commands.setText("Commands");
LinearLayout LL = new LinearLayout(this);
ImageView Logo = (ImageView)findViewById(0x7f020000);
Logo.setImageDrawable(getResources().getDrawable(android.R.drawable.ic_menu_myplaces));
ListView Cats = new ListView(this);
String Categories[] = new String[3];
Categories[0] = "Hardware";
Categories[1] = "Language";
Categories[2] = "Libraries";
ArrayAdapter<String> list = new ArrayAdapter<String>(this, 0, 2, Categories);
Cats.setAdapter(list);
LL.addView(Logo);
LL.addView(Cats);
setContentView(LL);
}
任何帮助,我真的很困惑
答
确定。我认为问题在于,您没有在onCreate()中调用setContentView()。
很明显,Android不知道在哪里寻找ImageView
与该ID,你会最有可能运行到空指针。
添加setContentView()
您onCreate()
thsi前行,
ImageView Logo = (ImageView)findViewById(0x7f020000);
答
创建布局最简单的方法在Android是在RES /布局文件夹中的XML创建布局和它充气,当你加载你的onCreate方法。例如这个XML可能完成你在找什么:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/(name for image view)"
android:layout_width="(some size)"
android:layout_height="(some size)"
>
</ImageView>
<ListView
android:id="@+id/(name for list view)"
android:layout_width="(some size)"
android:layout_height="fill_parent">
</ListView>
<LinearLayout>
你应该能够找到与XML创建布局大量的教程。