[英]Android - referencing a string in strings.xml in a .java file
i have this code in MainActivity.java:我在 MainActivity.java 中有这段代码:
String passName = new String(getString(R.string.name));
where name is a string in strings.xml:其中 name 是 strings.xml 中的一个字符串:
<string name="name">My name</string>
it does not give me an error, but the app keeps crashing, how can i do this properly?它没有给我一个错误,但应用程序不断崩溃,我该如何正确执行此操作?
basically i want to save my name as a string variable in MainActivity.java, but store the actual text in strings.xml基本上我想将我的名字保存为 MainActivity.java 中的字符串变量,但将实际文本存储在 strings.xml 中
i originally had:我原来有:
String passName = new String("my name");
and i was able to successfully pass it to the second activity, but i want the text to be stored in strings.xml, not the.java file我能够成功地将它传递给第二个活动,但我希望将文本存储在 strings.xml 中,而不是 .java 文件中
edit: i have provided more of my code for context:编辑:我为上下文提供了更多代码:
public class MainActivity extends AppCompatActivity {
Button button;
//code that will eventually point to my name in strings.xml
String passName = new String("my name");
//String passName = getResources().getString(R.string.name);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//code that senses when the button is clicked
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//code that executes when the button is clicked
Toast.makeText(getApplicationContext(),"Moving to second activity...",Toast.LENGTH_SHORT).show();
//code that passes my name to the second activity
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("key", passName);
startActivity(intent);
}
});
}
}
as for a crash log, there doesn't seem to be one.至于崩溃日志,似乎没有。 when i launch the app it opens and then immediately closes again.当我启动应用程序时,它会打开,然后立即再次关闭。
the message it gives me is this:它给我的信息是这样的:
01/19 15:21:11: Launching 'app' on Pixel 4 XL API 30 (test).
Install successfully finished in 388 ms.
$ adb shell am start -n "my.name.n0000.lab_n0000/my.name.n0000.lab_n0000.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
edit: i have added the proper logcat dialogue:编辑:我添加了正确的 logcat 对话:
2023-01-19 15:32:41.587 7562-7562/my.name.n00000.lab_n00000 E/AndroidRuntime: FATAL EXCEPTION: main
Process: my.name.n00000.lab_n00000, PID: 7562
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{my.name.n00000.lab_n00000/my.name.n00000.lab_n00000.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3365)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.content.ContextWrapper.getResources(ContextWrapper.java:97)
at android.view.ContextThemeWrapper.getResourcesInternal(ContextThemeWrapper.java:134)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:128)
at androidx.appcompat.app.AppCompatActivity.getResources(AppCompatActivity.java:612)
at my.name.n00000.lab_n00000.MainActivity.<init>(MainActivity.java:17)
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45)
at android.app.Instrumentation.newActivity(Instrumentation.java:1253)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3353)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
2023-01-19 15:32:41.615 7562-7562/my.name.n00000.lab_n00000 I/Process: Sending signal. PID: 7562 SIG: 9
There is a lifecycle involved when creating activities.创建活动时涉及生命周期。 Some things should not be accessed before the onCreate
method is called.在调用onCreate
方法之前不应访问某些内容。 So you should be able to declare passName
prior and then inside onCreate
, you can initialise it.所以你应该能够先声明passName
然后在onCreate
里面,你可以初始化它。
public class MainActivity extends AppCompatActivity {
Button button;
String passName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//code that senses when the button is clicked
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//code that executes when the button is clicked
Toast.makeText(getApplicationContext(),"Moving to second activity...",Toast.LENGTH_SHORT).show();
//code that passes my name to the second activity
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("key", passName);
startActivity(intent);
}
});
passName = getResources().getString(R.string.name);
}
}
You can read more about the activity lifecycle here您可以在此处阅读有关活动生命周期的更多信息
And in particular in this paragraph , it talks about instantiating class scope variables.特别是在本段中,它讨论了实例化 class scope 变量。
For example, your implementation of onCreate() might bind data to lists, associate the activity with a ViewModel, and instantiate some class-scope variables.例如,您的 onCreate() 实现可能会将数据绑定到列表,将活动与 ViewModel 相关联,并实例化一些类范围变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.