-4

Basically the problem is that, when you want to use the method "FindViewById" or "GetSystemService" or other does not let me.

He only lets me use them in MainActivity, I wanted to know how I can solve this problem.

I'm new to android Studio and I'm basically learning

MainAtivity

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position){
            case 0:
                tap1alarmas tap1 = new tap1alarmas();
                return tap1;
            case 1:
                tap2tips tap2 = new tap2tips();
                return tap2;
            case 2:
                tap3estadisticas tap3 = new tap3estadisticas();
                return tap3;
            default:
                return null;

        }
    }

Fragment "Tap1alarmas"

import android.app.AlarmManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;

import java.util.Calendar;


public class tap1alarmas extends Fragment {


AlarmManager alarm_manager;
TimePicker alarm_timepicker;
TextView update_text;
tap1alarmas context;


private MainActivity.SectionsPagerAdapter mSectionsPagerAdapter;

private ViewPager mViewPager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

this.context = this;

//Inicizaliar alarmmanager
alarm_manager = (AlarmManager) getSystemService(ALARM_SERVICE);

//Inicializar timepicker

alarm_timepicker = (TimePicker) findViewById(R.id.TimePicker);

//Text uptate`enter code here`

update_text = (TextView) findViewById(R.id.update_text);
Bruno
  • 3,872
  • 4
  • 20
  • 37
  • 1
    Those are functions on Context. You must call them either in a Context, or on a Context. – Gabe Sechan Oct 03 '18 at 18:55
  • 1
    In very simple language: you can use **findViewById(R.id.your_id)** anywhere in your Activity, But when you're in a Fragment, you need to have a reference to your View object (which is accessible from onCreateView method) **view.findViewById(R.id.your_id);** – Joe Oct 03 '18 at 19:01

1 Answers1

0

If you're trying to use findViewById in Fragment, first of all make sure you're using onCreateView() and you're returning the view at the end.

See the differences in here: Difference and uses of onCreate(), onCreateView() and onActivityCreated() in fragments

Here is an example for that:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_single_image, parent, false);

    alarm_timepicker = (TimePicker) rootView.findViewById(R.id.TimePicker); 
    // your widgets with rootView prefix

    return rootView;
}

rootView is the point here. So, if you want to declare any widgets (or etc), use rootView in this case as a prefix before referecning your id by findViewById.

ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108