4

I'm developing an Android application that uses Fragments. I got this error.

Caused by: java.lang.NullPointerException: name == null

What does name == null mean? Here's the full log

08-04 15:02:22.422: E/AndroidRuntime(2499): FATAL EXCEPTION: main
08-04 15:02:22.422: E/AndroidRuntime(2499): java.lang.RuntimeException: Unable to start activity ComponentInfo{fi.peltoset.mikko.home/fi.peltoset.mikko.home.Koti}: android.view.InflateException: Binary XML file line #17: Error inflating class fragment
08-04 15:02:22.422: E/AndroidRuntime(2499):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
08-04 15:02:22.422: E/AndroidRuntime(2499):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-04 15:02:22.422: E/AndroidRuntime(2499):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
08-04 15:02:22.422: E/AndroidRuntime(2499):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
08-04 15:02:22.422: E/AndroidRuntime(2499):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-04 15:02:22.422: E/AndroidRuntime(2499):     at android.os.Looper.loop(Looper.java:137)
08-04 15:02:22.422: E/AndroidRuntime(2499):     at android.app.ActivityThread.main(ActivityThread.java:4745)
08-04 15:02:22.422: E/AndroidRuntime(2499):     at java.lang.reflect.Method.invokeNative(Native Method)
08-04 15:02:22.422: E/AndroidRuntime(2499):     at java.lang.reflect.Method.invoke(Method.java:511)
08-04 15:02:22.422: E/AndroidRuntime(2499):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-04 15:02:22.422: E/AndroidRuntime(2499):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-04 15:02:22.422: E/AndroidRuntime(2499):     at dalvik.system.NativeStart.main(Native Method)
08-04 15:02:22.422: E/AndroidRuntime(2499): Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class fragment
08-04 15:02:22.422: E/AndroidRuntime(2499):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
08-04 15:02:22.422: E/AndroidRuntime(2499):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
08-04 15:02:22.422: E/AndroidRuntime(2499):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
08-04 15:02:22.422: E/AndroidRuntime(2499):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
08-04 15:02:22.422: E/AndroidRuntime(2499):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
08-04 15:02:22.422: E/AndroidRuntime(2499):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
08-04 15:02:22.422: E/AndroidRuntime(2499):     at android.app.Activity.setContentView(Activity.java:1867)
08-04 15:02:22.422: E/AndroidRuntime(2499):     at fi.peltoset.mikko.home.Koti.onCreate(Koti.java:13)
08-04 15:02:22.422: E/AndroidRuntime(2499):     at android.app.Activity.performCreate(Activity.java:5008)
08-04 15:02:22.422: E/AndroidRuntime(2499):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
08-04 15:02:22.422: E/AndroidRuntime(2499):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
08-04 15:02:22.422: E/AndroidRuntime(2499):     ... 11 more
08-04 15:02:22.422: E/AndroidRuntime(2499): Caused by: java.lang.NullPointerException: name == null
08-04 15:02:22.422: E/AndroidRuntime(2499):     at java.lang.VMClassLoader.findLoadedClass(Native Method)
08-04 15:02:22.422: E/AndroidRuntime(2499):     at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:354)
08-04 15:02:22.422: E/AndroidRuntime(2499):     at java.lang.ClassLoader.loadClass(ClassLoader.java:491)
08-04 15:02:22.422: E/AndroidRuntime(2499):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
08-04 15:02:22.422: E/AndroidRuntime(2499):     at android.app.Fragment.instantiate(Fragment.java:574)
08-04 15:02:22.422: E/AndroidRuntime(2499):     at android.app.Fragment.instantiate(Fragment.java:552)
08-04 15:02:22.422: E/AndroidRuntime(2499):     at android.app.Activity.onCreateView(Activity.java:4656)
08-04 15:02:22.422: E/AndroidRuntime(2499):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
08-04 15:02:22.422: E/AndroidRuntime(2499):     ... 21 more

This is in a class called Navigation. It extends android.app.Fragment.

In it's onCreateView method I inflate the layout and return it. In the onActivityCreated method I check if the layout has two panes (I have different layouts for different devices) and based on that I add onClickListeners to these.

My code looks like this.

public class Navigation extends Fragment {
    private OnItemSelectedListener listener;
    private boolean dualPane = false;

    public interface OnItemSelectedListener {
        public void onMenuItemSelected(int fragmentId);
    }

    public void onAttach(Activity activity) {
        super.onAttach(activity);

        if(activity instanceof OnItemSelectedListener) {
            this.listener = (OnItemSelectedListener) activity;
        } else {
            ...
        }
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_navigation, container, false);

        return view;
    }

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

        View contentPane = getActivity().findViewById(R.id.contentFragment);
        dualPane = contentPane != null && contentPane.getVisibility() == View.VISIBLE;

        if(dualPane) {
            final ListView menu = (ListView) getActivity().findViewById(R.id.valikko);

            menu.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

            String[] texts = new String[] {
                    ...
            };

            ArrayList<MenuItem> items = new ArrayList<MenuItem>();
            items.add(new MenuItem("Home", R.drawable.koti2));
            ...

            ImageArrayAdapter adapter = new ImageArrayAdapter(getActivity(), items, texts);
            menu.setAdapter(adapter);

            menu.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    String item = ((TextView) view.findViewById(R.id.label)).getText().toString();

                    listener.onMenuItemSelected(0); // TODO ...
                }           
            });
        } else {
            ImageView btnYleiskatsaus   = (ImageView) getActivity().findViewById(R.id.btnYleiskatsaus);
            ...

            btnYleiskatsaus.setOnClickListener(onMenuClikkedListener);
            ...
        }       
    }

    private OnClickListener onMenuClikkedListener = new OnClickListener() {

        @Override
        public void onClick(View v) {           
            ...         
        }
    };
}

The layout file layout-sw720dp-land/activity_navigation.xml looks this

<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"
    android:background="@color/pressed_koti"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/valikko"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
    </ListView>
</LinearLayout>

Here is the activity_koti.xml 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="horizontal" >

    <fragment 
        android:id="@+id/menuFragment"
        android:layout_width="300dp"
        android:layout_height="match_parent"
        class="fi.peltoset.mikko.home.Navigation" />

    <fragment 
        android:id="@+id/contentFragment"
        android:layout_width="fill_parent"
        android:layout_height="match_parent" />
</LinearLayout>

I ran the application on an emulator with Android 4.1.2.

What causes these errors and how do I fix them?

6
  • I know what those are, but I'm wondering where that came from. Can't find it in the code... Commented Aug 6, 2013 at 14:11
  • What is at fi.peltoset.mikko.home.Koti.onCreate(Koti.java:13) it appears this is where your code is causing this in the stack trace Commented Aug 6, 2013 at 14:11
  • @Robadob It's the applications launcher Activity's onCreate method and on that line there is setContentView(R.layout.activity_koti); Commented Aug 6, 2013 at 14:13
  • Show that xml file then, not the other one. Commented Aug 6, 2013 at 14:13
  • You should check that in whichever XML file R.layout is stored that activity_koti is set correctly. If that is, check the acitivity_koti.xml for errors. Commented Aug 6, 2013 at 14:13

3 Answers 3

6

You should use android:name to point to your fragments instead of class, it is also necessary to include the same attribute for your second fragment, this would explain the name==null.

Edit: Apparently class may be interchangeable with android:name, but I would follow what the docs use.

It should be;

<?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="horizontal" >

    <fragment 
        android:id="@+id/menuFragment"
        android:layout_width="300dp"
        android:layout_height="match_parent"
        android:name="fi.peltoset.mikko.home.Navigation" />

    <fragment 
        android:id="@+id/contentFragment"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:name="I NEED A NAME" />
</LinearLayout>

http://developer.android.com/training/basics/fragments/creating.html#AddInLayout

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for pointing out I need the android:name (or the class) attribute on the second fragment element too. That Element contains swappable content so I left it without it. Thanks to this post I replaced the second fragment with a LinearLayout and programmatically inserted the layout.
1

When you add a fragment in the XML file, you need to give it a name attribute. That's why it says "name = null", because you're not doing that.

See the guide for adding Fragments for more.

Comments

0

Try adding a class to your second fragment in activity_koti.xml if you're referencing it in your FragmentActivity.

Or, as Robadob says, add android:name:"your.path.YourName" to both.

Interesting additional info on class vs android:name in XML references to Fragments here.

4 Comments

The two (1, 2) android guide pages on fragments don't mention the class attribute, they use android:name for the function. On checking the docs page though, that uses class and not android:name.
@Robadob yes, and it actually works, I promise :) I've developed some fragments using class in the xml. Wasn't too sure it would apply because they were support fragments, but it doesn't seem to be relevant. See my edit for a link to the SO question on class vs android:name.
I've already included the same link in my answer, read it then.
@Robadob oops - I read your comment and reacted before reading your answer again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.