-7

Hello, I have a Fragment for a ViewPager:

package my.test.myapplication;

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

public class Fragment1 extends Fragment {

    private Button btn;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // point A
        return  inflater.inflate(R.layout.frag1,container,false);
    }
}

When I would add this under point A, it comes a error message:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // point A
    btn = (Button) findViewById(R.id.button);
    return  inflater.inflate(R.layout.frag1,container,false);
}

The error message:

Cannot resolve method 'findViewById(int)'

What can I do?

ThePi
  • 77
  • 2
  • 6

6 Answers6

3

If you want to access any view, you have view object to access it.

  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          View view =   inflater.inflate(R.layout.frag1,container,false);
         btn = (Button)view.findViewById(R.id.button);

       return view;
   }
Tejas
  • 358
  • 5
  • 23
0

Try below code in OnViewCreated() method:

btn = (Button) view.findViewById(R.id.button);
nivesh shastri
  • 430
  • 2
  • 13
0

try this if want to bind your controls in fragment than your have to first get the your layout in to a view using LayoutInflater than with the help of that view you can bind your all controls like this

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.frag1,container,false);
        btn = (Button) rootView.findViewById(R.id.button);
        return rootView  ;
    }
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • 1
    I had saved you c: –  Aug 31 '17 at 09:38
  • and may know the reason for down votes – AskNilesh Aug 31 '17 at 09:43
  • The only problem that I see is a missed "=" –  Aug 31 '17 at 09:44
  • TBH all answers here are wrong, because everyone should use ButterKnife. –  Aug 31 '17 at 09:45
  • 3
    @PavelSavchkov. Using ButterKnife is something that a developer might prefer, but that doesn't make other answers wrong – 0xDEADC0DE Aug 31 '17 at 09:47
  • @0xDEADC0DE C'mon, that's just kind of joke. On the other hand, I really recommend ButterKnife for everyone. It makes the code a lot cleaner. –  Aug 31 '17 at 09:49
  • 1
    @PavelSavchkov. I'm fine with that, but there are users around here, like OP, that have no clue about what they're doing and they might not see your comment as a joke. They might really think that the other answers are wrong although they are clearly not – 0xDEADC0DE Aug 31 '17 at 09:53
  • @0xDEADC0DE Tbh, I just don't realize why the people can't just use google for a such simple problem. It's just like: "Oh, ok let just someone else solve the problem". I think that it's not how anyone should learn programming or even whatever. –  Aug 31 '17 at 10:01
  • @PavelSavchkov. I agree with you on that. Especially questions like these. – 0xDEADC0DE Aug 31 '17 at 10:12
0

Try that

 view.findViewById(R.id.expandable);
Harshit Trivedi
  • 764
  • 9
  • 33
0

You can try like this:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{
  View rootView = inflater.inflate(R.layout.frag1, container, false);
  btn = (Button)rootView.findViewById(R.id.button);
  return  rootView ;
}
Vishal Vaishnav
  • 3,346
  • 3
  • 26
  • 57
0

A Fragment is not a subclass of Activity.

You have to call findViewById(R.id.*id*); on a Activity object.

See API.