[英]findViewById in Fragment returns Null
I've tried both getActivity().findViewById()
and getView()
. 我已经尝试过
getActivity().findViewById()
和getView()
。 This is the code in my fragment. 这是我片段中的代码。 They all return null..
它们都返回null。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.edit_viewer, container, false);
Bundle bundle = getArguments();
id = bundle.getLong("VIEWER_ID");
DatabaseHelper dbHelper = new DatabaseHelper(getActivity());
username = (EditText)getView().findViewById(R.id.username);
password = (EditText)getView().findViewById(R.id.entry);
hostUrl = (EditText)getView().findViewById(R.id.hostUrl);
webApp = (EditText)getView().findViewById(R.id.webApp);
modelId = (EditText)getView().findViewById(R.id.modelId);
properties = (EditText)getView().findViewById(R.id.properties);
Code from Activity 活动代码
public class EditViewerActivity extends ActionBarActivity {
private Long id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_viewer);
Intent intent = getIntent();
Long id = intent.getLongExtra("VIEWER_ID", 0);
ViewerEditFragment newFragment = new ViewerEditFragment();
Bundle bundle = new Bundle();
bundle.putLong("VIEWER_ID", id);
newFragment.setArguments(bundle);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, newFragment)
.commit();
}
}
edit_viewer.xml edit_viewer.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/edit_viewer"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
style="?android:attr/listSeparatorTextViewStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="sfn Credentials"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/username"
android:paddingLeft="10dp" />
<EditText
android:id="@+id/username"
android:hint="Admin"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/password"
android:paddingLeft="10dp" />
<EditText
android:id="@+id/entry"
android:inputType="textPassword"
android:hint="Password"
android:minWidth="100dip"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<TextView
style="?android:attr/listSeparatorTextViewStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Host Settings"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/host_url"
android:paddingLeft="10dp" />
<EditText
android:id="@+id/hostUrl"
android:hint="http://www.mysfn.com"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/web_app"
android:paddingLeft="10dp" />
<EditText
android:id="@+id/webApp"
android:hint="sfn"
android:minWidth="100dip"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/model_id"
android:paddingLeft="10dp" />
<EditText
android:id="@+id/modelId"
android:hint="aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/properties"
android:paddingLeft="10dp" />
<EditText
android:id="@+id/properties"
android:hint="dv=icon"
android:minWidth="100dip"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<Button android:id="@+id/close"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Save" />
</LinearLayout>
Change this 改变这个
username = (EditText)getView().findViewById(R.id.username);
to 至
username = (EditText)rootView.findViewById(R.id.username);
and do the same for all other Views
并对所有其他
Views
执行相同操作
getView
returns the view you inflated in onCreateView
, and before onCreateView
returns it is not available. getView
返回您在onCreateView
的视图,在onCreateView
返回之前,该视图不可用。 You should use the inflated view to retrieve your component or override onViewCreated
that takes the inflated View
as parameter, and use it to retrieve your component. 您应该使用膨胀后的视图来检索您的组件,或者重写以膨胀后的
View
作为参数的onViewCreated
,并使用它来检索您的组件。 Here you can find the documentation for onViewCreated 在这里您可以找到onViewCreated的文档
Called immediately after onCreateView(LayoutInflater, ViewGroup, Bundle) has returned, but before any saved state has been restored in to the view.
在onCreateView(LayoutInflater,ViewGroup,Bundle)返回之后但在任何保存的状态恢复到视图中之前立即调用。 This gives subclasses a chance to initialize themselves once they know their view hierarchy has been completely created.
一旦子类知道其视图层次结构已完全创建,这将给子类一个初始化自身的机会。
您应该这样呼叫:
username = (EditText) rootView.findViewById(R.id.username);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.