Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error : java.lang.NoClassDefFoundError Android

Tags:

android

gradle

I am following this tutorial to create swipe tabs. But I get the above error.

My gradle dependencies sturucture

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:22.2.0'
}

Here are my files. MainActivity.java

import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

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

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    tabLayout.addTab(tabLayout.newTab().setText("Tab 1")); //this line gives error
    tabLayout.addTab(tabLayout.newTab().setText("Tab 2")); //this line gives error
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    PageAdapter pageAdapter = new PageAdapter(getSupportFragmentManager());
    viewPager.setAdapter(pageAdapter);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });
 }

}

And my PageAdapter.java file is

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

public class PageAdapter extends FragmentStatePagerAdapter {

  public PageAdapter(FragmentManager fm) {
    super(fm);
  }

  @Override
  public Fragment getItem(int position) {

    switch (position) {
        case 0:
            return new InstaPicSwipeTab();
        case 1:
            return new SongsDownloadSwipeTab();
    }
    return null;
 }

 @Override
 public int getCount() {
    return 2;
 }
}

And this is what LogCat is giving me

12-08 00:27:24.452 11770-11770/com.example.suraj.freewee E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: com.example.suraj.freewee, PID: 11770
                                                                           java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v7/internal/widget/TintManager;
                                                                               at android.support.design.widget.TabLayout$TabView.<init>(TabLayout.java:1041)
                                                                               at android.support.design.widget.TabLayout.createTabView(TabLayout.java:582)
                                                                               at android.support.design.widget.TabLayout.addTabView(TabLayout.java:616)
                                                                               at android.support.design.widget.TabLayout.addTab(TabLayout.java:334)
                                                                               at android.support.design.widget.TabLayout.addTab(TabLayout.java:309)
                                                                               at com.example.suraj.freewee.MainActivity.onCreate(MainActivity.java:23)
                                                                               at android.app.Activity.performCreate(Activity.java:6237)
                                                                               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                               at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                               at android.os.Looper.loop(Looper.java:148)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                            Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v7.internal.widget.TintManager" on path: DexPathList[[zip file "/data/app/com.example.suraj.freewee-1/base.apk"],nativeLibraryDirectories=[/data/app/com.example.suraj.freewee-1/lib/x86, /vendor/lib, /system/lib]]
                                                                               at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                                                                               at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
                                                                               at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
                                                                               at android.support.design.widget.TabLayout$TabView.<init>(TabLayout.java:1041) 
                                                                               at android.support.design.widget.TabLayout.createTabView(TabLayout.java:582) 
                                                                               at android.support.design.widget.TabLayout.addTabView(TabLayout.java:616) 
                                                                               at android.support.design.widget.TabLayout.addTab(TabLayout.java:334) 
                                                                               at android.support.design.widget.TabLayout.addTab(TabLayout.java:309) 
                                                                               at com.example.suraj.freewee.MainActivity.onCreate(MainActivity.java:23) 
                                                                               at android.app.Activity.performCreate(Activity.java:6237) 
                                                                               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                               at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                               at android.os.Looper.loop(Looper.java:148) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                                            Suppressed: java.lang.ClassNotFoundException: android.support.v7.internal.widget.TintManager
                                                                               at java.lang.Class.classForName(Native Method)
                                                                               at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
                                                                               at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
                                                                               at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
                                                                                    ... 19 more
                                                                            Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available

How to solve this error?

like image 815
sap Avatar asked Dec 07 '15 19:12

sap


People also ask

How do I resolve error in Java Lang NoClassDefFoundError?

lang. NoClassDefFoundError, which means the Class Loader file responsible for dynamically loading classes can not find the . class file. So to remove this error, you should set your classpath to the location where your Class Loader is present.

What causes Java Lang NoClassDefFoundError?

java. lang. NoClassDefFoundError is runtime error thrown when a required class is not found in the classpath and hence JVM is unable to load it into memory.

Can we catch NoClassDefFoundError in Java?

Java runtime throws ClassNotFoundException while trying to load a class at runtime only and the name was provided during runtime. In the case of NoClassDefFoundError, the class was present at compile time, but Java runtime could not find it in Java classpath during runtime.


2 Answers

The version of support library and design library should be the same.

I changed my

compile 'com.android.support:appcompat-v7:23.1.1'

to

compile 'com.android.support:appcompat-v7:23.0.1'

and issue fixed for me.

Note: my compileSdkVersion and buildToolVersion is below compileSdkVersion 23 buildToolsVersion "23.0.1"

like image 143
Developine Avatar answered Oct 22 '22 13:10

Developine


this comes up when a class exists at compile time but not at runtime, right click on the app go to open module settings select dependencies click on the + and select 'library dependency' select design and click ok, wait for gradle to run, re-run your app, am sure that will fix it.

your top level gradle will need to look like this

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion '23.0.2'

defaultConfig {
    applicationId "aubry.chromio.com.dressup"
    minSdkVersion 15
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"

}


dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile fileTree(dir: 'libs', include: ['*.java'])
    compile fileTree(dir: 'libs', include: ['*.so'])


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

}
like image 43
life evader Avatar answered Oct 22 '22 14:10

life evader