-4

In my app using support library v7 I want to add a preference screen. Since Google provides absolutely no documentation on that subject I have looked up a post found elsewhere here on Stack Overflow.

So this is my project:

activity_preferences.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:support="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <include layout="@layout/toolbar_default"/>

    <fragment
        android:name=".FragmentPreferences"
        name=".FragmentPreferences"
        android:tag=".FragmentPreferences"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

ActivityPreferences.java

package com.example.testandroidsupportv7;

import com.example.testandroidsupportv7.R;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class ActivityPreferences extends AppCompatActivity
{

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

}

FragmentPreferences.java

package com.example.testandroidsupportv7;

import com.example.testandroidsupportv7.R;
import android.os.Bundle;
import android.support.v7.preference.PreferenceFragmentCompat;

public class FragmentPreferences extends PreferenceFragmentCompat
{

    @Override
    public void onCreatePreferences(Bundle bundle, String s) {
        addPreferencesFromResource(R.xml.preferences);
    }

}

AndroidManifest.xml

<activity
        android:name=".ActivityPreferences"
        android:label="@string/app_name"
        android:theme="@style/MyTheme.NoActionBar" >
</activity>

xml/preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.preference.PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android" >

    <android.support.v7.preference.PreferenceCategory
        android:title="Splošne nastavitve" >

        <android.support.v7.preference.CheckBoxPreference
            android:key="PREFERENCE_KEY_CHECK_BOX"
            android:defaultValue="false"
            android:summaryOff="Value is OFF."
            android:summaryOn="Value is ON."
            android:title="Check box preference" />

    </android.support.v7.preference.PreferenceCategory>

</android.support.v7.preference.PreferenceScreen>

If I try to start this preference activity I get an exception.

logcat

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testandroidsupportv7/com.example.testandroidsupportv7.ActivityPreferences}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:130)
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testandroidsupportv7/com.example.testandroidsupportv7.ActivityPreferences}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
    at android.app.ActivityThread.main(ActivityThread.java:3691)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
FATAL EXCEPTION: main
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
    at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:257)
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
    at com.example.testandroidsupportv7.ActivityPreferences.onCreate(ActivityPreferences.java:13)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
    ... 11 more
Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.example.testandroidsupportv7-2.apk]
    at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
    at android.view.LayoutInflater.createView(LayoutInflater.java:471)
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:549)
    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
    ... 20 more
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)

What is wrong with this code? This should work. The exception given is puzzling and offers no clue. Google provides no documentation and no working example. What I find suspicious is the line:

Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.example.testandroidsupportv7-2.apk]

How to get rid of this exception?

UPDATE

I am running on android 2.3.5 device. I have added preference.xml

animuson
  • 53,861
  • 28
  • 137
  • 147
f470071
  • 1,527
  • 3
  • 18
  • 30
  • Could it be in your activity_preferences.xml -- name=".FragmentPreferences" – mjp66 Nov 04 '15 at 23:27
  • 2
    Also see the [pro-tip on using the Preferences Support Library](https://plus.google.com/+AndroidDevelopers/posts/9kZ3SsXdT2T) – ianhanniballake Nov 04 '15 at 23:49
  • @ianhanniballake I have read that and a lot of other stuff. – f470071 Nov 05 '15 at 07:20
  • I just noticed you are extending appCompat Activity, did you try extending PreferenceActivity instead? refer to this example, http://alvinalexander.com/android/android-tutorial-preferencescreen-preferenceactivity-preferencefragment – Nouran S. Ahmad Nov 05 '15 at 14:13
  • AppCompatActivity extends android.support.v4.app.FragmentActivity and is recommended / necessary if you want to use toolbar in preference activity. The example you are referring to uses android 5.0 and not support library. – f470071 Nov 05 '15 at 14:18

1 Answers1

7

Your logcat hints at the error: XML file line #11: Error inflating class fragment -- the suspicious line there seems to be name=".FragmentPreferences" which you probably wanted to be android:name=".FragmentPreferences". Since you already have that, just delete the erroneous line :)

<fragment
    android:name=".FragmentPreferences"
    android:tag=".FragmentPreferences"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

UPDATE: if that doesn't work, then the other blatantly obvious error is in the android:namevalue. It should be:

android:name="com.example.testandroidsupportv7.FragmentPreferences"

or whatever is YOUR actual path to the FragmentPreferences class.

I even built your project on my end, it took all of less than 5 minutes to create, test, and confirm the error; I got exactly the same error as you, when I fixed android:name it builds just fine. Screenshot:

enter image description here

I hope you will apologize to everyone for your behaviour... it is NOT how to go about getting help from people on SO.

UPDATE 2: partial logcat from my build, with the erroneous android:name value:

11-07 14:00:24.742 26456-26456/com.example.testandroidsupportv7 E/AndroidRuntime: FATAL EXCEPTION: main
11-07 14:00:24.742 26456-26456/com.example.testandroidsupportv7 E/AndroidRuntime: Process: com.example.testandroidsupportv7, PID: 26456
11-07 14:00:24.742 26456-26456/com.example.testandroidsupportv7 E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testandroidsupportv7/com.example.testandroidsupportv7.ActivityPreferences}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
11-07 14:00:24.742 26456-26456/com.example.testandroidsupportv7 E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2345)
11-07 14:00:24.742 26456-26456/com.example.testandroidsupportv7 E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2405)
11-07 14:00:24.742 26456-26456/com.example.testandroidsupportv7 E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:155)
11-07 14:00:24.742 26456-26456/com.example.testandroidsupportv7 E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1323)
...
11-07 14:00:24.742 26456-26456/com.example.testandroidsupportv7 E/AndroidRuntime:   Suppressed: java.lang.ClassNotFoundException: Invalid name: .FragmentPreferences

dependencies:

compile 'com.android.support:support-v4:23.1.0'
compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.android.support:preference-v7:23.1.0'
compile 'com.android.support:design:23.1.0'

Please don't ask me to now find a 2.3.5 device to test for you.

mjp66
  • 4,214
  • 6
  • 26
  • 31
  • 2
    It's not a low quality guess. Your logcat indicates an error in your xml's fragment view and it's immediately obvious where that error is; if removing that line still gives you errors, you still at least now know where to continue looking. I will happily try to help you understand what your logcat is telling you but I certainly won't continue doing the rest of your own work for you. – mjp66 Nov 05 '15 at 08:56
  • I have tried all possible combinations I could think off. andrind:name, name, class, android:class, short class names, full class names, tag, android:tag and all cobinations between them. About 60 different tries. Each of them takes time. – f470071 Nov 05 '15 at 09:07
  • please check this http://stackoverflow.com/questions/22885261/android-error-inflating-class-preferencescreen – Nouran S. Ahmad Nov 05 '15 at 10:28
  • and this, I hope it helps: http://stackoverflow.com/questions/6424853/error-inflating-class-fragment – Nouran S. Ahmad Nov 05 '15 at 10:33
  • @NouranSAhmad Is there anything in particular you would like to draw attention to. I think something is amiss with setContentView() but with lack of documentation I am not sure, neither what should I use instead. – f470071 Nov 05 '15 at 14:00
  • @f470071 the answer is in my comment which gives you a strong hint as to where to look. The immediate clue is in name=".FragmentPreferences" - if that does not solve your problem, there's a very strong 2nd clue but it appears that you demand that the puzzle be solved for you vs. you looking at your fragment tag and seeing what is blatantly obvious. I will update my answer in a minute. – mjp66 Nov 07 '15 at 12:30
  • @mjp66 Since you do not get it.I have tried all combination around that I could think off.And even if this attribute would be a problem:Android ignores attributes it does not understand.I have took some time and trouble to prepare minimum repeatable example,that demonstrates the error. If someone can not replicate this same error I would very much like to know the version it was compiled against, support library revision and build tools version. Now I am thinking somewhere in direction taht setContentView() tries to instantiate wrong class android.view.fragment, which of course does not exist. – f470071 Nov 07 '15 at 12:46
  • 1
    @f470071 no, I do get it... but the point is, the two fixes to your fragment tag results in a perfectly working app (tried on 2 phones and a tablet -- android 4.3, 5.0.2, 5.1). I got the exact same logcat errors as you when I left android:name as-is. If you still cannot run your app after performing both fixes, then ask another question because there is then more at play here than your OP. For all intents and purposes, my answer 100% fixes the errors you were asking about. – mjp66 Nov 07 '15 at 12:55
  • @mjp66 Yes. Because you ran on 4.x devices and higher. That means, that native fragments and activities are used, not implementations in support library. My question revolves around support library. – f470071 Nov 07 '15 at 12:58
  • I used the support libraries... eg. PreferenceFragmentCompat. I'll update my answer once more (btw your original OP never specified what android version you were developing for, you disclosed that after the CORRECT answer was given to you based on your original OP. – mjp66 Nov 07 '15 at 13:07
  • @mjp66 Yes, I believe you did use support library. It is just that (if I am not very much mistaken) that support lib uses native implementations on newer devices and support implementation on older. – f470071 Nov 07 '15 at 13:13