我要做的就是引用存储在我的xml文件中的字符串,然后在数组中使用相同的String。这发生在非Activity类MyDataProvider中,如下所示。问题是,当我在屏幕上显示字符串时,我的参考字符串只显示单词“null”。据我所知,我已正确引用它们,但随后它们不会返回null。我确定这是一个愚蠢的错误,但我仍然是Android的新手,非常感谢帮助。
MyDataProvider:
package com.gmd.referenceapplication;
import android.content.Context;
import java.util.HashMap;
import java.util.Map;
public class MyDataProvider {
Context context;
String test; //string variable
//constructor for class MyDataProvider
public MyDataProvider(Context context1){
context=context1;
test = context.getString(R.string.app_name);//tester string is assigned
putInCommonMap();
putInElectromagneticMap();
}
//strings for sub and superscript
//data for common constants
ListViewItem constant_common_data[] = new ListViewItem[]
{
new ListViewItem(R.drawable.star, "Atomic Mass Constant " +test, "1.660 539 040 e-27", "kg", "0.000 000 020 e-27"),//test string is used, shows up as word "null" on display
new ListViewItem(R.drawable.star, "Avogadro Constant", "6.022 140 857 e23"," mol^-1", "0.000 000 074 x e23"),
new ListViewItem(R.drawable.star, "Boltzmann Constant", "1.380 648 52 e-23", "K^-1", "0.000 000 79 e-23"),
new ListViewItem(R.drawable.star, "Conductance Quantum", "7.748 091 7310 e-5", "s","0.000 000 0018 e-5"),
new ListViewItem(R.drawable.star, "Electric Constant", "8.854 187 817... e-12", "F m^-1", "(exact)"),
new ListViewItem(R.drawable.star, "Electron mass", "9.109 383 56 e-31", "kg", "0.000 000 11 e-31"),
new ListViewItem(R.drawable.star, "Electron volt", "1.602 176 6208 e-19"," J", "0.000 000 0098 e-19"),
new ListViewItem(R.drawable.star, "Elementary charge ", "1.602 176 6208 e-19", "C", "0.000 000 0098 e-19"),
new ListViewItem(R.drawable.star, "Faraday constant ", "96 485.332 89 e-5", "C mol^-1", "0.000 59"),
new ListViewItem(R.drawable.star, "Fine-structure constant ", "7.297 352 5664 e-3", " ", "0.000 000 0017 e-3"),
new ListViewItem(R.drawable.star, "Inverse fine-structure constant", "137.035 999 139", " ", "0.000 000 031"),
new ListViewItem(R.drawable.star, "Magnetic constant", "4pi e-7 = 12.566 370 614... e-7"," N A^-2", "(exact)"),
new ListViewItem(R.drawable.star, "Magnetic Flux Quantum", "2.067 833 831 e-15", "Wb", "0.000 000 013 e-15"),
new ListViewItem(R.drawable.star, "Molar Gas constant", "8.314 4598", "J mol^-1 K^-1", "0.000 0048"),
new ListViewItem(R.drawable.star, "Newtonian constant of gravitation ", "6.674 08 e-11", "m^3 kg^-1 s^-2", "0.000 31 e-11"),
new ListViewItem(R.drawable.star, "Planck constant", "6.626 070 040 e-34", "J s", "0.000 000 081 e-34"),
new ListViewItem(R.drawable.star, "Planck constant over 2 pi", "1.054 571 800 e-34"," J s", "0.000 000 013 e-34"),
new ListViewItem(R.drawable.star, "Proton Mass", "1.672 621 898 e-27", "kg", "0.000 000 021 e-27"),
new ListViewItem(R.drawable.star, "Proton-Electron Mass Ratio", "1836.152 673 89", " ", "0.000 000 17"),
new ListViewItem(R.drawable.star, "Rydberg constant", "10 973 731.568 508", "m^-1", "0.000 065"),
new ListViewItem(R.drawable.star, "Speed of Light in Vacuum", "299 792 458", "m s^-1", "(exact)"),
new ListViewItem(R.drawable.star, "Stefan-Boltzmann constant", "5.670 367 e-8", "Wm^-2 K^-4", "0.000 013 e-8"),
new ListViewItem(R.drawable.star, "Bohr Radius", "0.529 177 210 67 e-10", "m", "0.000 000 000 12 e-10"),
new ListViewItem(R.drawable.star, "Bohr Magneton ", "927.400 9994 e-26"," J T^-1", "0.000 0057 e-26"),
new ListViewItem(R.drawable.star, "Josephson constant", "483 597.8525 e9", "Hz V^-1", "0.0030 e9"),
new ListViewItem(R.drawable.star, "Von Klitzing constant", "25 812.807 4555", "Ohm", "0.000 0059"),
new ListViewItem(R.drawable.star, "Unified Atomic Mass Unit", "1.660 539 040 e-27"+ test , "kg", "0.000 000 020 e-27")
};
HashMap<String,ListViewItem> commonMap = new HashMap<String, ListViewItem>();
//data entry for electromagnetic constants
ListViewItem constant_electromagnetic_data[] = new ListViewItem[]
{
new ListViewItem(R.drawable.star, "Bohr Magneton ", "927.400 9994 e-26"," J T^-1", "0.000 0057 e-26"),
new ListViewItem(R.drawable.star, "Bohr magneton in (eV)/T", "5.788 381 8012 e-5", "(eV)/T", "0.000 000 0026 e-5"),
new ListViewItem(R.drawable.star, "Bohr magneton in Hz/T", "13.996 245 042 e9"," Hz T^-1", "0.000 000 086 e9"),
new ListViewItem(R.drawable.star, "Bohr magneton in inverse meters per tesla", "46.686 448 14", "m^-1 T^-1", "0.000 000 29"),
new ListViewItem(R.drawable.star, "Bohr magneton in K/T", "0.671 714 05", "K T^-1","0.000 000 39"),
new ListViewItem(R.drawable.star, "Conductance Quantum", "7.748 091 7310 e-5", "s","0.000 000 0018 e-5"),
new ListViewItem(R.drawable.star, "Elementary charge ", "1.602 176 6208 e-19", "C", "0.000 000 0098 e-19"),
new ListViewItem(R.drawable.star, "Electron volt", "1.602 176 6208 e-19"," J", "0.000 000 0098 e-19"),
new ListViewItem(R.drawable.star, "Elementary charge ", "1.602 176 6208 e-19", "C", "0.000 000 0098 e-19"),
new ListViewItem(R.drawable.star, "elementary charge over h", "2.417 989 262 e14", "A J^-1", "0.000 000 015 e14"),
new ListViewItem(R.drawable.star, "inverse of conductance quantum", "12 906.403 7278", "Ohms", "0.000 0029"),
new ListViewItem(R.drawable.star, "Josephson constant", "483 597.8525 e9", "Hz V^-1", "0.0030 e9"),
new ListViewItem(R.drawable.star, "Magnetic Flux Quantum", "2.067 833 831 e-15", "Wb", "0.000 000 013 e-15"),
new ListViewItem(R.drawable.star, "nuclear magneton", "5.050 783 699 e-27", "J T^-1", "0.000 000 031 e-27"),
new ListViewItem(R.drawable.star, "nuclear magneton in (eV)/T ", "3.152 451 2550 e-8", "(eV)/T", "0.000 000 0015 e-8"),
new ListViewItem(R.drawable.star, "nuclear magneton in inverse meters per tesla", "2.542 623 432 e-2", "m^-1 T^-1", "0.000 000 016 e-2"),
new ListViewItem(R.drawable.star, "nuclear magneton in K/T", "3.658 2690 e-4","K T^-1", "0.000 0021"),
new ListViewItem(R.drawable.star, "nuclear magneton in MHz/T ", "7.622 593 285", "MHz T^-1", "0.000 000 047"),
new ListViewItem(R.drawable.star, "Von Klitzing constant", "25 812.807 4555", "Ohm", "0.000 0059")
};
HashMap<String,ListViewItem> electromagneticMap = new HashMap<String, ListViewItem>();
//data entry for Atomic and nuclear constants :(
ListViewItem constant_atomic_data[] = new ListViewItem[]
{
new ListViewItem(R.drawable.star, "alpha particle mass", "6.644 657 230 e-27"," kg", "0.000 000 082 e-27"),
new ListViewItem(R.drawable.star, "alpha particle mass energy equivalent", "5.971 920 097 e-10", "J", "0.000 000 073 e-10"),
new ListViewItem(R.drawable.star, "alpha particle mass energy equivalent in MeV ", "3727.379 378"," MeV", "0.000 023"),
new ListViewItem(R.drawable.star, "alpha particle mass in u", "4.001 506 179 127", "u", "0.000 000 000 063"),
new ListViewItem(R.drawable.star, "alpha particle molar mass", "4.001 506 179 127 e-3", "mol^-1","0.000 000 000 063 e-3"),
new ListViewItem(R.drawable.star, "alpha particle-electron mass ratio", "7294.299 541 36", "(none)","0.000 000 24"),
new ListViewItem(R.drawable.star, "alpha particle-proton mass ratio ", "3.972 599 689 07", "(none)", "0.000 000 000 36"),
new ListViewItem(R.drawable.star, "Bohr Radius", "0.529 177 210 67 e-10", "m", "0.000 000 000 12 e-10"),
new ListViewItem(R.drawable.star, "classical electron radius", "2.817 940 3227 e-15","m", "0.000 000 0019 e-15"),
new ListViewItem(R.drawable.star, "Compton wavelength", "2.426 310 2367 e-12", "m", "0.000 000 0011 e-12"),
new ListViewItem(R.drawable.star, "Compton wavelength over 2 pi", "386.159 267 64 e-15", "m", "0.000 000 18 e-15"),
new ListViewItem(R.drawable.star, "deuteron g factor ", "0.857 438 2311", "(none)", "0.000 000 0048"),
new ListViewItem(R.drawable.star, "deuteron magnetic moment ", "0.433 073 5040 e-26", "J T^-1", "0.000 000 0036 e-26"),
new ListViewItem(R.drawable.star, "deuteron magnetic moment to Bohr magneton ratio ", "0.466 975 4554 e-3", "(none)", "0.000 000 0026 e-3"),
new ListViewItem(R.drawable.star, "deuteron magnetic moment to nuclear magneton ratio", "0.857 438 2311", "(none)", "0.000 000 0048"),
new ListViewItem(R.drawable.star, "deuteron mass ", "3.343 583 719 e-27", "kg", "0.000 000 041 e-27"),
new ListViewItem(R.drawable.star, "deuteron mass energy equivalent ", "3.005 063 183 e-10", "J", "0.000 000 037 e-10"),
new ListViewItem(R.drawable.star, "deuteron mass energy equivalent in MeV", " 1875.612 928","MeV", "0.000 012"),
new ListViewItem(R.drawable.star, "deuteron mass in u", "2.013 553 212 745", "u", "0.000 000 000 040"),
new ListViewItem(R.drawable.star, "deuteron molar mass", "2.013 553 212 745 e-3", "kg mol ^-1", "0.000 000 000 040 e-3"),
//resume data entry here
new ListViewItem(R.drawable.star, "deuteron rms charge radius", "2.1413 e-15","m", "0.0025 e-15"),
new ListViewItem(R.drawable.star, "deuteron-electron magnetic moment ratio", "-4.664 345 535 e-4", "(none)", "0.000 000 026 e-4"),
new ListViewItem(R.drawable.star, "deuteron-electron mass ratio", "3670.482 967 85","(none)", "0.000 000 13"),
new ListViewItem(R.drawable.star, "deuteron-neutron magnetic moment ratio", "-0.448 206 52", "(none)", "0.000 000 11"),
new ListViewItem(R.drawable.star, "deuteron-proton magnetic moment ratio", "0.307 012 2077", "(none)","0.000 000 0015"),
new ListViewItem(R.drawable.star, "deuteron-proton mass ratio", "1.999 007 500 87", "(none)","0.000 000 000 19"),
new ListViewItem(R.drawable.star, "electron charge to mass quotient", "-1.758 820 024 e11", "C kg^-1", "0.000 000 011 e11"),
new ListViewItem(R.drawable.star, "electron g factor ", "-2.002 319 304 361 82","(none)", "0.000 000 000 000 52"),
new ListViewItem(R.drawable.star, "Elementary charge ", "1.602 176 6208 e-19", "C", "0.000 000 0098 e-19"),
new ListViewItem(R.drawable.star, "elementary charge over h", "2.417 989 262 e14", "A J^-1", "0.000 000 015 e14"),
new ListViewItem(R.drawable.star, "inverse of conductance quantum", "12 906.403 7278", "Ohms", "0.000 0029"),
new ListViewItem(R.drawable.star, "Josephson constant", "483 597.8525 e9", "Hz V^-1", "0.0030 e9"),
new ListViewItem(R.drawable.star, "Magnetic Flux Quantum", "2.067 833 831 e-15", "Wb", "0.000 000 013 e-15"),
new ListViewItem(R.drawable.star, "nuclear magneton", "5.050 783 699 e-27", "J T^-1", "0.000 000 031 e-27"),
new ListViewItem(R.drawable.star, "nuclear magneton in (eV)/T ", "3.152 451 2550 e-8", "(eV)/T", "0.000 000 0015 e-8"),
new ListViewItem(R.drawable.star, "nuclear magneton in inverse meters per tesla", "2.542 623 432 e-2", "m^-1 T^-1", "0.000 000 016 e-2"),
new ListViewItem(R.drawable.star, "nuclear magneton in K/T", "3.658 2690 e-4","K T^-1", "0.000 0021"),
new ListViewItem(R.drawable.star, "nuclear magneton in MHz/T ", "7.622 593 285", "MHz T^-1", "0.000 000 047"),
new ListViewItem(R.drawable.star, "Von Klitzing constant", "25 812.807 4555", "Ohm", "0.000 0059")
};
HashMap<String,ListViewItem> AtomicMap = new HashMap<String, ListViewItem>();
//enters all data in common array into the common HashMap
public void putInCommonMap(){
for(ListViewItem i : constant_common_data) commonMap.put(i.getKey(),i);
}
public void putInElectromagneticMap(){
for(ListViewItem i : constant_electromagnetic_data) electromagneticMap.put(i.getKey(),i);
}
//returns common hashmap
public HashMap getCommonMap(){
return commonMap;
}
//returns electromagnetic HashMap
public HashMap getElectromagneticMap(){
return electromagneticMap;
}
}
字符串xml:
<resources>
<string name="app_name">Reference Application</string>
<string name="search_hint">Search constants, rules, and properties of water </string>
<string name="to_2"><![CDATA[ m<sup><small>-2]></small></sup> </string>
<string name="E_mc2">E=mc<sup><small>2</small></sup></string>
<string name="welcome">Welcome to <b>Android</b>!</string>
</resources>
全部谢谢!
答案 0 :(得分:2)
在构造函数中初始化你的项目,如下所示。
public class MyDataProvider {
Context context;
String test; //string variable
ListViewItem constant_common_data[];
//constructor for class MyDataProvider
public MyDataProvider(Context context1){
context=context1;
test = context.getString(R.string.app_name);//tester string is assigned
init_constant_common_data();
putInCommonMap();
putInElectromagneticMap();
}
//strings for sub and superscript
//data for common constants
private void init_constant_common_data(){
constant_common_data = new ListViewItem[]
{
new ListViewItem(R.drawable.star, "Atomic Mass Constant " +test, "1.660 539 040 e-27", "kg", "0.000 000 020 e-27"),//test string is used, shows up as word "null" on display
new ListViewItem(R.drawable.star, "Avogadro Constant", "6.022 140 857 e23"," mol^-1", "0.000 000 074 x e23"),
new ListViewItem(R.drawable.star, "Boltzmann Constant", "1.380 648 52 e-23", "K^-1", "0.000 000 79 e-23"),
new ListViewItem(R.drawable.star, "Conductance Quantum", "7.748 091 7310 e-5", "s","0.000 000 0018 e-5"),
new ListViewItem(R.....
....
..
..
}