Error when going into new activity Android

154 Views Asked by At

I'm trying to start new settings activity. Unfortunately I got these errors:

06-24 01:18:17.347: E/AndroidRuntime(16017): FATAL EXCEPTION: main
06-24 01:18:17.347: E/AndroidRuntime(16017): Process: com.stoper.moneystopwatch, PID: 16017
06-24 01:18:17.347: E/AndroidRuntime(16017): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.stoper.moneystopwatch/com.stoper.moneystopwatch.Settings}: android.view.InflateException: Binary XML file line #1: Error inflating class RelativeLayout
06-24 01:18:17.347: E/AndroidRuntime(16017):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2252)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2301)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at android.app.ActivityThread.access$800(ActivityThread.java:144)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at android.os.Handler.dispatchMessage(Handler.java:106)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at android.os.Looper.loop(Looper.java:136)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at android.app.ActivityThread.main(ActivityThread.java:5212)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at java.lang.reflect.Method.invokeNative(Native Method)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at java.lang.reflect.Method.invoke(Method.java:515)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at dalvik.system.NativeStart.main(Native Method)
06-24 01:18:17.347: E/AndroidRuntime(16017): Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class RelativeLayout
06-24 01:18:17.347: E/AndroidRuntime(16017):    at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:441)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at android.preference.GenericInflater.inflate(GenericInflater.java:317)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at android.preference.GenericInflater.inflate(GenericInflater.java:263)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:272)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at android.preference.PreferenceFragment.addPreferencesFromResource(PreferenceFragment.java:285)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at com.stoper.moneystopwatch.Settings$MyPreferenceFragment.onCreate(Settings.java:28)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at android.app.Fragment.performCreate(Fragment.java:1683)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:863)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1066)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at android.app.BackStackRecord.run(BackStackRecord.java:684)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1461)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at android.app.Activity.performStart(Activity.java:5249)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2225)
06-24 01:18:17.347: E/AndroidRuntime(16017):    ... 11 more
06-24 01:18:17.347: E/AndroidRuntime(16017): Caused by: java.lang.ClassNotFoundException: Didn't find class "android.preference.RelativeLayout" on path: DexPathList[[zip file "/data/app/com.stoper.moneystopwatch-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.stoper.moneystopwatch-1, /vendor/lib, /system/lib]]
06-24 01:18:17.347: E/AndroidRuntime(16017):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at android.preference.GenericInflater.createItem(GenericInflater.java:375)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at android.preference.GenericInflater.onCreateItem(GenericInflater.java:417)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:428)
06-24 01:18:17.347: E/AndroidRuntime(16017):    ... 23 more

My Activity start:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
         Intent i= new Intent(Main.this, Settings.class);
         startActivity(i);
      return true;
    }
}

My settings class:

public class Settings extends PreferenceActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
    }

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

My Android Manifest:

<activity
    android:name=".Settings"
    android:label="@string/action_settings" >
</activity>

I already tried to clean and rebuild the project. I'm using Eclipse.

It would be very nice if someone could help me with this problem because I've been fighting with it for hours already.

2

There are 2 best solutions below

1
On BEST ANSWER

Considering your logcat you have a problem inside your preference xml. android.view.InflateException: Binary XML file line #1: Error inflating class RelativeLayout

Your xml should not contain any RelativeLayout or any usual widgets, except preference specific tags as described in here.

As for a small test remove all xml from R.xml.settings and paste

<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:key="first_preferencescreen">
<CheckBoxPreference
        android:key="wifi enabled"
        android:title="WiFi" />
<PreferenceScreen
        android:key="second_preferencescreen"
        android:title="WiFi settings">
    <CheckBoxPreference
            android:key="prefer wifi"
            android:title="Prefer WiFi" />
     </PreferenceScreen> </PreferenceScreen> 


and try again.

0
On

You cant be replacing the android.R.id.content layout directly and you need a have your own layout as a child to that. That is have have a rough layout in Xml (say act_settings.xml) and in your oncreate use setContentView(R.layout.act_settings); and replace the root view of this layout in your fragment transaction