I am new at developing Android programs and am stuck on trying to complete an exercise from the Professional Android 4 book that introduces fragments. I get the following error:
InflateException: Binary XML file line #6: Error inflating class fragment
Below is my main activity java program:
package com.example.todolist;
import java.util.ArrayList;
import android.app.Activity;
import android.app.FragmentManager;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class MainActivity extends Activity
implements NewItemFragment.OnNewItemAddedListener {
private ArrayAdapter<String> aa;
private ArrayList<String> todoItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get references to the Fragments
FragmentManager fm = getFragmentManager();
ToDoListFragment todoListFragment =
(ToDoListFragment)fm.findFragmentById(R.id.ToDoListFragment);
aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, todoItems);
todoListFragment.setListAdapter(aa);
@Override
public void onNewItemAdded(String newItem) {
// TODO Auto-generated method stub
todoItems.add(newItem);
aa.notifyDataSetChanged();
}
}
The ToDoListFragment class is shown below:
package com.example.todolist;
import android.app.ListFragment;
public class ToDoListFragment extends ListFragment{
}
Below is my NewItemFragment class.
package com.example.todolist;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
public class NewItemFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.new_item_fragment, container, false);
final EditText myEditText = (EditText)view.findViewById(R.id.myEditText);
myEditText.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN)
if((keyCode == KeyEvent.KEYCODE_DPAD_CENTER) ||
(keyCode == KeyEvent.KEYCODE_ENTER)) {
String newItem = myEditText.getText().toString();
onNewItemAddedListener.onNewItemAdded(newItem);
myEditText.setText("");
return true;
}
return false;
}
});
return view;
}
public interface OnNewItemAddedListener {
public void onNewItemAdded(String newItem);
}
private OnNewItemAddedListener onNewItemAddedListener;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
onNewItemAddedListener = (OnNewItemAddedListener)activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString());
}
}
}
Below is my activity_main.xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="com.paad.todolist.NewItemFragment"
android:id="@+id/NewItemFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/addItemHint"
/>
<fragment android:name="com.paad.todolist.ToDoListFragment"
android:id="@+id/ToDoListFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Finally, here is my new_itemFragment xml file:
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/addItemHint"
/>
If anyone can tell me why I am getting the inflating fragment error I will greatly appreciate it. I tried using import android.support.v4.app.FragmentManager but that didn't work and try using android.support.v4.app.ListFragment and that didn't work either. I have been programming for many years in other languages but Android is really a challenge for me so far using Eclipse. Are their better IDEs for developing Android projects?
I try importing the android.support.v4.app fragment classes but when I do the getFragmentManager function is unresolved. Below is a dump of the logcat.
02-21 20:53:27.769: E/AndroidRuntime(1280): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.todolist/com.example.todolist.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
02-21 20:53:27.769: E/AndroidRuntime(1280): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-21 20:53:27.769: E/AndroidRuntime(1280): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-21 20:53:27.769: E/AndroidRuntime(1280): at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-21 20:53:27.769: E/AndroidRuntime(1280): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-21 20:53:27.769: E/AndroidRuntime(1280): at android.os.Handler.dispatchMessage(Handler.java:99)
02-21 20:53:27.769: E/AndroidRuntime(1280): at android.os.Looper.loop(Looper.java:137)
02-21 20:53:27.769: E/AndroidRuntime(1280): at android.app.ActivityThread.main(ActivityThread.java:5039)
02-21 20:53:27.769: E/AndroidRuntime(1280): at java.lang.reflect.Method.invokeNative(Native Method)
02-21 20:53:27.769: E/AndroidRuntime(1280): at java.lang.reflect.Method.invoke(Method.java:511)
02-21 20:53:27.769: E/AndroidRuntime(1280): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-21 20:53:27.769: E/AndroidRuntime(1280): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-21 20:53:27.769: E/AndroidRuntime(1280): at dalvik.system.NativeStart.main(Native Method)
02-21 20:53:27.769: E/AndroidRuntime(1280): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
02-21 20:53:27.769: E/AndroidRuntime(1280): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
02-21 20:53:27.769: E/AndroidRuntime(1280): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
02-21 20:53:27.769: E/AndroidRuntime(1280): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
02-21 20:53:27.769: E/AndroidRuntime(1280): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
02-21 20:53:27.769: E/AndroidRuntime(1280): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
02-21 20:53:27.769: E/AndroidRuntime(1280): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
02-21 20:53:27.769: E/AndroidRuntime(1280): at android.app.Activity.setContentView(Activity.java:1881)
02-21 20:53:27.769: E/AndroidRuntime(1280): at com.example.todolist.MainActivity.onCreate(MainActivity.java:20)
02-21 20:53:27.769: E/AndroidRuntime(1280): at android.app.Activity.performCreate(Activity.java:5104)
02-21 20:53:27.769: E/AndroidRuntime(1280): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-21 20:53:27.769: E/AndroidRuntime(1280): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
02-21 20:53:27.769: E/AndroidRuntime(1280): ... 11 more
02-21 20:53:27.769: E/AndroidRuntime(1280): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.paad.todolist.NewItemFragment: make sure class name exists, is public, and has an empty constructor that is public
02-21 20:53:27.769: E/AndroidRuntime(1280): at android.app.Fragment.instantiate(Fragment.java:592)
02-21 20:53:27.769: E/AndroidRuntime(1280): at android.app.Fragment.instantiate(Fragment.java:560)
02-21 20:53:27.769: E/AndroidRuntime(1280): at android.app.Activity.onCreateView(Activity.java:4709)
02-21 20:53:27.769: E/AndroidRuntime(1280): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
02-21 20:53:27.769: E/AndroidRuntime(1280): ... 21 more
02-21 20:53:27.769: E/AndroidRuntime(1280): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.paad.todolist.NewItemFragment" on path: /data/app/com.example.todolist-1.apk
02-21 20:53:27.769: E/AndroidRuntime(1280): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
02-21 20:53:27.769: E/AndroidRuntime(1280): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
02-21 20:53:27.769: E/AndroidRuntime(1280): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
Running the debugger I found that the exception was thrown at these instructions:
aa = new ArrayAdapter(this, android.R.layout.simple_list_item_1, todoItems);
todoListFragment.setListAdapter(aa);
I noted that todoItems has a value of null so I am wondering if that element has to be initialized with a value.