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 ?
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With