这些是示例中的主要字符串
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>