2

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
Zoe
  • 27,060
  • 21
  • 118
  • 148
Enrique Tasa
  • 25
  • 1
  • 7
  • Hi, is it the bundle that is returning null or your textview? You can text those separately by adding a debug stop point for each of those or even a log and check what gets printed out when the data comes as a bundle and what happens when you try to initialize the textview. I feel @brian's answer is probably correct if you're already getting the data within the bundle. – Francislainy Campos Mar 05 '19 at 20:26

3 Answers3

1

The IDE is telling you getView() is not an Activity method (it is however a Fragment method). In an Activity you can simply call findViewById().

TextView textViewBox = (TextView) findViewById(R.id.textView2);
Bryan
  • 14,756
  • 10
  • 70
  • 125
  • This answer is correct, my issue was different (in hindsight I believe I didn't explain everything correctly on the question), but once I referenced a TextView within the activity .xml file this worked. Thank you! – Enrique Tasa Mar 06 '19 at 11:12
1

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

Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
  • This answer did it for me - the key being your "setContentView(R.layout.yourLayoutFromActivity2)". My original code was returning null because I was trying to find a TextView that was defined in an .xml file different from the one that was set as the contentView (in my case, a fragment that wasn't launched yet). Thank you very much for your help, if anyone has a similar problem take care of looking at what view you're manipulating! – Enrique Tasa Mar 06 '19 at 11:10
  • If it worked feel free to mark this answer as a correct :D – Skizo-ozᴉʞS ツ Mar 06 '19 at 16:00
0

The getView() is a fragment method. You need to use findViewById() in the activity to initialize the view. Also move the initialization code to onCreate method and process the intent there as well.

Mobin Munir
  • 9
  • 1
  • 1
  • 4