问题填充的String []与字符串数组从strings.xml中数组、字符串、问题、xml

2023-09-08 09:44:46 作者:柠檬树下稚嫩少女i

编辑:哇...不知怎的,我替换了这个问题与另外一个我一直在问,很高兴有这样的回滚功能

这个具体问题涉及从我的 previous问题

 公共类的影响扩展了活动实施View.OnClickListener
{
    布尔[] impactsb =新的布尔[] {假的,假的,假的,假的,假的,假的,假的,假}  - 
    公共无效的onCreate(包savedInstanceState)
    {
    super.onCreate(savedInstanceState)
...

    字符串getImpacts()
    {
    的String [] impactsn =新的String [长度];
    资源为MyResources = this.getResources();
    impactsn = myResources.getStringArray(R.array.impacts);
    StringBuilder的impactss =新的StringBuilder();
    的for(int i = 0; I<长度;我++)
        {
        如果(impactsb [I])
            impactss.append(impactsn [我] +|);
        }
    如果(将String.valueOf(impactss)!=)
        impactss.insert(0,影响:);
    返回将String.valueOf(impactss);
    }
 

这些错误:

 的影响(ContextWrapper).getResources()线:80
Impacts.getImpacts()线:78
 
Labview如何把字符串数组转为字符串

的低于code中的最后一个支架:

  @覆盖
公共资源getResources()
{
    返回mBase.getResources();
}
 

和此行的code分别为:

  impactsn = getResources()getStringArray(R.array.impacts)。
 

这是我的strings.xml(的相应和部分反正)

 < XML版本=1.0编码=UTF-8&GT?;
<资源>
    <字符串数组名=影响>
        <项目> GasOilChemical污染物< /项目>
        <项目>幽灵捕捞< /项目>
        <项目>湿地破坏和LT; /项目>
        <项目>导航危害< /项目>
        <项目>贝类的损害和LT; /项目>
        <项目>废物污染环境< /项目>
        <项目>野生动物纠缠< /项目>
        <项目>其他< /项目>
    < /字符串数组>
< /资源>
 

我本来的第一项为:

 <项目>天然气/石油/化工污染物< /项目>
 

但固定的,希望至少会改变的错误,如果不解决这个问题。但没了,同样的错误。任何帮助,将大大AP preciated,即时通讯并不十分熟悉使用数组,尤其是获得资源的数组。

logcat的异常:

  06-05 23:02:30.792:ERROR / AndroidRuntime(3905):致命异常:主要
06-05 23:02:30.792:ERROR / AndroidRuntime(3905):显示java.lang.NullPointerException
06-05 23:02:30.792:ERROR / AndroidRuntime(3905):在android.content.ContextWrapper.getResources(ContextWrapper.java:80)
06-05 23:02:30.792:ERROR / AndroidRuntime(3905):在com.citsci.mardeb.Impacts.getImpacts(Impacts.java:79)
 

解决方案

我知道这是编码差,提前道歉。所以要解决这个问题,我感动

 的String [] impactsn;
impactsn = getResources()getStringArray(R.array.impacts)。
 

我的主要活动,并使其静态的。现在,我指的是它在我的影响类(MainActivity).impactsn []

我知道这是不是你应该来共享类的对象,但它似乎是最简单的方法为止。这是另一个原因,我后悔我的决定让我所有的标签单独活动。如果您对如何解决这一问题code有任何建议或意见而无需使用静态引用或原因,我所有的标签应该是一样的活动,请附和

EDIT: woah ... somehow i replaced this question with another one i was asking, glad there is this rollback feature

this specific question deals with the getter from my previous question

public class Impacts extends Activity implements View.OnClickListener
{
    boolean[] impactsb = new boolean[] {false, false, false, false, false, false, false, false}-
    public void onCreate (Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState)
...

    String getImpacts ()
    {
    String[] impactsn = new String[length];
    Resources myResources = this.getResources();
    impactsn = myResources.getStringArray(R.array.impacts);
    StringBuilder impactss = new StringBuilder();
    for (int i = 0; i < length; i ++)
        {
        if (impactsb[i])
            impactss.append(impactsn[i] + " | ");
        }
    if (String.valueOf(impactss) != "")
        impactss.insert(0, "Impacts: ");
    return String.valueOf(impactss);
    }

with these errors:

Impacts(ContextWrapper).getResources() line: 80
Impacts.getImpacts() line: 78

the final bracket of the below code:

@Override
public Resources getResources()
{
    return mBase.getResources();
}

and this line of code respectively:

impactsn = getResources().getStringArray(R.array.impacts);

here is my strings.xml (the relevent parts anyway)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="impacts">
        <item>GasOilChemical Pollutants</item>
        <item>Ghost Fishing</item>
        <item>Marsh Damage</item>
        <item>Navigational Hazard</item>
        <item>Shellfish Damage</item>
        <item>Waste Pollution</item>
        <item>Wildlife Entanglement</item>
        <item>Other</item>
    </string-array>
</resources>

i originally had the first item as:

<item>Gas/Oil/Chemical Pollutants</item>

but fixed that, hoping that would at least change the error if not fixing the problem. but nope, same error. any help would be vastly appreciated, im not terribly familiar with the use of array, especially getting resources for an array.

Logcat for exception:

06-05 23:02:30.792: ERROR/AndroidRuntime(3905): FATAL EXCEPTION: main
06-05 23:02:30.792: ERROR/AndroidRuntime(3905): java.lang.NullPointerException
06-05 23:02:30.792: ERROR/AndroidRuntime(3905): at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
06-05 23:02:30.792: ERROR/AndroidRuntime(3905): at com.citsci.mardeb.Impacts.getImpacts(Impacts.java:79)

解决方案

i know this is poor coding, apologies in advance. so to fix this problem, i moved

String[] impactsn;
impactsn = getResources().getStringArray(R.array.impacts);

to my main activity and made it static. now i refer to it within my Impacts class as (MainActivity).impactsn[]

i know this is not how you are supposed to share objects between classes, but it seems to be the easiest way so far. this is yet another reason why i am regretting my decision to make all of my tabs seperate activities. if you have any suggestions or advice on how to fix this code without using static references or on reasons why all of my tabs should be the same activities, please chime in.