Sauerkraut
Sauerkraut

Reputation: 31

android.view.InflateException - Android app developement for dummies

I am trying to learn some android programming with the book Android Application Developement for Dummies. One task in the book is to create an application called task reminder which introduces android fragments. According to the book the following pieces of coding should create a first simple listview:

import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;

public class ReminderListActivity extends ListActivity {

@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.reminder_list);    
    }

}

with reminder_list looking like this:

<?xml version="1.0" encoding="utf-8"?>

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
   android:name="com.holtkoe.android.birthdaytimer.ReminderListFragment"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent" />

The class ReminderListFragment is the following:

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;

public class ReminderListFragment extends ListFragment {

   private ListAdapter mAdapter;

    @Override
    public void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);

        String[] items = new String[] {"Foo","Bar","Fizz","Bin"};

        mAdapter = new ArrayAdapter<String>(getActivity(), R.layout.reminder_row,  R.id.text1, items);
        setListAdapter(mAdapter);

     }
  }

and finally reminder_row:

<?xml version="1.0" encoding="utf-8"?>
<TextView 
    android:id="@+id/text1"
    xmlns:android="http://schemas.android.com/apk/res/android"    
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"/>

I tried to run this on both an emulated device and on my smartphone but always get the following

09-26 22:44:53.270: D/AndroidRuntime(2493): Shutting down VM
09-26 22:44:53.270: W/dalvikvm(2493): threadid=1: thread exiting with uncaught   exception (group=0x412d8930)
09-26 22:44:53.290: E/AndroidRuntime(2493): FATAL EXCEPTION: main
09-26 22:44:53.290: E/AndroidRuntime(2493): java.lang.RuntimeException: Unable to  start activity   ComponentInfo{com.holtkoe.android.birthdaytimer/com.holtkoe.android.birthdaytimer.ReminderListActivity}: android.view.InflateException: Binary XML file line #3: Error inflating class fragment
09-26 22:44:53.290: E/AndroidRuntime(2493):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
09-26 22:44:53.290: E/AndroidRuntime(2493):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
09-26 22:44:53.290: E/AndroidRuntime(2493):     at android.app.ActivityThread.access$700(ActivityThread.java:150)
09-26 22:44:53.290: E/AndroidRuntime(2493):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
09-26 22:44:53.290: E/AndroidRuntime(2493):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-26 22:44:53.290: E/AndroidRuntime(2493):     at android.os.Looper.loop(Looper.java:137)
09-26 22:44:53.290: E/AndroidRuntime(2493):     at android.app.ActivityThread.main(ActivityThread.java:5283)  
09-26 22:44:53.290: E/AndroidRuntime(2493):     at java.lang.reflect.Method.invokeNative(Native Method)
09-26 22:44:53.290: E/AndroidRuntime(2493):     at java.lang.reflect.Method.invoke(Method.java:511)
09-26 22:44:53.290: E/AndroidRuntime(2493):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
09-26 22:44:53.290: E/AndroidRuntime(2493):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
09-26 22:44:53.290: E/AndroidRuntime(2493):     at dalvik.system.NativeStart.main(Native Method)
09-26 22:44:53.290: E/AndroidRuntime(2493): Caused by: android.view.InflateException: Binary XML file line #3: Error inflating class fragment
09-26 22:44:53.290: E/AndroidRuntime(2493):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
09-26 22:44:53.290: E/AndroidRuntime(2493):     at android.view.LayoutInflater.inflate(LayoutInflater.java:467)
09-26 22:44:53.290: E/AndroidRuntime(2493):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
09-26 22:44:53.290: E/AndroidRuntime(2493):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
09-26 22:44:53.290: E/AndroidRuntime(2493):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:364)
09-26 22:44:53.290: E/AndroidRuntime(2493):     at android.app.Activity.setContentView(Activity.java:1930)
09-26 22:44:53.290: E/AndroidRuntime(2493):     at com.holtkoe.android.birthdaytimer.ReminderListActivity.onCreate(ReminderListActivity.java:14)
09-26 22:44:53.290: E/AndroidRuntime(2493):     at android.app.Activity.performCreate(Activity.java:5283)
09-26 22:44:53.290: E/AndroidRuntime(2493):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)   
09-26 22:44:53.290: E/AndroidRuntime(2493):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
09-26 22:44:53.290: E/AndroidRuntime(2493):     ... 11 more
09-26 22:44:53.290: E/AndroidRuntime(2493): Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class com.holtkoe.android.birthdaytimer.ReminderListFragment that is not a Fragment
09-26 22:44:53.290: E/AndroidRuntime(2493):     at android.app.Fragment.instantiate(Fragment.java:584)
09-26 22:44:53.290: E/AndroidRuntime(2493):     at android.app.Fragment.instantiate(Fragment.java:560)
09-26 22:44:53.290: E/AndroidRuntime(2493):     at android.app.Activity.onCreateView(Activity.java:4864)
09-26 22:44:53.290: E/AndroidRuntime(2493):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:686)
09-26 22:44:53.290: E/AndroidRuntime(2493):     ... 20 more
09-26 22:44:53.290: E/AndroidRuntime(2493): Caused by: java.lang.ClassCastException
09-26 22:44:53.290: E/AndroidRuntime(2493):     ... 24 more

To be honest I am lost (and obviously not even a dummy)...I read the Android Developer Reference for fragments and the example given there looked quite similar to what was written in the book, however, for some reason there is an inflation problem.

I am very grateful for any help! Thanks, guys!

Upvotes: 2

Views: 173

Answers (3)

Sauerkraut
Sauerkraut

Reputation: 31

Initially I changed the fragment tag to a listview tag based on what I read about using fragmentlist at the developer's resources...at least I encountered no exemption but the list items have not been displayed. I tried some changes to the textview layout but finally gave up on this.

I then found a simple listfragment example in the web which did only adress API level 14, so I just used this as the minimal level and adapted the code accordingly (starting from scratch as I messed up the previous code). Obviously, no support library is needed and it works properly. So I will just go ahead on this API level and will try to get lower levels running once I am more experienced ;-)

This is the code which works now:

package de.holtkoe.birthdayreminder;

import android.app.Activity;
import android.os.Bundle;


public class ReminderListActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reminder_list);
    }

}

The layout resource reminder_list:

<LinearLayout 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">

    <fragment
        android:id="@+id/fragment"
        android:name="de.holtkoe.birthdayreminder.ReminderListFragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>


</LinearLayout>

The fragment activity is again ReminderListFragment:

package de.holtkoe.birthdayreminder;

import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;

public class ReminderListFragment extends ListFragment {

    private ListAdapter mAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle     savedInstanceState){
        super.onCreate(savedInstanceState);

        String[] items = new String[] {"Foo","Bar","Fizz","Bin"};

        mAdapter = new ArrayAdapter<String>(inflater.getContext(), R.layout.reminder_row, items);
        setListAdapter(mAdapter);

        return super.onCreateView(inflater, container, savedInstanceState);
    }
}

and finally reminder_row stayed unchanged.

Upvotes: 0

laalto
laalto

Reputation: 152787

You cannot mix platform fragments (android.app.Fragment and such) and support library fragments (android.support.v4.app.Fragment).

If you intend to inflate a support library fragment using the <fragment> tag, your activity should extend the support library FragmentActivity and not e.g. ListActivity.

Upvotes: 0

arlistan
arlistan

Reputation: 741

I think you are missing an onCreateView method to inflate your view. You still have to inflate a view inside your fragment.

i.e.

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;

public class ReminderListFragment extends ListFragment {
    private ListAdapter mAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.reminder_list, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);

        String[] items = new String[] {"Foo","Bar","Fizz","Bin"};

        mAdapter = new ArrayAdapter<String>(getActivity(), R.layout.reminder_row,
                                            R.id.text1, items);
        setListAdapter(mAdapter);

    }
}

Edit:

Here is another thing you can try, in the example you gave me, in the xml the fragment is inside a frame layout, you can try changing your xml to look like this:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent" android:layout_height="match_parent">

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        android:name="com.holtkoe.android.birthdaytimer.ReminderListFragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</FrameLayout>

Try with and without the onCreateView to see all options and how they behave.

Upvotes: 2

Related Questions