我已经尝试了两天,使用Fragment在可扩展列表中为组和子组充气我的自定义布局,但没有成功。我总是得到错误: java.lang.ClassCastException:android.widget.LinearLayout $ LayoutParams,但我不知道它意味着什么!!
这是我的片段类和适配器
public class ExpandableListFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section number.
*/
public static ExpandableListFragment newInstance(int sectionNumber) {
ExpandableListFragment fragment = new ExpandableListFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public ExpandableListFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_catalogo,
container, false);
ExpandableListView elv = (ExpandableListView) rootView.findViewById(R.id.lista_catalogo);
MiaExpandableListAdapter mela = new MiaExpandableListAdapter(getActivity());
mela.loadJSONFromAsset();
elv.setAdapter(mela);
return rootView;
}
public class MiaExpandableListAdapter extends BaseExpandableListAdapter {
private String[] groups ={"sedie", "tavoli" , "divani" , "letti" , "lampade"};
private String[][] children;
private Context ctx;
private LayoutInflater inflater;
public MiaExpandableListAdapter(Context ct)
{
ctx =ct;
inflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public String loadJSONFromAsset() {
String json = null;
try {
InputStream is = ExpandableListFragment.this.getActivity().getAssets().open("prova.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
//Log.d("JSON", json);
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
children = new String[5][2];
try{
JSONObject jsonResponse = new JSONObject(json);
JSONArray nome_oggetto = jsonResponse.getJSONArray("sedie");
for (int i=0; i<nome_oggetto.length(); i++) {
JSONObject prodotto = nome_oggetto.getJSONObject(i);
children[0][i] = prodotto.getString("name");
//Log.d("JSONSEDIE", children[0][i]);
}
}catch(JSONException ex){ex.printStackTrace();}
try{
JSONObject jsonResponse = new JSONObject(json);
JSONArray nome_oggetto = jsonResponse.getJSONArray("tavoli");
for (int i=0; i<nome_oggetto.length(); i++) {
JSONObject prodotto = nome_oggetto.getJSONObject(i);
children[1][i] = prodotto.getString("name");
}
}catch(JSONException ex){ex.printStackTrace();}
try{
JSONObject jsonResponse = new JSONObject(json);
JSONArray nome_oggetto = jsonResponse.getJSONArray("divani");
for (int i=0; i<nome_oggetto.length(); i++) {
JSONObject prodotto = nome_oggetto.getJSONObject(i);
children[2][i] = prodotto.getString("name");
}
}catch(JSONException ex){ex.printStackTrace();}
try{
JSONObject jsonResponse = new JSONObject(json);
JSONArray nome_oggetto = jsonResponse.getJSONArray("letti");
for (int i=0; i<nome_oggetto.length(); i++) {
JSONObject prodotto = nome_oggetto.getJSONObject(i);
children[3][i] = prodotto.getString("name");
}
}catch(JSONException ex){ex.printStackTrace();}
try{
JSONObject jsonResponse = new JSONObject(json);
JSONArray nome_oggetto = jsonResponse.getJSONArray("lampade");
for (int i=0; i<nome_oggetto.length(); i++) {
JSONObject prodotto = nome_oggetto.getJSONObject(i);
children[4][i] = prodotto.getString("name");
}
}catch(JSONException ex){ex.printStackTrace();}
return json;
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return groups.length;
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return children[groupPosition].length;
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return groups[groupPosition];
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return children[groupPosition][childPosition];
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
} @Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
convertView = inflater.inflate(R.layout.group_item, null);
TextView tv = (TextView) convertView.findViewById(R.id.group_catalogo);
tv.setText(getGroup(groupPosition).toString());
return tv;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
convertView = inflater.inflate(R.layout.child_item, null);
TextView tv = (TextView) convertView.findViewById(R.id.child_catalogo);
tv.setText(getChild(groupPosition, childPosition).toString());
return tv;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
}
}
以下是我试图膨胀的布局:child_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="45dp"
android:orientation="vertical" >
<TextView
android:id="@+id/child_catalogo"
android:layout_width="fill_parent"
android:layout_height="45dp"
android:layout_marginRight="15dp"
android:textColor="#FF0000"
android:textSize="17dp" />
</LinearLayout>
和group_item.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="45dp"
android:orientation="vertical" >
<TextView
android:id="@+id/group_catalogo"
android:layout_width="fill_parent"
android:layout_height="45dp"
android:layout_marginRight="15dp"
android:textColor="#FF0000"
android:textSize="30dp" />
</LinearLayout>
最后片段布局fragment_catalogo.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.lab2mad.ExpandableListFragment" >
<ExpandableListView
android:id="@+id/lista_catalogo"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ExpandableListView>
</LinearLayout>
如果有人能给我一些提示,我将非常感激。 谢谢
在这里你出错了日志:
05-05 21:51:15.718:W / dalvikvm(11944):threadid = 1:线程退出时未捕获异常(group = 0x40018578) 05-05 21:51:15.835:E / AndroidRuntime(11944):致命异常:主要 05-05 21:51:15.835:E / AndroidRuntime(11944):java.lang.ClassCastException:android.widget.LinearLayout $ LayoutParams 05-05 21:51:15.835:E / AndroidRuntime(11944):在android.widget.ListView.setupChild(ListView.java:1816) 05-05 21:51:15.835:E / AndroidRuntime(11944):在android.widget.ListView.makeAndAddView(ListView.java:1785) 05-05 21:51:15.835:E / AndroidRuntime(11944):在android.widget.ListView.fillDown(ListView.java:705) 05-05 21:51:15.835:E / AndroidRuntime(11944):在android.widget.ListView.fillFromTop(ListView.java:762) 05-05 21:51:15.835:E / AndroidRuntime(11944):在android.widget.ListView.layoutChildren(ListView.java:1633) 05-05 21:51:15.835:E / AndroidRuntime(11944):在android.widget.AbsListView.onLayout(AbsListView.java:1422) 05-05 21:51:15.835:E / AndroidRuntime(11944):在android.view.View.layout(View.java:7175) 05-05 21:51:15.835:E / AndroidRuntime(11944):在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254) 05-05 21:51:15.835:E / AndroidRuntime(11944):在android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1243) 05-05 21:51:15.835:E / AndroidRuntime(11944):在android.widget.LinearLayout.onLayout(LinearLayout.java:1049) 05-05 21:51:15.835:E / AndroidRuntime(11944):在android.view.View.layout(View.java:7175) 05-05 21:51:15.835:E / AndroidRuntime(11944):在android.widget.FrameLayout.onLayout(FrameLayout.java:338) 05-05 21:51:15.835:E / AndroidRuntime(11944):在android.view.View.layout(View.java:7175) 05-05 21:51:15.835:E / AndroidRuntime(11944):在android.widget.FrameLayout.onLayout(FrameLayout.java:338) 05-05 21:51:15.835:E / AndroidRuntime(11944):在android.view.View.layout(View.java:7175) 05-05 21:51:15.835:E / AndroidRuntime(11944):在android.widget.FrameLayout.onLayout(FrameLayout.java:338) 05-05 21:51:15.835:E / AndroidRuntime(11944):在android.view.View.layout(View.java:7175) 05-05 21:51:15.835:E / AndroidRuntime(11944):在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254) 05-05 21:51:15.835:E / AndroidRuntime(11944):在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130) 05-05 21:51:15.835:E / AndroidRuntime(11944):在android.widget.LinearLayout.onLayout(LinearLayout.java:1047) 05-05 21:51:15.835:E / AndroidRuntime(11944):在android.view.View.layout(View.java:7175) 05-05 21:51:15.835:E / AndroidRuntime(11944):在android.widget.FrameLayout.onLayout(FrameLayout.java:338) 05-05 21:51:15.835:E / AndroidRuntime(11944):在android.view.View.layout(View.java:7175) 05-05 21:51:15.835:E / AndroidRuntime(11944):在android.widget.FrameLayout.onLayout(FrameLayout.java:338) 05-05 21:51:15.835:E / AndroidRuntime(11944):在android.view.View.layout(View.java:7175) 05-05 21:51:15.835:E / AndroidRuntime(11944):在android.view.ViewRoot.performTraversals(ViewRoot.java:1146) 05-05 21:51:15.835:E / AndroidRuntime(11944):在android.view.ViewRoot.handleMessage(ViewRoot.java:1865) 05-05 21:51:15.835:E / AndroidRuntime(11944):在android.os.Handler.dispatchMessage(Handler.java:99) 05-05 21:51:15.835:E / AndroidRuntime(11944):在android.os.Looper.loop(Looper.java:130) 05-05 21:51:15.835:E / AndroidRuntime(11944):在android.app.ActivityThread.main(ActivityThread.java:3687) 05-05 21:51:15.835:E / AndroidRuntime(11944):at java.lang.reflect.Method.invokeNative(Native Method) 05-05 21:51:15.835:E / AndroidRuntime(11944):at java.lang.reflect.Method.invoke(Method.java:507) 05-05 21:51:15.835:E / AndroidRuntime(11944):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:867) 05-05 21:51:15.835:E / AndroidRuntime(11944):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 05-05 21:51:15.835:E / AndroidRuntime(11944):at dalvik.system.NativeStart.main(Native Method)
再次感谢
答案 0 :(得分:0)
在onCreateView ---
中替换它 View rootView = inflater.inflate(R.layout.fragment_catalogo, container, false);
用这个----
LinearLayout rootView = (LinearLayout) inflater.inflate(fragment_catalogo, container, false);