Android 字符串数组崩溃
我的 Android 应用程序有一个非常奇怪的问题。每次我将字符串数组添加到 strings.xml (或 res/values/ 中的任何其他文件)时,我的程序都会在启动时崩溃。我绝对确定这就是导致它崩溃的原因(因为每当我删除它时,它都能正常工作)。无论如何,这是导致崩溃的 XML 代码:
<string-array name="main_list">
<item>Collections</item>
<item>Requests</item>
<item>Forums</item>
</string-array>
它的格式是否有问题?它位于“资源”标签和所有内容内。下面是完整的 XML 文件:
<?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>
同样,删除底部的“字符串数组”部分可以使程序正常运行,但添加它会导致崩溃。
这是logcat崩溃日志(虽然没有多大帮助):
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
如果有人有任何想法,请分享。我已经为此工作了很长时间,我确信这是一个愚蠢的错误,但我只是需要另一双眼睛。我愿意尝试任何可能的解决方案。
编辑:
我删除了对字符串数组资源的所有引用,并且简单地包含该资源会导致崩溃。我没有在任何地方引用它,所以这并不是造成问题的原因。
根据要求,这是我的“登录”onCreate,尽管我很长一段时间没有触及该代码并且它始终有效:
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;
}
});
}
仅通过将资源包含在 res/values 中,即使没有引用它,也会引发异常。这就是为什么它对我来说如此奇怪......
编辑编辑:
乔恩·斯基特可能是在做某事。在包含字符串数组资源并注释掉登录 onCreate 的第 32 行(正如我在上面的注释中所说的那一行)时,应用程序再次运行。那么添加一个字符串数组会以某种方式破坏该行吗?有什么帮助可以解释为什么添加字符串数组资源会破坏这一点,但它在不添加资源的情况下可以 100% 工作吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当然,您的字符串数组没有问题。问题出在您访问字符串的代码中,
请像这样尝试
Surely there is no problem with your string array.Problem is in the code where you are accessing the string
try like this
好吧,这是一篇 5 年前的帖子,但我也遇到了完全相同的问题(Android Studio 2.1.3)。对我来说,第一个字符串数组工作得很好,但每当我尝试在同一个文件 (strings.xml) 中添加第二个字符串数组时,我的应用程序就会崩溃。因此,我正在进行测试,我注意到,如果您对字符串资源进行更改,更改会立即出现在设计上,但如果您运行应用程序,它就会保持不变。我认为这与缓存有关,所以我只是关闭并打开 ANDDROID STUDIO,令人惊讶的是,它确实有效,至少对我来说是这样。如果您想要比手动关闭和打开它更快的东西,这是最好的解决方案:在您的项目中单击文件->使缓存/重新启动无效->只需重新启动即可完成。
博士。当我发现这个问题时,我正在使用 Spinners,这是我的一些代码:
“strings.xml”:
“RegistrarMascota.java”(仅 onCreate 方法,这是我在 Spinners 中使用字符串数组的地方):
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":
"RegistrarMascota.java" (only the onCreate method, this is where I use the string-arrays in my Spinners):
我也遇到了同样的问题,备份不好。只需清除您的应用程序的数据即可(卸载不起作用)
I had the same problem, it was a bad backup. just clear the data of your app (uninstalling won't work)
同样的问题,2022 Android 11。
我试图制作一个 Shortcuts.xml 文件,需要一个字符串数组。 Shortcuts.xml 位于 res/values/xml 下。
如果我将放置在应用程序中,应用程序就会崩溃。在 res/values 下的 strings.xml 中引用,或者在 res/values 中创建一个 arrays.xml 并将其放在那里。
我将 arrays.xml 放在 res/values/xml 下并且它可以工作。
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.