FindViewByID未定义类型Android错误

时间:2014-06-02 18:46:19

标签: android

我一直在从片段扩展,但是为FindViewByID获取了一个未定义的错误。这是代码

public class hashingfragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_hashing, container, false);

    return rootView;
}
public void Hash(View view){
     EditText edit_text = (EditText)
             getView().findViewById(R.id.editText1);

}

这是xml

  <EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView1"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginTop="17dp"
    android:ems="20"
    android:inputType="textMultiLine"
    android:hint="Enter the Text" >

    <requestFocus />
</EditText>

1 个答案:

答案 0 :(得分:0)

上述评论中人们的建议是:

public class hashingfragment extends Fragment {

    // hold on to your EditText
    private EditText editText;

    @Override
    public View onCreateView(LayoutInflater inflater,
                            ViewGroup container,
                            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_hashing, container, false);
        // get your EditText here from the inflated View
        editText = (EditText) rootView.findViewById(R.id.editText1);
        return rootView;
    }

    public void Hash(View view){
        // now you can use 'editText' however you wish here
    }

}