Fragment中的findViewById返回Null

时间:2014-07-25 09:43:32

标签: java android android-fragments

我已经尝试了getActivity().findViewById()getView()。这是我片段中的代码。它们都返回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);

活动代码

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

<?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>

3 个答案:

答案 0 :(得分:5)

更改此

 username = (EditText)getView().findViewById(R.id.username);

 username = (EditText)rootView.findViewById(R.id.username);

并为所有其他Views

执行相同的操作

答案 1 :(得分:2)

getView返回您在onCreateView中夸大的视图,在onCreateView返回之前,该视图不可用。您应该使用膨胀的视图来检索您的组件或覆盖以onViewCreated为参数的View,并使用它来检索您的组件。 Here您可以找到onViewCreated的文档

  

在onCreateView(LayoutInflater,ViewGroup,   Bundle)已经返回,但在恢复任何已保存的状态之前   到了视野。这为子类提供了初始化自己的机会   一旦他们知道他们的视图层次结构已经完全创建。

答案 2 :(得分:0)

你应该这样打电话:

username = (EditText) rootView.findViewById(R.id.username);