-1

I'm trying to get the strings from strings.xml file, but I get this error:java.lang.IllegalStateException: Fragment Ric_fragment{3975f30e} not attached to Activity

public class Ricerca_fragment_menu extends android.support.v4.app.Fragment {

private ListView lista;

String[] itemname ={
        getResources().getString(R.string.ric_e),
        getResources().getString(R.string.ric_u),
        getResources().getString(R.string.ric_p),
        getResources().getString(R.string.txt_c),
        getResources().getString(R.string.ric_c),
        getResources().getString(R.string.ben)
};
...
user2847219
  • 555
  • 1
  • 16
  • 27
  • possible duplicate of [Fragment MyFragment not attached to Activity](http://stackoverflow.com/questions/10919240/fragment-myfragment-not-attached-to-activity) – Ed Holloway-George May 24 '15 at 20:54

1 Answers1

3

You can't call getResources() before onCreate, because the Fragment is not completely created. You should modify your code like this:

String[] itemname;

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

    itemname = new String[] {
            getResources().getString(R.string.hello_blank_fragment),
            getResources().getString(R.string.hello_blank_fragment),
            getResources().getString(R.string.hello_blank_fragment),
            getResources().getString(R.string.hello_blank_fragment),
            getResources().getString(R.string.hello_blank_fragment),
            getResources().getString(R.string.hello_blank_fragment)
    };
}
Mattia Maestrini
  • 32,270
  • 15
  • 87
  • 94