Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get string array from xml to fragment's java file?

Tags:

java

android

Below code shows null pointer exception..

public class MyFragment extends Fragment {

private String[] eArray = getActivity()
                              .getResources()
                              .getStringArray(R.array.English);

What are the other options, searching in stackoverflow did not give me success.. I am using FragmentPagerAdapter also.. which is not related to this eArray.. but it contains data for images.

like image 689
Axay Prajapati Avatar asked Dec 02 '25 00:12

Axay Prajapati


1 Answers

getActivity() is returning null because you are using it before fragment get attached to Activity.

You need to initialize this at onCreateView

@Override
public View onCreateView(LayoutInflater inflater,
      ViewGroup container, Bundle savedInstanceState) {
    eArray = getActivity().getResources().getStringArray(R.array.English);
}
like image 94
Pankaj Kumar Avatar answered Dec 03 '25 12:12

Pankaj Kumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!