-1

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

    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字符串更改为value文件夹中的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>  
4

1 回答 1

0

您尚未发布字符串数组的 xml 版本,但看起来您可能没有正确定义它。无论如何,您的数组适配器对于 resources 中定义的字符串数组肯定是错误的。这是一个工作示例:

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"
/>
于 2013-08-06T12:09:52.583 回答