0

So as you can see in the onCreate method i call getView on the 0th fragment in the "fragments" list. It returns null. I've tried putting a timer in the code and have it run every second and check if the view is null. And on the second tick of the timer the view is NOT null. Basically

MainActivity.java

package com.axinite.standapp;

import android.graphics.Color;
import android.os.CountDownTimer;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import android.view.ViewParent;
import android.widget.EditText;
import android.widget.NumberPicker;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;

public class MainActivity extends AppCompatActivity {

    int i = 0;

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the sections. We use a
     * {@link FragmentPagerAdapter} derivative, which will keep every
     * loaded fragment in memory. If this becomes too memory intensive, it
     * may be best to switch to a
     * {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
    private SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    private ViewPager mViewPager;

    int standUpSeconds = 4000;

    List<android.support.v4.app.Fragment> fragments = new Vector<android.support.v4.app.Fragment>();

    NumberPicker SUPickerH;
    NumberPicker SUPickerM;
    NumberPicker SUPickerS;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

        setSupportActionBar(toolbar);
        fragments.add(Fragment.instantiate(this, BlankFragment.class.getName()));

        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), fragments);
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);
        final TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);
        tabLayout.setSelectedTabIndicatorColor(Color.WHITE);
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        final int[] ICONS = new int[] {
                R.drawable.walking,
                R.drawable.iris
        };
        for (int i=0; i < tabLayout.getTabCount(); i++)
        {
            tabLayout.getTabAt(i).setIcon(getDrawable(ICONS[i]));
        }//endfor)
        View view = ((BlankFragment) fragments.get(0)).getView();
        if(view != null) {
            TextView tv = (TextView) view.findViewById(R.id.glupost);
            tv.setText("dasdas");
            //Toast.makeText(getApplicationContext(), ((Fragment) fragments.get(0)).toString(), Toast.LENGTH_SHORT).show();
        }
    }

    void Setup() {



    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {
        List<Fragment> fragments;
        public SectionsPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
            super(fm);
            this.fragments = fragments;
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return fragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {

            return "";
        }
    }
}

BlankFragment.java

    package com.axinite.standapp;

import android.app.Activity;
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;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link BlankFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link BlankFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class BlankFragment extends Fragment {

    protected View mView;
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment BlankFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static BlankFragment newInstance(String param1, String param2) {
        BlankFragment fragment = new BlankFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    public BlankFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

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

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnFragmentInteractionListener) activity;
        } catch (ClassCastException e) {

        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p/>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        public void onFragmentInteraction(Uri uri);
    }

}
Axinite
  • 193
  • 1
  • 1
  • 6
  • there is nowhere in your code a call to `getView` – njzk2 Nov 08 '15 at 00:38
  • What is it you're ultimately attempting to achieve? Do you just want to set a `TextView` inside your `Fragment`? – PPartisan Nov 08 '15 at 01:15
  • seems pretty clear: the views in the fragments are not available until `onViewCreated` is called in the fragment. in the Activity's onCreate, they haven't been created yet. And are necessarly null – njzk2 Nov 08 '15 at 01:26
  • I want to have access ( From the main Activity. Not the Fragment class ) to a TextView that's inside a fragment and that fragment is inside of a ViewPager. Am i really asking for too much? I've been struggling with this for 5 days. – Axinite Nov 08 '15 at 01:26
  • Yes. I thought so. So how can i access them AFTER onViewCreated from my MainActivity? – Axinite Nov 08 '15 at 01:31

1 Answers1

0

The typical method for an Activity to communicate with its Fragments is as follows:

MyFragment frag = (MyFragment) getFragmentManager().findFragmentById(R.id.container_of_fragment);
frag.getMyTextView().setText("New text");

or (often preferred IMO)

MyFragment frag = (MyFragment) getFragmentManager().findFragmentByTag("fragment_tag_string_value");
frag.getMyTextView().setText("New text");

This is rendered more difficult with a ViewPager however, as the "id" of the container is the ViewPager itself, which contains multiple fragments. In addition, there isn't an in built adapter mechanism (that I'm aware of) to set tags to fragments.

There are two methods I can think of that circumvent this. First, you can use an EventBus, such as GreenRobot's EventBus, or LocalBroadcastManager from the Android API. GreenRobot's EventBus is also the recommended technique in the answer to this similar question.

An alternative would leverage the fact that a Fragment always has a reference to its host Activity, which means the Fragment could request the text from the Activity via an interface, which returns it to the Fragment:

public class MainActivity implements FragmentOne.Callbacks {

    //...
    @Override
    public String requestTextViewString() {
        return "Text to send to Fragment"
    }

And inside your hypothetical FragmentOne:

public class FragmentOne extends Fragment {

    private Callbacks mCallback;
    private TextView mTextView;

    public interface Callbacks {
        String requestTextViewString();
    }

    @Override
    public void onAttach(Activity activity) {
        if (activity instanceof Callbacks) {
            mCallback = (Callbacks) activity;
        }
    }

/*
*Later in your Fragment, when you want to get text from your MainActivity
* i.e. in onCreateView()
*/
    mTextView.setText(mCallback.requestTextViewString());

Edit: Also keep in mind that attempting to access your Fragment from onCreate() in your Activity is likely to result in a NullPointerException. This diagram shows both lifecycles alongside each other. This shouldn't be a problem however, as anything required during the "set-up" lifecycle events of the Fragment can just be passed as an argument in the Fragment's newInstance() method:

public class FragmentOne extends Fragment {

    private static final String KEY = "text_for_text_view_key";

    public static FragmentOne newInstance(String textForTextView) {
        Bundle args = new Bundle();
        FragmentOne frag = new FragmentOne();
        args.putString(KEY, textForTextView);

        frag.setArguments(args);
        return frag;
    }

//And when required...
    mTextView.setText(getArguments().getString(KEY));
Community
  • 1
  • 1
PPartisan
  • 8,173
  • 4
  • 29
  • 48