0

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.

Dave
  • 873
  • 2
  • 15
  • 27
  • I think you need to extend your activity with FragmentActivity not Activity. Can you also post the stack trace of your logcat? – Ariel Magbanua Feb 21 '13 at 20:16
  • Why do you set ArrayAdapter for ToDoListFragment in MainActivity? – user1888162 Feb 21 '13 at 20:28
  • Change this line: View view = inflater.inflate(R.layout.new_item_fragment, container, false); to: View view = inflater.inflate(R.layout.new_item_fragment, null); – user1888162 Feb 21 '13 at 20:40
  • Please post the *entire* stack trace. – CommonsWare Feb 21 '13 at 20:48
  • Below is the stack trace of the logcat. If I import from android.support.v4.app for any fragment class, then the getFragmentManager is unresolved. So is there a replacement for that function? – Dave Feb 21 '13 at 21:15
  • ^ getSupportFragmentManager(); ^ – user1888162 Feb 21 '13 at 21:20
  • I forgot to mention that I am running using an emulator using Nexus S as the device, target is ANdroid 4.2 = API Level 17, RAM 343. – Dave Feb 21 '13 at 21:32
  • I was able to change everything to android.support.v4.app for the Fragment classes and changed to getSupportFragmentManager and changed the View suggested above but still get the same error but I also am getting a D warning now saying that Shutting down VM which is listed before all the other errors I get. I am about to start over on a new example and forget this one as the code from the author from which I am doing my example has errors in it that I had to correct. – Dave Feb 21 '13 at 21:51

3 Answers3

2

It's looking for a Fragment here: com.paad.todolist.NewItemFragment

which is what you have in your xml file.

However, the package that the implementation of that is actually in is in here:

package com.example.todolist;

So, you need to change your xml file to be:

<fragment android:name="com.example.todolist.NewItemFragment"

Check ToDoListFragment too.

James
  • 3,729
  • 3
  • 20
  • 16
  • Thanks for pointing out the package name error. I made the change to point to the right package and now I get a different error which is 02-21 21:17:45.319: E/AndroidRuntime(1752): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.todolist/com.example.todolist.MainActivity}: java.lang.NullPointerException Any idea on what caused that? – Dave Feb 22 '13 at 00:15
  • Look at what line number that NPE occurs on in the MainActivity. – James Feb 22 '13 at 10:22
  • I don't know where to get the line number from LogCat. All I can see is 5134 for the PID and TID. I tried to set breaks and run the debugger but nothing happened. I need to make the transition from Visual Studio and learn how to run the debugger. – Dave Feb 22 '13 at 16:37
  • Running the debugger I found that the exception was thrown at the instruction aa = new ArrayAdapter(this, android.R.layout.simple_list_item_1, todoItems); todoListFragment.setListAdapter(aa); I noted that todoItems have a value of null so I am wondering if that element has to be initialized with a value. – Dave Feb 22 '13 at 17:29
  • Just initialise the ArrayList with some string data, before that line – James Feb 22 '13 at 19:16
  • Ok; I found the problem the in that the declaration of the Arraylist did not include the new ArrayList(). The proper instruction is private ArrayList todoItems = new ArrayList(); Thanks to everyone for their advice and their time. – Dave Feb 22 '13 at 20:46
0

Use new fragment class that is replace the imported package android.app.Fragment\ to android.support.v4.app.Fragment it will help you.

Jitesh Dalsaniya
  • 1,917
  • 3
  • 20
  • 36
-2

MainActivity must extend FragmentActivity class