我第一次使用 Expandable ListView,我在这里找到了一个例子:
http://android-adda.blogspot.ro/2011/06/custom-expandable-listview.html
我经历了它,根据我的需要对其进行了调整,但是应用适配器时应用程序会崩溃。我复制粘贴了整个示例,甚至使用了给定的字符串数组,我认为我可能在那方面做错了。
不过,错误是一样的。
下面是我的活动的代码 + 其中包含的自定义适配器类。出于显而易见的原因,我已经将诸如 onClickListeners 之类的不相关的内容移至其他元素。
///////////////////////////////////////////////
public String[] parentArray = {
"India",
"Australia",
"England",
"South Africa"
};
public String[][] childArray = {
{
"Sachin Tendulkar",
"Raina",
"Dhoni",
"Yuvi"
},
{
"Ponting",
"Adam Gilchrist",
"Michael Clarke"
},
{
"Andrew Strauss",
"kevin Peterson",
"Nasser Hussain"
},
{
"Graeme Smith",
"AB de villiers",
"Jacques Kallis"
}
};
///////////////////////////////////////////////
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.viewschedule);
getViews();
setSpinnerAdapter();
listview.setAdapter(new ExpAdapter(this));
}
public class ExpAdapter extends BaseExpandableListAdapter {
private Context myContext;
public ExpAdapter(Context context) {
myContext = context;
}
public Object getChild(int groupPosition, int childPosition) {
return null;
}
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.childrow, null);
}
TextView tvPlayerName = (TextView) convertView.findViewById(R.id.tvPlayerName);
tvPlayerName.setText(childArray[groupPosition][childPosition]);
return convertView;
}
public int getChildrenCount(int groupPosition) {
return childArray[groupPosition].length;
}
public Object getGroup(int groupPosition) {
return null;
}
public int getGroupCount() {
return parentArray.length;
}
public long getGroupId(int groupPosition) {
return 0;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.parentrow, null);
}
TextView tvGroupName = (TextView) convertView.findViewById(R.id.tvGroupName);
tvGroupName.setText(parentArray[groupPosition]);
return convertView;
}
public boolean hasStableIds() {
return false;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
}
在此下方,您的日志主要显示“找不到类”。
07-19 12:34:09.760: E/AndroidRuntime(2889): at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
07-19 12:34:09.760: E/AndroidRuntime(2889): at android.view.View.layout(View.java:11180)
07-19 12:34:09.760: E/AndroidRuntime(2889): at android.view.ViewGroup.layout(ViewGroup.java:4203)
07-19 12:34:09.760: E/AndroidRuntime(2889): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
07-19 12:34:09.760: E/AndroidRuntime(2889): at android.view.View.layout(View.java:11180)
07-19 12:34:09.760: E/AndroidRuntime(2889): at android.view.ViewGroup.layout(ViewGroup.java:4203)
07-19 12:34:09.760: E/AndroidRuntime(2889): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1468)
07-19 12:34:09.760: E/AndroidRuntime(2889): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2418)
07-19 12:34:09.760: E/AndroidRuntime(2889): at android.os.Handler.dispatchMessage(Handler.java:99)
07-19 12:34:09.760: E/AndroidRuntime(2889): at android.os.Looper.loop(Looper.java:137)
07-19 12:34:09.760: E/AndroidRuntime(2889): at android.app.ActivityThread.main(ActivityThread.java:4340)
07-19 12:34:09.760: E/AndroidRuntime(2889): at java.lang.reflect.Method.invokeNative(Native Method)
07-19 12:34:09.760: E/AndroidRuntime(2889): at java.lang.reflect.Method.invoke(Method.java:511)
07-19 12:34:09.760: E/AndroidRuntime(2889): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-19 12:34:09.760: E/AndroidRuntime(2889): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-19 12:34:09.760: E/AndroidRuntime(2889): at dalvik.system.NativeStart.main(Native Method)
07-19 12:34:09.760: E/AndroidRuntime(2889): Caused by: java.lang.ClassNotFoundException: android.view.linearlayout
07-19 12:34:09.760: E/AndroidRuntime(2889): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
07-19 12:34:09.760: E/AndroidRuntime(2889): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
07-19 12:34:09.760: E/AndroidRuntime(2889): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
07-19 12:34:09.760: E/AndroidRuntime(2889): at android.view.LayoutInflater.createView(LayoutInflater.java:552)
07-19 12:34:09.760: E/AndroidRuntime(2889): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:636)
07-19 12:34:09.760: E/AndroidRuntime(2889): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
07-19 12:34:09.760: E/AndroidRuntime(2889): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
07-19 12:34:09.760: E/AndroidRuntime(2889): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
我的 getViews() 方法:
private void getViews(){
font = Typeface.createFromAsset(getAssets(), "arialnb.ttf");
groupInput = (EditText) findViewById(R.id.editText1);
groupInput.setTextColor(getResources().getColor(R.color.lightGrey));
groupInput.setText("Grupa: (ex: 10201b)");
titleText = (TextView) findViewById(R.id.textView1);
titleText.setTypeface(font);
editDefault = (Button) findViewById(R.id.button2);
homeImage = (ImageView) findViewById(R.id.imageView2);
daySpinner = (Spinner) findViewById(R.id.spinner1);
listview = (ExpandableListView) findViewById(R.id.expandableListView1);
listview.setVerticalFadingEdgeEnabled(false);
}
整个 ViewSchedule.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/aceLightBlue"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:background="@color/aceDarkBlue" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/orarbuton" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:layout_toRightOf="@+id/imageView1"
android:text="@string/aceOrar"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/homebuton" />
</RelativeLayout>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="@string/editDefault" />
<RelativeLayout
android:id="@+id/relativeLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/relativeLayout1"
android:layout_marginTop="14dp" >
<LinearLayout
android:layout_width="0px"
android:layout_height="0px"
android:focusable="true"
android:focusableInTouchMode="true" >
</LinearLayout>
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/spinner1"
android:ems="10" />
</RelativeLayout>
<ExpandableListView
android:id="@+id/expandableListView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_below="@+id/relativeLayout2" >
</ExpandableListView>
</RelativeLayout>
</LinearLayout>
任何我目前缺少的任何帮助将不胜感激......