我是Android新手,我正在尝试将此活动:ANDROID: EXPANDABLE LIST VIEW EXAMPLE改编为片段,以便我可以在主 - 详细信息流中使用它,并且我不断收到此消息:
FATAL EXCEPTION: main
E/AndroidRuntime(2391): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.expandablelistdemo/com.example.expandablelistdemo.MainActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
这是我的代码:
清单
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.expandablelistdemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java (它是默认活动)
package com.example.expandablelistdemo;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
activity_main.xml中
<RelativeLayout 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=".MainActivity" >
<fragment
android:id="@+id/item_list"
android:name="com.example.expandablelistdemo.MainFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="@android:layout/list_content" />
</RelativeLayout>
MainFragment.java
package com.example.expandablelistdemo;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainFragment extends Fragment {
List<String> groupList;
List<String> childList;
Map<String, List<String>> laptopCollection;
ExpandableListView expListView;
private LinearLayout ll;
private FragmentActivity fa;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
fa = (FragmentActivity) super.getActivity();
ll = (LinearLayout) inflater.inflate(R.layout.fragment_main, container, false);
return ll;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.getActivity().setContentView(R.layout.fragment_main);
createGroupList();
createCollection();
expListView = (ExpandableListView) super.getActivity().findViewById(R.id.laptop_list);
final ExpandableListAdapter expListAdapter = new ExpandableListAdapter(
super.getActivity(), groupList, laptopCollection);
expListView.setAdapter(expListAdapter);
expListView.setOnChildClickListener(new OnChildClickListener() {
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
final String selected = (String) expListAdapter.getChild(
groupPosition, childPosition);
// Toast.makeText(getBaseContext(), selected, Toast.LENGTH_LONG).show();
return true;
}
});
}
private void createGroupList() {
groupList = new ArrayList<String>();
groupList.add("HP");
groupList.add("Dell");
groupList.add("Lenovo");
groupList.add("Sony");
groupList.add("HCL");
groupList.add("Samsung");
}
private void createCollection() {
// preparing laptops collection(child)
String[] hpModels = { "HP Pavilion G6-2014TX", "ProBook HP 4540",
"HP Envy 4-1025TX" };
String[] hclModels = { "HCL S2101", "HCL L2102", "HCL V2002" };
String[] lenovoModels = { "IdeaPad Z Series", "Essential G Series",
"ThinkPad X Series", "Ideapad Z Series" };
String[] sonyModels = { "VAIO E Series", "VAIO Z Series",
"VAIO S Series", "VAIO YB Series" };
String[] dellModels = { "Inspiron", "Vostro", "XPS" };
String[] samsungModels = { "NP Series", "Series 5", "SF Series" };
laptopCollection = new LinkedHashMap<String, List<String>>();
for (String laptop : groupList) {
if (laptop.equals("HP")) {
loadChild(hpModels);
} else if (laptop.equals("Dell"))
loadChild(dellModels);
else if (laptop.equals("Sony"))
loadChild(sonyModels);
else if (laptop.equals("HCL"))
loadChild(hclModels);
else if (laptop.equals("Samsung"))
loadChild(samsungModels);
else
loadChild(lenovoModels);
laptopCollection.put(laptop, childList);
}
}
private void loadChild(String[] laptopModels) {
childList = new ArrayList<String>();
for (String model : laptopModels)
childList.add(model);
}
// Convert pixel to dip
public int getDipsFromPixel(float pixels) {
// Get the screen's density scale
final float scale = getResources().getDisplayMetrics().density;
// Convert the dps to pixels, based on density scale
return (int) (pixels * scale + 0.5f);
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
super.getActivity().getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
fragment_main.xml
<RelativeLayout 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"
tools:context=".MainFragment" >
<ExpandableListView
android:id="@+id/laptop_list"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
</ExpandableListView>
</RelativeLayout>
还有1个java类和2个布局在列表中使用,但我确定这些不是问题....我只能弄明白问题在哪里
我最好猜测它是fa = (FragmentActivity) super.getActivity();
,那个演员似乎很奇怪......或者也许它只是我
非常感谢任何帮助!
答案 0 :(得分:1)
尝试使用MainActivity
而不仅仅FragmentActivity
扩展Activity
。即:
public class MainActivity extends FragmentActivity