Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AppCompat and Fragment not working

02-19 11:49:17.369: E/AndroidRuntime(4209): java.lang.NoClassDefFoundError: com.slidingmenus.fragments.HomeFragment
02-19 11:49:17.369: E/AndroidRuntime(4209):     at com.slidingmenus.MainActivity.displayCategoryView(MainActivity.java:242)
02-19 11:49:17.369: E/AndroidRuntime(4209):     at com.slidingmenus.MainActivity.onCreate(MainActivity.java:121)

Tried each and every steps suggested in stack overflow but they didn't help.

It works without an error in 4.0+ but in 2.3.x devices its giving java.lang.NoClassDefFoundError in

line 242: fragment = new HomeFragment();

My imports from fragments are:

import android.app.Fragment;
import android.app.FragmentManager;

and I'm using:

FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();

Here is my home fragment:

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;   

public class HomeFragment extends Fragment {        
   public HomeFragment(){}

   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
           Bundle savedInstanceState) {

           View rootView = inflater.inflate(R.layout.layout_main_fragment,container, false);

           return rootView;
    }
 }

Spent whole morning already trying to solve this. still no luck. Any help is highly appreciated.

Thanks

like image 850
prashantwosti Avatar asked Feb 19 '14 06:02

prashantwosti


People also ask

What is AppCompat in Android?

AppCompat (aka ActionBarCompat) started out as a backport of the Android 4.0 ActionBar API for devices running on Gingerbread, providing a common API layer on top of the backported implementation and the framework implementation. AppCompat v21 delivers an API and feature-set that is up-to-date with Android 5.0.

Should I use activity or AppCompatActivity?

Unless some third-party library you are using insists upon an ActionBarActivity , you should prefer AppCompatActivity over ActionBarActivity . So, given your minSdkVersion in the 15-16 range: If you want the backported Material Design look, use AppCompatActivity.

What is Androidx fragment?

fragment:fragment. Official Description: The Support Library is a static library that you can add to your Android application in order to use APIs that are either not available for older platform versions or utility APIs that aren't a part of the framework APIs. Compatible on devices running API 14 or later.


3 Answers

You should use Fragment from the support library.

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

You also need to use getSupportFragmentManager() and since you are using AppCompat your activity must extend ActionbarActivity.

 FragmentManager fragmentManager = getSupportFragmentManager();

Update:

ActionBarActivity is deprecated use AppCompatActivity from support library. Don't forget to update your support repository to the latest.

like image 148
Raghunandan Avatar answered Oct 17 '22 07:10

Raghunandan


Try this code Import:

import android.support.v4.app.Fragment;

And use

    Fragment fragment = new HomeFragment();
    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.layout.layout_main_fragment, fragment).commit();
like image 23
LONGMAN Avatar answered Oct 17 '22 06:10

LONGMAN


I faced the same issue and resolved it by import support library and used

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

Instead

import android.app.Fragment;
import android.app.FragmentManager;
like image 36
M D Avatar answered Oct 17 '22 05:10

M D