Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I can't use findViewById

Why can't I use findViewById in this file?

Regfragment :

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;


public class RegFragment extends Fragment {

EditText text1,text2,text3;
Button btn1;

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

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

    return rootView;


}
}

and then I tried to make like this:

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;


public class RegFragment extends Fragment {

EditText text1,text2,text3;
Button btn1;

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

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

    return rootView;

    text1 = (EditText) findViewById(R.id.text1);   -----> ERROR....
}
}

I got error
Is it because I use Fragments ?

like image 606
user2467583 Avatar asked Dec 25 '13 09:12

user2467583


1 Answers

you can not use it in fragment because it's method of activity

try following

in onActivityCreated()

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

or in onCreateView()

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

like image 157
Ketan Ahir Avatar answered Nov 08 '22 02:11

Ketan Ahir