我对这个案子一整天都很疯狂。
这是MyPagerAdapter类
/**
* Created by ram on 5/9/16.
*/
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import com.bidhee.familyhealthnepal.fragment.TabFragmentDetail;
import com.bidhee.familyhealthnepal.fragment.TabFragmentFollowUp;
import com.bidhee.familyhealthnepal.fragment.TabFragmentImaging;
import com.bidhee.familyhealthnepal.fragment.TabFragmentLabTest;
/**
* Created by ronem on 2/9/16.
*/
public class MyPagerAdapter extends FragmentStatePagerAdapter {
String[] tabs;
public MyPagerAdapter(FragmentManager fm, String[] tabs) {
super(fm);
this.tabs = tabs;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return TabFragmentDetail.newInstance();
case 1:
return TabFragmentImaging.newInstance();
case 2:
return TabFragmentLabTest.newInstance();
case 3:
return TabFragmentFollowUp.newInstance();
default:
return null;
}
}
@Override
public int getCount() {
return tabs.length;
}
@Override
public CharSequence getPageTitle(int position) {
return tabs[position];
}
}
这是PagerActivity.java的pager_activity_layout.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<include layout="@layout/toolbar" />
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="?attr/colorPrimary" />
<android.support.v4.view.ViewPager
android:id="@+id/all_news_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
这是我的PagerActivity.java
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import com.bidhee.familyhealthnepal.R;
import com.bidhee.familyhealthnepal.util.AppText;
import com.bidhee.familyhealthnepal.widget.adapter.MyPagerAdapter;
import java.util.Arrays;
import butterknife.Bind;
import butterknife.ButterKnife;
/**
* Created by ram on 5/9/16.
*/
public class PagerActivity extends AppCompatActivity {
@Bind(R.id.toolbar)
Toolbar toolbar;
@Bind(R.id.tab_layout)
TabLayout tabLayout;
@Bind(R.id.all_news_viewpager)
ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pager_activity_layout);
ButterKnife.bind(this);
setToolbar();
setViewPager();
setTabLayout();//problem in this line
}
private void setToolbar() {
setSupportActionBar(toolbar);
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.admission_history_color)));
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
private void setViewPager() {
String[] array = AppText.Tab.getTabs(AppText.Tab.ADMINSSION_HISTORY);
Log.i("array", Arrays.toString(array));
MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager(), array);
viewPager.setAdapter(adapter);
}
private void setTabLayout() {
tabLayout.setupWithViewPager(viewPager);
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) {
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
ButterKnife.unbind(this);
}
}
在上面的代码中每当我在onCreate()方法中使用函数 setTabLayout(); 注释该行时 view pager工作正常但没有在tablayout中显示任何文本。当我取消评论时 setTabLayout(); 我收到了这些错误
05-09 19:11:14.480 18543-18543/com.bidhee.familyhealthnepal E/AndroidRuntime: FATAL EXCEPTION: main
05-09 19:11:14.480 18543-18543/com.bidhee.familyhealthnepal E/AndroidRuntime: Process: com.bidhee.familyhealthnepal, PID: 18543
05-09 19:11:14.480 18543-18543/com.bidhee.familyhealthnepal E/AndroidRuntime: java.lang.NoClassDefFoundError: android.support.v7.widget.TintManager
05-09 19:11:14.480 18543-18543/com.bidhee.familyhealthnepal E/AndroidRuntime: at android.support.design.widget.TabLayout$TabView.<init>(TabLayout.java:1185)
05-09 19:11:14.480 18543-18543/com.bidhee.familyhealthnepal E/AndroidRuntime: at android.support.design.widget.TabLayout.createTabView(TabLayout.java:656)
05-09 19:11:14.480 18543-18543/com.bidhee.familyhealthnepal E/AndroidRuntime: at android.support.design.widget.TabLayout.addTabView(TabLayout.java:695)
05-09 19:11:14.480 18543-18543/com.bidhee.familyhealthnepal E/AndroidRuntime: at android.support.design.widget.TabLayout.addTab(TabLayout.java:386)
05-09 19:11:14.480 18543-18543/com.bidhee.familyhealthnepal E/AndroidRuntime: at android.support.design.widget.TabLayout.addTab(TabLayout.java:361)
05-09 19:11:14.480 18543-18543/com.bidhee.familyhealthnepal E/AndroidRuntime: at android.support.design.widget.TabLayout.setTabsFromPagerAdapter(TabLayout.java:645)
05-09 19:11:14.480 18543-18543/com.bidhee.familyhealthnepal E/AndroidRuntime: at android.support.design.widget.TabLayout.setupWithViewPager(TabLayout.java:616)
05-09 19:11:14.480 18543-18543/com.bidhee.familyhealthnepal E/AndroidRuntime: at com.bidhee.familyhealthnepal.activity.PagerActivity.setTabLayout(PagerActivity.java:61)
05-09 19:11:14.480 18543-18543/com.bidhee.familyhealthnepal E/AndroidRuntime: at com.bidhee.familyhealthnepal.activity.PagerActivity.onCreate(PagerActivity.java:40)
05-09 19:11:14.480 18543-18543/com.bidhee.familyhealthnepal E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5322)
05-09 19:11:14.480 18543-18543/com.bidhee.familyhealthnepal E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
05-09 19:11:14.480 18543-18543/com.bidhee.familyhealthnepal E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2303)
05-09 19:11:14.480 18543-18543/com.bidhee.familyhealthnepal E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
05-09 19:11:14.480 18543-18543/com.bidhee.familyhealthnepal E/AndroidRuntime: at android.app.ActivityThread.access$900(ActivityThread.java:166)
05-09 19:11:14.480 18543-18543/com.bidhee.familyhealthnepal E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
05-09 19:11:14.480 18543-18543/com.bidhee.familyhealthnepal E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
05-09 19:11:14.480 18543-18543/com.bidhee.familyhealthnepal E/AndroidRuntime: at android.os.Looper.loop(Looper.java:146)
05-09 19:11:14.480 18543-18543/com.bidhee.familyhealthnepal E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5511)
05-09 19:11:14.480 18543-18543/com.bidhee.familyhealthnepal E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
05-09 19:11:14.480 18543-18543/com.bidhee.familyhealthnepal E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
05-09 19:11:14.480 18543-18543/com.bidhee.familyhealthnepal E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
05-09 19:11:14.480 18543-18543/com.bidhee.familyhealthnepal E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
05-09 19:11:14.480 18543-18543/com.bidhee.familyhealthnepal E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
我在网上搜索了这种错误,大多数答案都说支持库的版本应该相同。 这是我的依赖
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.android.support:recyclerview-v7:23.1.1'
compile 'com.android.support:cardview-v7:23.1.1'
答案 0 :(得分:1)
将依赖项23.1.1的版本更改为23.0.3
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.yourapp.todo"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:design:23.3.0'
}