我正在关注此tutorial to create swipe tabs。但我得到了上述错误。
我的gradle依赖关系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'
}
这是我的档案。 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) {
}
});
}
}
我的PageAdapter.java
文件是
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;
}
}
这就是LogCat给我的东西
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
如何解决此错误?
答案 0 :(得分:24)
支持库和设计库的版本应该相同。
我改变了我的
compile 'com.android.support:appcompat-v7:23.1.1'
到
compile 'com.android.support:appcompat-v7:23.0.1'
并为我解决问题。
注意:我的compileSdkVersion和buildToolVersion如下所示 compileSdkVersion 23 buildToolsVersion“23.0.1”
答案 1 :(得分:19)
当一个类在编译时存在而不是在运行时出现时,右键单击app
转到open module settings
选择dependencies
点击+
然后选择“库依赖项”选择design
并点击ok
,等待gradle运行,重新运行您的应用,相信它会解决它。
你的顶级gradle需要看起来像这样
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'
}
答案 2 :(得分:3)
您必须导入相同的appcompat和设计库以及12.2.0版本。
我通过导入这些库来修复它。
compile 'com.android.support:appcompat-v7:25.2.0'
compile 'com.android.support:design:25.2.0'
compile 'com.android.support:recyclerview-v7:25.2.0'
compile 'com.android.support:support-v4:25.2.0'
compile 'com.android.support:cardview-v7:25.2.0'
答案 3 :(得分:0)
你只需添加: 编译'com.android.support:design:22.2.0'或高版本: 编译'com.android.support:design:25.0.0'很不错。
支持:设计包含支持:appcompat。