Rúben Dias Rúben Dias - 6 months ago 16
Android Question

How can I get the text from Charsequence?

thats my Strings file:

<resources>
<string name="app_name">Name</string>
<string name="vp_locations">Locations</string>
<string name="vp_client_area">Client Area</string>
<string name="vp_menu">Menu</string>
</resources>


And I would like to get programmatically all these strings, I created an array to get all the string values that I need:

private CharSequence[] tbTitles = {
getResources().getText(R.string.vp_locations),
getResources().getText(R.string.vp_client_area),
getResources().getText(R.string.vp_menu),
};


Now I would like to know how can I get the values, when I do this:

textView.setText(tbTitles[0]);


The application crashes

I already to convert from charsequence to string, doing this:

textView.setText(tbTitle[0].toString());


But nothing seems to work. Can you guys tell me what am I doing wrong?

Log:


java.lang.NullPointerException
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2436)
at android.app.ActivityThread.access$800(ActivityThread.java:157)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1374)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5398)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:940)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
at
android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
at
com.example.rd.app.Interface.MainActivity.(MainActivity.java:36)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1215)
at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2436) 
at android.app.ActivityThread.access$800(ActivityThread.java:157) 
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1374) 
at android.os.Handler.dispatchMessage(Handler.java:110) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:5398) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:940) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 
at dalvik.system.NativeStart.main(Native Method)


Thank you.

Answer

You can't call getResources() from a field initializer like you are doing in

private CharSequence[] tbTitles = {
    getResources().getText(R.string.vp_locations),
    getResources().getText(R.string.vp_client_area),
    getResources().getText(R.string.vp_menu),
};

At this moment context is not setup yet. Its done after calling onCreate.

Change your code like this

private CharSequence[] tbTitles = new CharSequence[3];

and add this in onCreate

tbTitles = new CharSequence[]{
    getResources().getText(R.string.vp_locations),
    getResources().getText(R.string.vp_client_area),
    getResources().getText(R.string.vp_menu),
};

And finally use the array like this

textView.setText(tbTitle[0].toString());

Note: If you are using this array in only one method then you can simply move the declaration inside that method.