Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

not working with Preference Headers #9

Open
agrajaghh opened this issue Nov 3, 2014 · 2 comments
Open

not working with Preference Headers #9

agrajaghh opened this issue Nov 3, 2014 · 2 comments

Comments

@agrajaghh
Copy link

I tried to build my preferences with preference headers using this lib, but I'm getting the following error when clicking on a header:

    2513-2513/org.testing.preferencefragment_test E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: org.testing.preferencefragment_test, PID: 2513
    java.lang.RuntimeException: Unable to start activity ComponentInfo{org.testing.preferencefragment_test/org.testing.preferencefragment_test.ApplicationPreferencesActivity}: android.app.Fragment$InstantiationException: Trying to instantiate a class org.testing.preferencefragment_test.ApplicationPreferencesActivity$SettingsFragmentTest that is not a Fragment
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class org.testing.preferencefragment_test.ApplicationPreferencesActivity$SettingsFragmentTest that is not a Fragment
            at android.app.Fragment.instantiate(Fragment.java:585)
            at android.preference.PreferenceActivity.switchToHeaderInner(PreferenceActivity.java:1183)
            at android.preference.PreferenceActivity.switchToHeader(PreferenceActivity.java:1199)
            at android.preference.PreferenceActivity.onCreate(PreferenceActivity.java:545)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.ClassCastException
            at android.app.Fragment.instantiate(Fragment.java:585)
            at android.preference.PreferenceActivity.switchToHeaderInner(PreferenceActivity.java:1183)
            at android.preference.PreferenceActivity.switchToHeader(PreferenceActivity.java:1199)
            at android.preference.PreferenceActivity.onCreate(PreferenceActivity.java:545)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

I created a test-project at: https://github.com/agrajaghh/PreferenceFragment-Test following the guidlines at https://developer.android.com/guide/topics/ui/settings.html#PreferenceHeaders

The PreferenceActivity looks like this:

package org.testing.preferencefragment_test;

import android.os.Bundle;
import android.preference.PreferenceActivity;
// Not working:
import android.support.v4.preference.PreferenceFragment;
// working:
//import android.preference.PreferenceFragment;

import java.util.List;

public class ApplicationPreferencesActivity extends PreferenceActivity {
  @Override
  public void onBuildHeaders(List<Header> target) {
    loadHeadersFromResource(R.xml.preference_headers, target);
  }

  @Override
  protected boolean isValidFragment (String fragmentName)
  {
    return (SettingsFragmentTest.class.getName().equals(fragmentName));
  }

  public static class SettingsFragmentTest extends PreferenceFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      addPreferencesFromResource(R.xml.settings_test);
    }
  }
}

with preference_headers.xml:

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

<preference-headers xmlns:android="http://schemas.android.com/apk/res/android">
    <header android:fragment="org.testing.preferencefragment_test.ApplicationPreferencesActivity$SettingsFragmentTest"
            android:title="TEST" />
</preference-headers>

and settings_test.xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <CheckBoxPreference
        android:key="pref_test1"
        android:title="Test1_Title"
        android:defaultValue="true" />
</PreferenceScreen>

When I replace import android.support.v4.preference.PreferenceFragment; with import android.preference.PreferenceFragment; its working. Could I do anything about it, or is it a bug/missing feature of this library?

@kolavar
Copy link
Owner

kolavar commented Nov 25, 2014

#10

@Pitel
Copy link

Pitel commented May 4, 2015

I just got the same problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants