Android 使用片段布局。。。。。FindByViewWid始终为空
我是android开发的新手,我正在努力让工作成为一个简单的例子 当布局包含片段时,该片段被膨胀,并且方法findByViewId返回空指针。我想知道如何避免这种情况Android 使用片段布局。。。。。FindByViewWid始终为空,android,layout,fragment,findviewbyid,Android,Layout,Fragment,Findviewbyid,我是android开发的新手,我正在努力让工作成为一个简单的例子 当布局包含片段时,该片段被膨胀,并且方法findByViewId返回空指针。我想知道如何避免这种情况 activity_main.xml <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id
activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.todoit.MainActivity"
tools:ignore="MergeRootFrame" />
activity\u main.xml
到目前为止,我找到的唯一解决方案是不使用片段布局:/
fragment_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.todoit.MainActivity$PlaceholderFragment">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/myEditText"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/myListView"
android:layout_below="@+id/myEditText"
android:layout_alignRight="@+id/myEditText"
android:footerDividersEnabled="false" /> </RelativeLayout>
fragment_main.xml
此代码返回空指针
setContentView(R.layout.activity_main);列表视图myListView=
(ListView)findViewById(R.id.myListView)
我找到的唯一办法就是用这个来代替
setContentView(R.layout.fragment_main);列表视图myListView=
(ListView)findViewById(R.id.myListView)
所有这些代码都在onCreate方法中
有人能告诉我如何解决这个问题吗?有没有办法将整个布局(包括碎片)充气
非常感谢。
JoseListView myListView=(ListView)fragment.getView().findViewById(R.id.myListView);需要从片段类代码内部调用,而不是从主活动中调用。在activitycreated方法内部调用它,到那时fragment的onCreateView函数就已经成功了您应该只在片段代码中膨胀片段的视图,而不是在活动代码中膨胀片段的视图。您可以使用
onCreateView
方法来执行此操作
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_main, container, false);
ListView myListView= (ListView) v.findViewById(R.id.myListView);
return v;
}
如果要在调用了onCreateView
之后查找ListView
,可以使用getView()
方法:
ListView myListView= (ListView) getView().findViewById(R.id.myListView);
这不起作用,因为
Fragment
类上没有findViewById()
方法。这是正确的,但您的答案没有提到getView()
。请添加它,我将删除否决票。已完成,非常感谢。但是,在我获得Id后,我现在需要使用的一些其他代码在该方法中不起作用。aa=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,todoItems);它说它找不到构造函数。。。在“活动”中找到之前,onCreate没有问题。:?将此更改为getActivity()
<代码>片段
不是从上下文
继承的。之前在活动内部的onCreate方法中工作的代码,在onCreateView方法中根本不做任何事情。基本上我会尝试:ListView myListView=(ListView)rootView.findViewById(R.id.myListView);final EditText myEditText=(EditText)rootView.findViewById(R.id.myEditText)。。。。myListView.setAdapter(aa);myEditText.setOnKeyListener(新视图.OnKeyListener(){public boolean onKey(…)});返回rootView;}基本上,我只想获取列表的ID和编辑文本,并捕获键笔划,以便向listview添加新项。这是ToDoList应用程序的一个非常简单的示例。。。但对于片段,我无法让它工作。@user1564193我认为如果你发布一个新问题并给出更新的答案会更好。堆栈溢出应该有一个关于一个主题的问题和相关答案。