Ad

Android View InflateException: Binary XML File Line #11 With Runtime Error

- 1 answer

I'd like to anticipate my apologies for proposing again this subject, but I tried to follow any other Q/A I found, and no one worked for me.

Also, I just started programming for Android, so please bear with my ignorance.

Since I'm studying Fragments, I'm trying to find some good examples on the web that can clearly explain this subject.
This is the web resource I followed for my first exercise

Fragment Example

After reading and copying the code in files, I payed attention in changing the package name, but after I ran the application, here is the LogCat I received:

02-12 06:02:03.838: E/AndroidRuntime(1599): java.lang.RuntimeException: Unable to start activity ComponentInfo{it.coppola.fragmentexample/it.coppola.fragmentexample.FragmentExampleActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
02-12 06:02:03.838: E/AndroidRuntime(1599):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
02-12 06:02:03.838: E/AndroidRuntime(1599):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-12 06:02:03.838: E/AndroidRuntime(1599):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-12 06:02:03.838: E/AndroidRuntime(1599):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
02-12 06:02:03.838: E/AndroidRuntime(1599):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
02-12 06:02:03.838: E/AndroidRuntime(1599):     at android.os.Handler.dispatchMessage(Handler.java:102)
02-12 06:02:03.838: E/AndroidRuntime(1599):     at android.os.Looper.loop(Looper.java:136)
02-12 06:02:03.838: E/AndroidRuntime(1599):     at android.app.ActivityThread.main(ActivityThread.java:5017)
02-12 06:02:03.838: E/AndroidRuntime(1599):     at java.lang.reflect.Method.invokeNative(Native Method)
02-12 06:02:03.838: E/AndroidRuntime(1599):     at java.lang.reflect.Method.invoke(Method.java:515)
02-12 06:02:03.838: E/AndroidRuntime(1599):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-12 06:02:03.838: E/AndroidRuntime(1599):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-12 06:02:03.838: E/AndroidRuntime(1599):     at dalvik.system.NativeStart.main(Native Method)
02-12 06:02:03.838: E/AndroidRuntime(1599): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
02-12 06:02:03.838: E/AndroidRuntime(1599):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
02-12 06:02:03.838: E/AndroidRuntime(1599):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
02-12 06:02:03.838: E/AndroidRuntime(1599):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
02-12 06:02:03.838: E/AndroidRuntime(1599):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
02-12 06:02:03.838: E/AndroidRuntime(1599):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
02-12 06:02:03.838: E/AndroidRuntime(1599):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
02-12 06:02:03.838: E/AndroidRuntime(1599):     at android.app.Activity.setContentView(Activity.java:1929)
02-12 06:02:03.838: E/AndroidRuntime(1599):     at it.coppola.fragmentexample.FragmentExampleActivity.onCreate(FragmentExampleActivity.java:13)
02-12 06:02:03.838: E/AndroidRuntime(1599):     at android.app.Activity.performCreate(Activity.java:5231)
02-12 06:02:03.838: E/AndroidRuntime(1599):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-12 06:02:03.838: E/AndroidRuntime(1599):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
02-12 06:02:03.838: E/AndroidRuntime(1599): Caused by: java.lang.NullPointerException
02-12 06:02:03.838: E/AndroidRuntime(1599):     at it.coppola.fragmentexample.ToolbarFragment.onCreateView(ToolbarFragment.java:51)
02-12 06:02:03.838: E/AndroidRuntime(1599):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
02-12 06:02:03.838: E/AndroidRuntime(1599):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:900)
02-12 06:02:03.838: E/AndroidRuntime(1599):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
02-12 06:02:03.838: E/AndroidRuntime(1599):     at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1184)
02-12 06:02:03.838: E/AndroidRuntime(1599):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:291)
02-12 06:02:03.838: E/AndroidRuntime(1599):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)

Class and layout files

FramentExampleActivity.java

package it.coppola.fragmentexample;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.Fragment;
import android.view.Menu;

public class FragmentExampleActivity extends FragmentActivity implements ToolbarFragment.ToolbarListener {

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

    public void onButtonClick(int fontsize, String text) {
        TextFragment textFragment = 
                   (TextFragment) 
                     getSupportFragmentManager().findFragmentById(R.id.text_fragment);      
                 textFragment.changeTextProperties(fontsize, text);

    }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.layout.activity_fragment_example,  menu);
    return true;
}

}

TextFragment.java

package it.coppola.fragmentexample;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class TextFragment extends Fragment {

    private static TextView textview;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.text_fragment,  container, false);
          textview = (TextView) view.findViewById(R.id.textView1);

        return view;
    }

     public void changeTextProperties(int fontsize, String text)
        {
            textview.setTextSize(fontsize);
            textview.setText(text);
        } 

}

ToolbarFrament.java

package it.coppola.fragmentexample;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class ToolbarFragment extends Fragment implements OnSeekBarChangeListener {

      private static int seekvalue = 10;
      private static EditText edittext;
      ToolbarListener activityCallback;

      public interface ToolbarListener {
            public void onButtonClick(int position, String text);
      }

      @Override
      public void onAttach(Activity activity) {
            super.onAttach(activity);
            try {
                activityCallback = (ToolbarListener) activity;
            } catch (ClassCastException e) {
                throw new ClassCastException(activity.toString()
                        + " must implement ToolbarListener");
            }
        }


       @Override
        public View onCreateView(LayoutInflater inflater, 
            ViewGroup container, Bundle savedInstanceState) {
            // Inflate the layout for this fragment

            View view =  inflater.inflate(R.layout.toolbar_fragment,  container, false);

            edittext = (EditText) view.findViewById(R.id.editText1);
            final SeekBar seekbar = (SeekBar) view.findViewById(R.id.seekBar1);

            seekbar.setOnSeekBarChangeListener(this);

            final Button button = (Button) view.findViewById(R.id.button1);

            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    buttonClicked(v);
                }
            });

            return view;
       }

       public void buttonClicked (View view) {
           activityCallback.onButtonClick(seekvalue, edittext.getText().toString());

       }

       @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
           seekvalue = progress;
        }

       @Override
       public void onStartTrackingTouch(SeekBar arg0) {
           // TODO Auto-generated method stub
       }

       @Override
       public void onStopTrackingTouch(SeekBar arg0) {
           // TODO Auto-generated method stub
       }
}

activity_fragment_example.xml

<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=".FragmentExampleActivity" >

    <fragment
        android:id="@+id/toolbar_fragment"
        class="it.coppola.fragmentexample.ToolbarFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        tools:layout="@layout/toolbar_fragment" />

    <fragment
        android:id="@+id/text_fragment"
        class="it.coppola.fragmentexample.TextFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        tools:layout="@layout/text_fragment" />

</RelativeLayout>

text_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

 <TextView
       android:id="@+id/textView1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_centerHorizontal="true"
       android:layout_centerVertical="true"
       android:text="Fragment Two"
       android:textAppearance="?android:attr/textAppearanceLarge" />   
</RelativeLayout>

toolbar_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
     xmlns:tools="http://schemas.android.com/tools">

<Button
    android.id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/seekBar1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="17dp"
    android:text="Change Text" />

<EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="16dp"
        android:ems="10"
        android:inputType="text" >
        <requestFocus />

    </EditText>

        <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="14dp" />

</RelativeLayout>

A couple more:

I ensured I loaded the android-support-v4.jar lib, and I didn't modify the AndroidManifest.xml in any way.

Thanks for any help you'll like to provide, and for your patience.

Ad

Answer

There's a typo in your toolbar fragment layout XML:

android.id="@+id/button1"

Change to android:id:

android:id="@+id/button1"

This causes the findViewById() to return null and attempting to invoke setOnClickListener() on a null causes the NPE on line 51.

Ad
source: stackoverflow.com
Ad