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)
};
...
答案 0 :(得分:2)
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)
};
}