getView() returns null in second activity

Enrique Tasa :

I have an app that builds a bundle and passes it to a second activity, for the data within the bundle to be used later.

So far I just want to display one of the elements in the bundle in a TextView, to be sure that I can handle the data in activity two. I'm calling getView().findViewByID(R.id.theTextViewIWant), but it is always returning null, and the IDE says it cannot resolve getView(). I believe it has got to be something I don't fully understand about the View that holds the second activity, so I'd appreciate any help.

public class MyClass extends AppCompatActivity {

    private SectionsPagerAdapter mSectionsPagerAdapter;

    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mark_student);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);
    }

    @Override
    protected void onStart(){
        super.onStart();

        Bundle receivedInfo = getIntent().getExtras();
        String unitSelected = receivedInfo.getString("key");

        TextView textViewBox = (TextView) getView().findViewById(R.id.textView2);
        textViewBox.setText(unitSelected);
    }

I've tried these two other ways of getting the view object, and they haven't worked either:

ViewGroup rootView = (ViewGroup) ((ViewGroup) this.findViewById(android.R.id.content)).getChildAt(0); //nope

View fragmentView = getView(); //neither
Skizo-ozᴉʞS :

I'm calling getView().findViewByID(R.id.theTextViewIWant), but it is always returning null, and the IDE says it cannot resolve getView()

I recommend to you to read getView() method documentation, but there's a small explanation

Get a View that displays the data at the specified position in the data set.

So if you want to find a view that's not on a data set, for instance in your example TextView then you want to use findViewById(int)

Finds a view that was identified by the android:id XML attribute that was processed in onCreate(Bundle).

As it says in the documentation I recommend you to put it on onCreate() method, instead of onStart().

Then you have to remove the getView() and do it like :

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.yourLayoutFromActivity2);
  TextView textViewBox = (TextView) findViewById(R.id.textView2);
}

Note : if you put your stuff in onStart() it means that every-time the app come from foreground will execute all of you have inside of it. I also recommend to you to take a look at Android life cycle

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=151891&siteId=1