我已经寻找了很长时间,也找不到解决我的问题的方法。我正在尝试创建一个带有AutoCompleteTextView的对话框。我遵循了Android开发者网站上的教程,它工作得很好。我之前已经成功地使用了对话框上的布局,所以我想这也会很简单。我为我的对话框创建了一个布局,并确保AutoCompleteTextView有一个ID。这是有趣的事情发生的地方…
dialog.setContentView(R.layout.auto_layout);
AutoCompleteTextView auto_tv = (AutoCompleteTextView)findViewById(R.id.role_ac);这也是布局。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Role" />
<AutoCompleteTextView android:id="@+id/role_ac" android:layout_width="280dip" android:layout_height="wrap_content"/>
<Button android:layout_height="wrap_content"
android:layout_width="fill_parent" android:text="Done"
android:id="@+id/auto_doneButton" />
</LinearLayout>由于某些原因,即使在布局auto_layout中确实存在auto_tv,它也是null。我能够在对话框中获得AutoCompleteTextView的唯一方法是以编程方式构建布局。当我尝试使用AutoCompletTextView时,为什么它是空的?我是不是在布局中忘记了什么,或者我没有正确地构造对象?在这方面的任何帮助都将非常感谢。谢谢。
发布于 2010-11-24 02:51:20
您在对setContentView和findViewById的调用中混合了两个不同的上下文。在第一条语句中,您将设置对象dialog的内容视图。在第二个语句中,您将在父活动中查找视图。您希望使用对话框对象进行这两个调用。正确的语法应该是:
dialog.setContentView(R.layout.auto_layout);
AutoCompleteTextView auto_tv = (AutoCompleteTextView) dialog.findViewById(R.id.role_ac);发布于 2010-11-24 02:50:12
好的。我就想明白了。查看了太多的代码,无法捕捉到我的错误。这就是解决方案。
dialog.setContentView(R.layout.auto_layout);
AutoCompleteTextView auto_tv = (AutoCompleteTextView)dialog.findViewById(R.id.role_ac);我需要指定使用对话框的布局,而不是不使用dialog.findViewByID.所隐含的主应用程序的布局
https://stackoverflow.com/questions/4259559
复制相似问题