Reputation: 13
When I try to include a List Fragment to my Activity I get stuck on this error
03-04 02:58:09.740: E/AndroidRuntime(1777): FATAL EXCEPTION: main
03-04 02:58:09.740: E/AndroidRuntime(1777): Process: com.example.health, PID: 1777
03-04 02:58:09.740: E/AndroidRuntime(1777): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.health/com.example.health.PatientMain}: android.view.InflateException: Binary XML file line #13: Error inflating class fragment
03-04 02:58:09.740: E/AndroidRuntime(1777): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
03-04 02:58:09.740: E/AndroidRuntime(1777): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
03-04 02:58:09.740: E/AndroidRuntime(1777): at android.app.ActivityThread.access$800(ActivityThread.java:144)
03-04 02:58:09.740: E/AndroidRuntime(1777): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
03-04 02:58:09.740: E/AndroidRuntime(1777): at android.os.Handler.dispatchMessage(Handler.java:102)
03-04 02:58:09.740: E/AndroidRuntime(1777): at android.os.Looper.loop(Looper.java:135)
03-04 02:58:09.740: E/AndroidRuntime(1777): at android.app.ActivityThread.main(ActivityThread.java:5221)
03-04 02:58:09.740: E/AndroidRuntime(1777): at java.lang.reflect.Method.invoke(Native Method)
03-04 02:58:09.740: E/AndroidRuntime(1777): at java.lang.reflect.Method.invoke(Method.java:372)
03-04 02:58:09.740: E/AndroidRuntime(1777): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
03-04 02:58:09.740: E/AndroidRuntime(1777): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
03-04 02:58:09.740: E/AndroidRuntime(1777): Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class fragment
03-04 02:58:09.740: E/AndroidRuntime(1777): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
03-04 02:58:09.740: E/AndroidRuntime(1777): at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
03-04 02:58:09.740: E/AndroidRuntime(1777): at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
03-04 02:58:09.740: E/AndroidRuntime(1777): at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
03-04 02:58:09.740: E/AndroidRuntime(1777): at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
03-04 02:58:09.740: E/AndroidRuntime(1777): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377)
03-04 02:58:09.740: E/AndroidRuntime(1777): at android.app.Activity.setContentView(Activity.java:2144)
03-04 02:58:09.740: E/AndroidRuntime(1777): at com.example.health.PatientMain.onCreate(PatientMain.java:24)
03-04 02:58:09.740: E/AndroidRuntime(1777): at android.app.Activity.performCreate(Activity.java:5933)
03-04 02:58:09.740: E/AndroidRuntime(1777): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
03-04 02:58:09.740: E/AndroidRuntime(1777): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
03-04 02:58:09.740: E/AndroidRuntime(1777): ... 10 more
03-04 02:58:09.740: E/AndroidRuntime(1777): Caused by: java.lang.ClassCastException: com.example.health.PatientMain@1d8e7e14 must implement OnFragmentInteractionListener
03-04 02:58:09.740: E/AndroidRuntime(1777): at com.example.health.AppointmentFragment.onAttach(AppointmentFragment.java:74)
03-04 02:58:09.740: E/AndroidRuntime(1777): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:853)
03-04 02:58:09.740: E/AndroidRuntime(1777): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1045)
03-04 02:58:09.740: E/AndroidRuntime(1777): at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1147)
03-04 02:58:09.740: E/AndroidRuntime(1777): at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2116)
03-04 02:58:09.740: E/AndroidRuntime(1777): at android.app.Activity.onCreateView(Activity.java:5282)
03-04 02:58:09.740: E/AndroidRuntime(1777): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
03-04 02:58:09.740: E/AndroidRuntime(1777): ... 20 more
here is my activity code and its xml layout file:
package com.example.health;
import java.util.ArrayList;
import com.example.health.R;
import Entity.Appointment;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.app.FragmentManager;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class PatientMain extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_patient_main);
//ListView appointmentLv = (ListView) findViewById(R.id.listview);
Intent intent = getIntent();
int patient_id = intent.getIntExtra("patient_id", -1);
//Log.d("patient id", "patient id " + patient_id);
final DatabaseHandler db = new DatabaseHandler(this);
ArrayList<Appointment> appointmentArray = db.findAppointmentByPatientId(patient_id);
Bundle extras = new Bundle();
extras.putParcelableArrayList("arraylist", appointmentArray);
FragmentManager fragmentManager = getFragmentManager();
Fragment appointmentFragment = (Fragment) fragmentManager.findFragmentById(R.id.appointmentFragment);
appointmentFragment.setArguments(extras);
//Log.d("patient id", "appointment array: " + appointmentArray.size());
//AppointmentListViewArrayAdapter appointmentAdapter = new AppointmentListViewArrayAdapter(this, appointmentArray);
//appointmentLv.setAdapter(appointmentAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.patient_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
layout file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
android:orientation="vertical" >
<CalendarView
android:id="@+id/calendar"
android:layout_width="match_parent"
android:layout_height="216dp"
android:layout_margin="0dp" />
<fragment
android:id="@+id/appointmentFragment"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
class = "com.example.health.AppointmentFragment" />
</LinearLayout>
The fragment java and layout file:
package com.example.health;
import java.util.ArrayList;
import com.example.health.dummy.DummyContent;
import android.app.Activity;
import android.os.Bundle;
import android.os.Parcelable;
import android.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.example.health.R;
public class AppointmentFragment extends ListFragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
// TODO: Rename and change types of parameters
public static AppointmentFragment newInstance(String param1, String param2) {
AppointmentFragment fragment = new AppointmentFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public AppointmentFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
// TODO: Change Adapter to display your content
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
if (null != mListener) {
// Notify the active callbacks interface (the activity, if the
// fragment is attached to one) that an item has been selected.
mListener
.onFragmentInteraction(DummyContent.ITEMS.get(position).id);
}
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated to
* the activity and potentially other fragments contained in that activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
public void onFragmentInteraction(String id);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.appointment_list_view_fragment, container, false);
Bundle extras = getArguments();
ArrayList<Parcelable> myArr = extras.getParcelableArrayList("arraylist");
ListView appointmentLv = (ListView) view.findViewById(R.id.listview_fragment);
AppointmentListViewArrayAdapter appointmentAdapter = new AppointmentListViewArrayAdapter(view.getContext(), myArr);
appointmentLv.setAdapter(appointmentAdapter);
return view;
}
}
and its layout file
<?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" >
<ListView
android:id="@+id/listview_fragment"
android:layout_width = "match_parent"
android:layout_height = "wrap_content" >
</ListView>
</LinearLayout>
Please help me, I have tried many solutions from the Internet but it does not work
Upvotes: 0
Views: 484
Reputation: 981
You have to implement the OnFragmentInteractionListener interface in your PatientMain Activity class and override the corresponding methods.
If you dont need the interaction between your fragment and Activity you can remove the interface declaration on
private OnFragmentInteractionListener mListener;
and this from your Fragment.
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated to
* the activity and potentially other fragments contained in that activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
public void onFragmentInteraction(String id);
}
Upvotes: 1
Reputation: 2153
FragmentActivity
expects you to use support fragments, which must be fully qualified in the layout XML using android.support.v4.app.Fragment
. The result would be:
<android.support.v4.app.Fragment
android:id="@+id/appointmentFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
class="com.example.health.AppointmentFragment" />
Note, you will have to update your other classes to use the support library.
If you do not care about supporting earlier versions of Android, you can simply update PatientMain
to extend Activity
rather than FragmentActivity
.
Upvotes: 0