user2467583
user2467583

Reputation: 143

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 ?

Upvotes: 6

Views: 13962

Answers (6)

Kirk
Kirk

Reputation: 5077

Just a simple way. you already inflated the layout. just inherit from that. find the code below

View rootView = inflater.inflate(R.layout.reg_layout, container, false);
text1 = (EditText)rootView.findViewById(R.id.text1); 

return rootView;

Upvotes: 1

Ketan Ahir
Ketan Ahir

Reputation: 6738

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);

Upvotes: 10

VikramV
VikramV

Reputation: 1111

Since you've used a separate View to inflate your layout, you are required to use that view to access all text views/image views/edit text present in that layout.

So to correct your code, you would need to tweak your code like shown below

View rootView = inflater.inflate(R.layout.reg_layout, container, false);
text1 = (EditText) rootView.findViewById(R.id.text1);
return rootView;

Upvotes: 1

InnocentKiller
InnocentKiller

Reputation: 5234

you have used inflater

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

so use

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

and so on.

Upvotes: 3

GrIsHu
GrIsHu

Reputation: 23638

To access the view in the fragment you need to access it through your View class which contains your layout.

You need to change your code as below :

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);
    text1 = (EditText)rootView .findViewById(R.id.text1); 
    //Same way initialize other views also. 
    return rootView;
}
}

Upvotes: 1

androidcodehunter
androidcodehunter

Reputation: 21937

You have to do it before return statement like this way

    View rootView = inflater.inflate(R.layout.reg_layout, container, false);
   text1 = (EditText) rootView.findViewById(R.id.text1);
    return rootView;

Hope it will work.

Upvotes: 2

Related Questions