Search code examples
javaandroidnullpointerexceptionarraysruntimeexception

Android string-array crash


I have a very weird problem with my Android application. Every time I add a string-array to my strings.xml (or any other file in res/values/), my program crashes on start up. I know, with absolute certainty, that this is what is crashing it (since whenever I remove it, it works fine). Anyway, here is the XML code that causes the crash:

<string-array name="main_list">
        <item>Collections</item>
        <item>Requests</item>
        <item>Forums</item>
</string-array>

Is there something wrong with the formatting of it at all? It is inside the "resources" tags and everything. Here is the full XML file:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- Activity Names -->
    <string name="app_name">MyFavs</string>
    <!-- Login Vars -->
    <string name="username_login_field">Username</string>
    <string name="password_login_field">Password</string>
    <string name="login_btn">Login!</string>
    <string name="remember_login_tag">Save Login Info</string>
    <string name="no_username_password">You must supply a username and password</string>
    <string name="sign_in_cancelled">Sign-in cancelled</string>
    <string name="sign_in_progress">Signing in...</string>
    <string name="invalid_username_password_alert_title">Error</string>
    <string name="invalid_username_password_alert_btn">Ok</string>
    <string name="menu_forgotten_password">Forgot Password?</string>
    <!-- ForgotPasswd Vars -->
    <string name="forgotten_pass_field">Email</string>
    <string name="forgotten_pass_btn">Recover Account</string>
    <string name="forgot_passwd_progress">Recovering account...</string>
    <string name="invalid_forgot_passwd_title">Recover Account</string>
    <string name="invalid_forgot_passwd_btn">Ok</string>
    <string name="no_email">You must supply a valid email</string>
    <string name="recover_cancelled">Recover cancelled</string>
    <string name="menu_home">Home</string>
    <string-array name="main_list">
        <item>Collections</item>
        <item>Requests</item>
        <item>Forums</item>
    </string-array>
</resources>

Again, removing the "string-array" section at the bottom makes the program work, but adding it causes a crash.

Here is the logcat crash log (although it doesn't help much):

W/dalvikvm(  849): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
E/AndroidRuntime(  849): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime(  849): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.myfavs.droid/org.myfavs.droid.login}: java.lang.NullPointerException
E/AndroidRuntime(  849):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
E/AndroidRuntime(  849):        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
E/AndroidRuntime(  849):        at android.app.ActivityThread.access$2200(ActivityThread.java:119)
E/AndroidRuntime(  849):        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
E/AndroidRuntime(  849):        at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(  849):        at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(  849):        at android.app.ActivityThread.main(ActivityThread.java:4363)
E/AndroidRuntime(  849):        at java.lang.reflect.Method.invokeNative(NativeMethod)
E/AndroidRuntime(  849):        at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(  849):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
E/AndroidRuntime(  849):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
E/AndroidRuntime(  849):        at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(  849): Caused by: java.lang.NullPointerException
E/AndroidRuntime(  849):        at org.myfavs.droid.login.onCreate(login.java:32)
E/AndroidRuntime(  849):        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime(  849):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
E/AndroidRuntime(  849):        ... 11 more
I/Process (   51): Sending signal. PID: 849 SIG: 3

If anyone has any ideas, please share them. I have been working on this for quite a long time, and I'm sure it is a stupid mistake but I just need another pair of eyes. I would be willing to try any possible solution.

EDIT:

I removed every reference to the string-array resource and simply including the resource causes the crash. I don't reference it anywhere, so its not that that is causing the problem.

As requested, here is my onCreate for "login", although I haven't touched that code in a long time and it has always worked:

public void onCreate(Bundle savedInstanceState)
    {
    Log.d("Login","onCreate called");
        super.onCreate(savedInstanceState);
    Interactor.create(this);
        setContentView(R.layout.login);
    ((EditText)findViewById(R.id.username)).setText(Interactor.getDB().getSetting("username"));
        EditText pwd = (EditText)findViewById(R.id.password);
    pwd.setText(Interactor.getDB().getSetting("password"));
    if (Interactor.getDB().getSetting("save_login").equals("yes"))
        ((CheckBox)findViewById(R.id.remember_login)).setChecked(true);
        // Treat "Send" soft-button on keyboard as a button click
        pwd.setOnEditorActionListener(new android.widget.TextView.OnEditorActionListener()
                {
                    public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
                    {
                        if (actionId == EditorInfo.IME_ACTION_SEND)
                            ((Button)findViewById(R.id.login_btn)).performClick();
                        return true;
                    }
                });
    }

The exception is being thrown just by including the resource inside res/values even without ever referencing it. That's why its so weird to me...

EDIT of the EDIT:

Jon Skeet might be on to something. While including the string-array resource, and commenting out line 32 of login's onCreate (as I said what line that is in the comments above), the application works again. So adding a string-array breaks that line somehow? Any help in why adding the string-array resource would break that, but it works 100% without adding the resource?


Solution

  • Surely there is no problem with your string array.Problem is in the code where you are accessing the string

    try like this

    Resources res = getResources();
    String[] list = res.getStringArray(R.array.main_list);