我是 android 开发的新手,我试图使用字符串数组中的值创建一个幻灯片菜单。我想这样做是因为我想使用适当的图标自定义每一行,然后将幻灯片菜单的文本自定义为多语言。当我加载代码时,它显示正在读取所有值,但随后抛出异常,这是我的代码:(我的代码基于此示例教程:Android 中的 ListView 使用自定义 ListAdapter 和缓存视图)
我的代码:
public class Slide_Menu extends SherlockListFragment {
private String TAG = getClass().getSimpleName();;
private ListView listView;
private Context ctx;
private View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
Log.i(TAG,"Slide Menu is called");
this.view = inflater.inflate(R.layout.slide_menu, null);
this.ctx = getActivity();
return this.view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.i(TAG,"Slide Menu is on onActivityCreated");
try {
List menu_list= new ArrayList();
Resources res = getResources();
String[] menu_values = res.getStringArray(R.array.slide_menu_options);
for(int i=0; i<=menu_values.length;i++)
{
Log.i(TAG,"Adding the items on the Slide Menu: " + menu_values[i].toString());
menu_list.add(new SlideMenu_Contents(menu_values[i].toString(),"search"));
}
Log.i(TAG,"Slide Menu is on Loop");
this.listView = (ListView) this.view.findViewById(R.id.list);
this.listView.setAdapter( new SlideMenu_Adapter(this.ctx, R.layout.single_row_slidingmenu, menu_list ) );
}
catch (Exception e) {
Log.i(TAG,"Error on loading the string menu on Slide Menu.");
}
}
}
SlideMenu_Adapter、SlideMenu_Contents 等其他文件与示例相同,我根据需要更改了它们,例如 SlideMenu_Contents 只有文本和图像。
这是我的布局: single_row_slidingmenu.xml
<!-- ListRow Left sied Thumbnail image -->
<LinearLayout android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dip">
<ImageView
android:id="@+id/image_option"
android:layout_width="50dip"
android:layout_height="50dip"
android:contentDescription=""/>
</LinearLayout>
<!-- Title Of Song-->
<TextView
android:id="@+id/slidemenu_text_option"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/thumbnail"
android:layout_toRightOf="@+id/thumbnail"
android:text=""
android:textColor="#040404"
android:typeface="sans"
android:textSize="15sp"
android:textStyle="bold"/>
</RelativeLayout>
幻灯片菜单.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/slidemenu_list_selector">
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="@dimen/list_padding"
android:paddingRight="@dimen/list_padding"
android:divider="#1e1e1e"
android:dividerHeight="2dp"
android:textColor="@color/slidemenu_textcolour"
android:listSelector="@drawable/slidemenu_list_selector">
</ListView>
</LinearLayout>