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(Instrumentati开发者_StackOverflowon.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?
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);
Well, this is a 5 years old post, but I just had the exact same problem (Android Studio 2.1.3). For me, the first string-array worked perfectly, but whenever I tried to add a second one in the same file (strings.xml) my app crashed. So I was making tests and I noticed that if you make changes to your String resources, the changes appear immediately on the design, but if you run the app it just remain the same. I thought it has to do with the cache, so I just CLOSE AND OPEN ANDDROID STUDIO, and surprise, IT DID WORK, at least for me. If you want something quicker than close and open it manually, here is the best solution: In your project click File->Invalidates Cache/Restart->Just restart and it's done.
Pd. I was working whit Spinners when I found this problem, here is some of my code:
"strings.xml":
<resources>
<string name="app_name">Mascota Mia</string>
<string name="registrar_mascota">Registrar Mascota</string>
<string name="agendar_consulta">Agendar Consulta</string>
<string-array name="raza_array">
<item>Perro</item>
<item>Gato</item>
<item>Cocodrilo</item>
<item>Armadillo</item>
<item>Ganso</item>
</string-array>
<string-array name="pasatiempo_array">
<item>Correr</item>
<item>Nadar</item>
<item>Dormir</item>
<item>Morder el hueso</item>
<item>Perseguir al gato</item>
</string-array>
</resources>
"RegistrarMascota.java" (only the onCreate method, this is where I use the string-arrays in my Spinners):
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registrar_mascota);
//Spinner para la raza
Spinner spinneRaza = (Spinner) findViewById(R.id.raza);
ArrayAdapter<CharSequence>adapteRaz = ArrayAdapter.createFromResource(this,
R.array.raza_array, android.R.layout.simple_spinner_item);
adapteRaz.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinneRaza.setAdapter(adapteRaz);
spinneRaza.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Toast.makeText(getBaseContext(), parent.getItemAtPosition(pos)+" seleccionado", Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
//Spinner para los pasatiempos
Spinner spinnerPas = (Spinner) findViewById(R.id.pasatiempo);
ArrayAdapter<CharSequence> adapterPas = ArrayAdapter.createFromResource(this,
R.array.pasatiempo_array, android.R.layout.simple_spinner_item);
adapterPas.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerPas.setAdapter(adapterPas);
spinnerPas.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Toast.makeText(getBaseContext(), parent.getItemAtPosition(pos)+" seleccionado", Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
I had the same problem, it was a bad backup. just clear the data of your app (uninstalling won't work)
Same problem, 2022 Android 11.
I was trying to make a shortcuts.xml file and needed a string array. The shortcuts.xml is under res/values/xml.
The app crashed if I put a <string-array> reference in the strings.xml under res/values or made an arrays.xml in res/values and put it there.
I put the arrays.xml under res/values/xml and it works.
精彩评论