Fragment error: can not find symbol method findViewById (int)

0

I'm working with fragments, and I made a new fragment in which I need to use a button that turns on the phone's flashlight. The problem is that when I try to declare it, it gives me an error, (I still get an error in @Override):

package com.example.oscarsierra.bladenotes;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;


public class LinternaFragment extends Fragment {
Button button;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener(){
        @Override
        public void OnClick(View v){

        }
    });
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_linterna, container, false);
}
}

    
asked by Oscar Anibal 17.07.2018 в 05:12
source

1 answer

0

To be able to associate the objects that you have created in the view, in the fragments it is a little different.

You have to do it in the onCreateView method, not in the onCreate method.

To be able to call the findViewById method you have to do it in the following way:

View view = inflater.inflate(R.layout.nombreTab, container, false);

Where container is an attribute of the method, as you can see in your code. Then, you can associate it from the view object:

button = (Button) view.findViewById(R.id.button);
    
answered by 17.07.2018 / 08:27
source