更改字符串后应用程序崩溃

时间:2013-08-05 07:47:03

标签: android string

这些是示例中的主要字符串

    String[] strings = {"CoderzHeaven","Google",
        "Microsoft", "Apple", "Yahoo","Samsung"};

    String[] subs = {"Heaven of all working codes ","Google sub",
        "Microsoft sub", "Apple sub", "Yahoo sub","Samsung sub"};

       int arr_images[] = { R.drawable.ds,
           R.drawable.ds, R.drawable.ds,
           R.drawable.ds, R.drawable.ds, R.drawable.ds};

我尝试做的是将java字符串更改为值文件夹中的string.xml

    Resources res = getResources();
    String[] strings = res.getStringArray(R.array.SSolo);

...

    @Override
public void onCreate(Bundle savedInstanceState) {    
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

这里是微调......

        Spinner mySpinner = (Spinner)findViewById(R.id.spinner);
        mySpinner.setAdapter(new MyAdapter(Main.this, R.layout.row, strings));

这是代码的其余部分

    public class MyAdapter extends ArrayAdapter<String>{

    public MyAdapter(Context context, int textViewResourceId,   String[] objects) {
        super(context, textViewResourceId, objects);
    }

    @Override
    public View getDropDownView(int position, View convertView,ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }

    public View getCustomView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater=getLayoutInflater();
        View row=inflater.inflate(R.layout.row, parent, false);
        TextView label=(TextView)row.findViewById(R.id.company);
        label.setText(strings[position]);
        TextView sub=(TextView)row.findViewById(R.id.sub);
        sub.setText(subs[position]);
        ImageView icon=(ImageView)row.findViewById(R.id.image);
                    icon.setImageResource(arr_images[position]);

        return row;
    }
当我尝试替换上面的字符串时,

应用崩溃

并且它给了我这个错误:

  

/ AndroidRuntime(882):java.lang.RuntimeException:无法执行   实例化活动   ComponentInfo {custom.spinner.myspinner / custom.spinner.myspinner.Main}:   显示java.lang.NullPointerException

这是main.xml ..

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />

<Spinner
    android:id="@+id/spinner"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:drawSelectorOnTop="true"
    android:prompt="@string/prompt" />

这里是row.xml

<?xml version="1.0" encoding="utf-8"?>

<ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/company"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dip"
    android:layout_marginTop="2dip"
    android:layout_toRightOf="@+id/image"
    android:padding="3dip"
    android:text="CoderzHeaven"
    android:textColor="@drawable/red"
    android:textStyle="bold" />

<TextView
    android:id="@+id/sub"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/company"
    android:layout_marginLeft="5dip"
    android:layout_toRightOf="@+id/image"
    android:padding="2dip"
    android:text="Heaven of all working codes"
    android:textColor="@drawable/darkgrey" />

这是string.xml

<string name="app_name">CustomSpinnerDemo</string>
<string name="action_settings">Settings</string>

<string name="hello">CustomSpinner Demo from CoderzHeaven!</string>
<string name="prompt">  Select your Favourite  </string>
<drawable name="white">#ffffff</drawable>
<drawable name="black">#000000</drawable>
<drawable name="green">#347C2C</drawable>
<drawable name="pink">#FF00FF</drawable>
<drawable name="violet">#a020f0</drawable>
<drawable name="grey">#778899</drawable>
<drawable name="red">#C11B17</drawable>
<drawable name="yellow">#FFFF8C</drawable>
<drawable name="PowderBlue">#b0e0e6</drawable>
<drawable name="brown">#2F1700</drawable>
<drawable name="Hotpink">#7D2252</drawable>
<string name="select_Category">Select Category</string>
<drawable name="darkgrey">#606060</drawable>

<string-array name="SSolo">
    <item>CoderzHeaven</item>
    <item>Google</item>
    <item>Microsoft</item>
    <item>Apple</item>
    <item>Yahoo</item>        
    <item>Samsung</item>        
    </string-array>  

1 个答案:

答案 0 :(得分:0)

你还没有发布你的字符串数组的xml版本,但它看起来好像你可能没有 正确定义它。在任何情况下,您的数组适配器对于资源中定义的字符串数组肯定是错误的。这是一个有效的例子:

ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(this, R.array.maths_subject_area, R.layout.custom_spinner);

并在res / values文件夹中定义它(不直接在strings.xml中):

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="maths_subject_area">
        <item>item 1</item>
        <item>item 2</item>
        <item>item 3</item>
        <item>item 4</item>
        <item>item 5</item>        
    </string-array>
</resources>

如果你想使用它,这是我的custom_spinner:

<?xml version="1.0" encoding="utf-8"?>

<TextView  
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:textSize="@dimen/text_size"
android:gravity="left"           
android:padding="5dip"
/>