我试图将2个片段放入一个活动中,底部片段给出了布局膨胀异常。请帮我解决这个问题,我坚持使用这个。当我放入列表视图但它不能正常工作时这很好用可扩展列表视图
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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="com.mindtree.calenderevents.MainActivity">
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.mindtree.calenderevents.TopSectionFragment"
android:id="@+id/fragment"
android:layout_alignParentStart="true"
android:layout_marginStart="111dp"
tools:layout="@layout/top_section_fragment" />
<fragment
android:layout_width="340dp"
android:layout_height="330dp"
android:name="com.mindtree.calenderevents.BottomSectionFragment"
android:id="@+id/fragment2"
tools:layout="@layout/bottom_fragment"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
top_section_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/get_events"
android:id="@+id/getEventsButton"
android:layout_marginTop="44dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:onClick="updateDisplay"/>
</RelativeLayout>
bottom_fragment.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:orientation="vertical" >
<ExpandableListView
android:id="@+id/lvExp"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:cacheColorHint="#00000000"/>
</LinearLayout>
这是我的MainActivity,我获得阅读日历和获取当天活动的权限并显示活动的标题
public class MainActivity extends AppCompatActivity implements TopSectionFragment.GetAllEvents {
private static final int WRITE_CALENDER = 1;
private static final int READ_CALENDER = 2;
Calendar c;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TopSectionFragment fragment = TopSectionFragment.newInstance();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment, fragment)
.commit();
}
@Override
public void getEvents() {
//TODO: implement method to fetch events from calendar and add as fragment arguments
getPermissionToReadCalender();
}
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String permissions[], @NonNull int[] grantResults) {
switch (requestCode) {
case READ_CALENDER: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
RuntimeData.setsCalendarData(getCalender(null));
if (RuntimeData.getsCalendarData() != null) {
BottomSectionFragment bottomFragment = new BottomSectionFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment2,
bottomFragment).commit();
}
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
private void getPermissionToReadCalender() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
if (shouldShowRequestPermissionRationale(
Manifest.permission.READ_CALENDAR)) {
}
requestPermissions(new String[]{Manifest.permission.READ_CALENDAR},
READ_CALENDER);
}else{
RuntimeData.setsCalendarData(getCalender(null));
if (RuntimeData.getsCalendarData() != null) {
BottomSectionFragment bottomFragment = new BottomSectionFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment2,
bottomFragment).commit();
}
}
}
protected CalendarData getCalender(View view) {
String[] projection = new String[]{CalendarContract.Events.CALENDAR_ID, CalendarContract.Events.TITLE, CalendarContract.Events.DESCRIPTION, CalendarContract.Events.DTSTART, CalendarContract.Events.DTEND, CalendarContract.Events.ALL_DAY, CalendarContract.Events.EVENT_LOCATION};
c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = df.format(c.getTime());
String splittedDate[] = formattedDate.split("[-\\s]");
for (int i = 0; i < splittedDate.length; i++) {
}
int currentYear = Integer.parseInt(splittedDate[0]);
int currentMonth = Integer.parseInt(splittedDate[1]);
int currentDay = Integer.parseInt(splittedDate[2]);
String currentTime = splittedDate[3];
String splittedTime[] = currentTime.split(":");
int currentHour = Integer.parseInt(splittedTime[0]);
int currentMin = Integer.parseInt(splittedTime[1]);
// 0 = January, 1 = February, ...
Calendar startTime = Calendar.getInstance();
startTime.set(currentYear, currentMonth - 1, 12, 0, currentMin);
// startTime.set(2016,9,26,2,00);
Calendar endTime = Calendar.getInstance();
endTime.set(currentYear, currentMonth, currentDay, 24, 00);
// the range is all data from 2016
String selection = "(( " + CalendarContract.Events.DTSTART + " >= " + startTime.getTimeInMillis() + " ) AND ( " + CalendarContract.Events.DTSTART + " <= " + endTime.getTimeInMillis() + " ))";
Cursor cursor = this.getBaseContext().getContentResolver().query(CalendarContract.Events.CONTENT_URI, projection, selection, null, null);
// output the events
CalendarData mCalendarData = new CalendarData();
ArrayList<String> eventTitle = new ArrayList<String>();
ArrayList<String> eventDescription = new ArrayList<String>();
ArrayList<String> eventLocation = new ArrayList<String>();
// assert cursor != null;
if (cursor != null && cursor.moveToFirst()) {
do {
// Toast.makeText(this.getApplicationContext(), "Title: " + cursor.getString(1) + " Start-Time: " + (new Date(cursor.getLong(3))).toString(), Toast.LENGTH_LONG).show();
eventTitle.add(cursor.getString(1));
eventDescription.add(cursor.getString(2));
eventLocation.add(cursor.getString(6));
} while (cursor.moveToNext());
mCalendarData.setEventTitle(eventTitle);
mCalendarData.setEventDesc(eventDescription);
mCalendarData.setEventLoc(eventLocation);
} else {
Toast.makeText(this.getApplicationContext(), "no events for today", Toast.LENGTH_LONG).show();
}
return mCalendarData;
}
}
TopSectionFragment.java
public class TopSectionFragment extends Fragment implements View.OnClickListener {
private GetAllEvents mEventListener;
public static TopSectionFragment newInstance() {
TopSectionFragment fragment = new TopSectionFragment();
return fragment;
}
public TopSectionFragment() {
// Required empty public constructor
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mEventListener = (GetAllEvents) activity;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.top_section_fragment, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Button eventsButton = (Button) view.findViewById(R.id.getEventsButton);
eventsButton.setOnClickListener(this);
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.getEventsButton:
updateDisplay( view);
break;
}
}
protected void updateDisplay(View view) {
mEventListener.getEvents();
}
public interface GetAllEvents {
void getEvents();
}
}
BottomSectionFragment.java
public class BottomSectionFragment extends ListFragment implements AdapterView.OnItemClickListener {
private CalendarData mData=new CalendarData();
ArrayList<String> mEventDesc;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.bottom_fragment,container,false);
if (RuntimeData.getsCalendarData()!=null){
mData = RuntimeData.getsCalendarData();
}
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mEventDesc=mData.getEventTitle();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, mEventDesc);
if(mEventDesc!=null) {
setListAdapter(adapter);
getListView().setOnItemClickListener(this);
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast.makeText(getActivity(), "Item: " + position, Toast.LENGTH_SHORT)
.show();
}
}
这是我得到的堆栈跟踪
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mindtree.calenderevents, PID: 14450
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mindtree.calenderevents/com.mindtree.calenderevents.MainActivity}: android.view.InflateException: Binary XML file line #22: Binary XML file line #22: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3319)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
at android.app.ActivityThread.access$1100(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: android.view.InflateException: Binary XML file line #22: Binary XML file line #22: Error inflating class fragment
at android.view.LayoutInflater.inflate(LayoutInflater.java:551)
at android.view.LayoutInflater.inflate(LayoutInflater.java:429)
at android.view.LayoutInflater.inflate(LayoutInflater.java:380)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.mindtree.calenderevents.MainActivity.onCreate(MainActivity.java:28)
at android.app.Activity.performCreate(Activity.java:6904)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
at android.app.ActivityThread.access$1100(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: android.view.InflateException: Binary XML file line #22: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:794)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:847)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)
at android.view.LayoutInflater.inflate(LayoutInflater.java:527)
at android.view.LayoutInflater.inflate(LayoutInflater.java:429)
at android.view.LayoutInflater.inflate(LayoutInflater.java:380)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.mindtree.calenderevents.MainActivity.onCreate(MainActivity.java:28)
at android.app.Activity.performCreate(Activity.java:6904)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
at android.app.ActivityThread.access$1100(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
at android.support.v4.app.ListFragment.ensureList(ListFragment.java:346)
at android.support.v4.app.ListFragment.onViewCreated(ListFragment.java:147)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1075)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1268)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1370)
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2415)
at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:376)
at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:33)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:75)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:766)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:847)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)
at android.view.LayoutInflater.inflate(LayoutInflater.java:527)
at android.view.LayoutInflater.inflate(LayoutInflater.java:429)
at android.view.LayoutInflater.inflate(LayoutInflater.java:380)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.mindtree.calenderevents.MainActivity.onCreate(MainActivity.java:28)
at android.app.Activity.performCreate(Activity.java:6904)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
at android.app.ActivityThread.access$1100(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)