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

Android 更改字符串后应用程序崩溃,android,string,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"};

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

    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};
我试图做的是在value文件夹中将java字符串更改为string.xml

这是旋转器

        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;
    }
当我试图替换上面的字符串时,应用程序崩溃了

这给了我一个错误:

/AndroidRuntime882: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>  

您尚未发布字符串数组的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>
这是我的自定义微调器,如果您想使用它:

<?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"
/>

请发布空指针异常的其余部分。不存在空指针异常。请首先尝试将公共类MyAdapter extends ArrayAdapter{替换为公共类MyAdapter extends ArrayAdapter{。另外,将代码粘贴到使用setContentView的地方。在其中,你也应该包括微调器的布局。有人可以修复此崩溃或给我一些建议吗?我尝试了我所知道的每件事!!!应用程序仍在崩溃:你好…我确实从源代码发布了链接..这是地址…哦…我希望在xml中看到它,这是是你问题的一部分。我也只是发布了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>
<?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"
/>