Looking for wrong fragment class with Android compatibility library
Asked Answered
A

7

9

I am trying to inflate a layout containing a Fragment using the backwards compatibility package and SDK level 10. I took the jar file and placed it in the libs folder of my project. I extended FragmentActivity.

It all works perfectly when I run at API level 11 on an XLarge screen device.

When I drop back to compiling against level 10, and running on a normal sized screen, I get failure at the point where it create a new Activity and inflate the fragment in it.

Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.motoappsummitagenda-1.apk] 04-01 01:07:14.311 2870 2870 E AndroidRuntime: at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)

So it looks like something, somewhere is looking for android.view.fragment, and not the compatibility version, android.support.v4.app.Fragment. Of course, android.view.fragment won't be found on API level 10. But where is that android.view.fragment coming from?

The XML that is being inflated is

<?xml version="1.0" encoding="utf-8"?>
<fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:name="com.motorapp.SessionFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/sessionfragment">
</fragment>

The code for it starts:

package com.motorapp;

import java.util.List;
import java.util.ListIterator;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.support.v4.app.*;
import android.support.v4.util.*;
import android.support.v4.*;
import android.support.v4.widget.*; 

public class SessionFragment extends android.support.v4.app.Fragment {

The symptoms are similar to this problem, but I don't have those mistakes inflating fragments with compatibility package android

The android.compatibility.v4.android-support-v4 jar file is on my build path in Eclipse under Project > Properties > Java build path

I have in the manifest (and I have tried different variations of the name).

In getting started with fragments, I first used API 11 on a honeycomb device, and that all works perfectly. It is in getting the same code (modified to use the slightly different compatibility API) working with the compatibility library that I have this problem. Any ideas would be very welcome. Thanks, Peter

Agrapha answered 1/4, 2011 at 8:21 Comment(0)
D
15

I had the same wrong class problem with the compatibility library and tried everything mentioned above with no luck. I finally figured it out: I was using the incorrect XML element Fragment rather than correct one fragment. Capitalization seems to matter.

Descriptive answered 5/1, 2012 at 7:42 Comment(0)
W
41

I just solved this problem in Android API 8 machine (Samsung Galaxy S).

Please change Activity class to FragmentActivity.

  1. public class FragmentLayout extends Activity {} --> public class FragmentLayout extends FragmentActivity {}

  2. public static class DetailsActivity extends Activity {} --> public static class DetailsActivity extends FragmentActivity {}

  3. finally getFragmentManager() --> getSupportFragmentManager()

  4. register android-support-v4.jar to Eclipse's referenced Libraries

  5. put android-support-v4.jar to {root directory of your project}/libs directory

  6. change to API 10 enum (ex:simple_list_item_1) from simple_list_item_activated_1

  7. import android.support.v4.app.Fragment;

Woll answered 8/4, 2011 at 7:24 Comment(0)
D
15

I had the same wrong class problem with the compatibility library and tried everything mentioned above with no luck. I finally figured it out: I was using the incorrect XML element Fragment rather than correct one fragment. Capitalization seems to matter.

Descriptive answered 5/1, 2012 at 7:42 Comment(0)
J
7

I bet you are trying to call setConetentView before super.onCreate in your FragmentActivity

Jarlathus answered 8/4, 2011 at 8:12 Comment(1)
I was facing the same problem in spite of doing everything right with FragmentActivity etc. Doing super.onCreate() before setContentView solved the problem.Ingratitude
T
2

I had exactly this problem with an app running on pre-Honeycomb devices. I based my Fragment coding on the example given in Dianne Hackborn's February 2011 blog post and other standard Fragment reference material. The solution to the problems is contained in the SDK reference link, http://developer.android.com/sdk/compatibility-library.html#Using. There are minor differences in class and method names that must be used in apps with Fragments, when those apps are to be deployed with the Compatibility library. The link explains this.

The compatibility library, as downloaded and installed, includes example code using Fragments. Ref: Android Developers > Resources > Sample Code > API 4+ Support Demos

Tint answered 13/9, 2011 at 11:34 Comment(0)
T
1

The imports that you have provided all look correct. Do double check any other classes to make sure Eclipse didn't auto import the wrong Fragment some where. The other thing that is key is you need to add the compatibility library to a lib\ that you must create at your project's root to make sure it gets bundled with your app. I don't know if it is required, but I always have my project build path refer to it's copy.

Your layout seems to be missing the class parameter. I think you meant to use class where you have used name in your layout.

<fragment class="com.example.<class to path to your fragment>" android:id="@+id/items"
    android:layout_weight="1" android:layout_width="0px"
    android:layout_height="fill_parent" />

In Eclipse you should see android-support-v4.jar under both Referenced Libraries and libs.

Telemechanics answered 7/4, 2011 at 16:15 Comment(0)
I
0

I think you should delete android-support-v4.jar from build path. This will remove it from referenced libraries in Eclipse. Than creace folder libs and put the jar in there. Clean your project and Eclipse will add the jar to Android dependencies. This did the trick for me as I was struggling with class not found exception.

Impartial answered 10/5, 2012 at 13:17 Comment(0)
A
0

I got the

 java.lang.NoClassDefFoundError: com.android.example.SupportFragment
    at com.android.example.SupportFragmentActivity.onCreate()

on

SupportFragment extends SherlockFragment implements PopupMenu.OnMenuItemClickListener
...
    @Override
    public boolean onMenuItemClick(android.view.MenuItem item) {
        return onOptionsItemSelected(item);
    }

when trying to make a api 17 app compatible with api 8, the only indication was the logcat error above, so check that all your imported classes are supported if you get this error.

Apollonian answered 22/3, 2013 at 11:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.