java.lang.RuntimeException: Unable to start activity ComponentInfo{urlinq.android.com.edu_chat_lollipop/urlinq.android.com.edu_chat.LoginActivity}: android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class android.support.v4.view.ViewPager
重新安裝Android Studio後仍然收到此消息。不知道這裏究竟發生了什麼,但是在更新minSDK版本後我收到了這條消息。LayoutInflate異常與ViewPager
我有另一個用戶誰也使用相同的文件,他的應用程序能夠運行。這是運行ViewPager的片段。在活動中定義的ViewPager背景旁邊有一個紅色感嘆號。
private ViewPager mViewPager;
private PagerAdapter mCustomPagerAdapter;
private ImageButton signUpBtnToggle;
private ImageButton loginBtnToggle;
private ImageButton loginBtn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
/**
* Load the login_main.xml layout.
* In addition, load the ViewPager.
*/
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//initialize viewpager object.
View v = inflater.inflate(R.layout.login_main, container, false);
mViewPager = (ViewPager) v.findViewById(R.id.loginPager);
mViewPager.setAdapter(new LoginAdapter(getActivity().getSupportFragmentManager()));
final LayerDrawable background = (LayerDrawable) mViewPager.getBackground();
background.getDrawable(0).setAlpha(0);
//lowest drawable
background.getDrawable(1).setAlpha(0);
background.getDrawable(2).setAlpha(1); //outermost drawable.
mViewPager.setPageTransformer(true, new ViewPager.PageTransformer() {
@Override
public void transformPage(View view, float position) {
int index = (Integer) view.getTag();
Drawable currentDrawableInLayerDrawable;
currentDrawableInLayerDrawable = background.getDrawable(index);
// Change the visibility of each of the fragment layers when swiping left and right.
if (position <= -1 || position >= 1) {
currentDrawableInLayerDrawable.setAlpha(0);
} else if (position == 0) {
currentDrawableInLayerDrawable.setAlpha(255);
} else {
currentDrawableInLayerDrawable.setAlpha((int) (255 - Math.abs(position * 255)));
}
}
});
return v;
}
Login_Drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap
android:id="@+id/Idofbg3"
android:gravity="fill"
android:src="@drawable/welcome" />
</item>
<item>
<bitmap
android:id="@+id/Idofbg2"
android:gravity="fill"
android:src="@drawable/welcome2" />
</item>
<item>
<bitmap
android:id="@+id/Idofbg1"
android:gravity="fill"
android:src="@drawable/welcome3" />
</item>
</layer-list>
的build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion '23.0.1'
defaultConfig {
applicationId "urlinq.android.com.edu_chat_lollipop"
minSdkVersion 22
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
repositories {
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots/'
}
}
dependencies {
compile 'com.loopj.android:android-async-http:1.4.9'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:design:23.0.1'
}
login_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true">
<android.support.v4.view.ViewPager
android:id="@+id/loginPager"
android:layout_width="match_parent"
android:layout_height="340dp"
android:background = "@drawable/login_drawable"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true" />
<fragment
android:layout_width="match_parent"
android:layout_height="285dp"
android:name="urlinq.android.com.edu_chat.SignUpFragment"
android:id="@+id/fragment"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
tools:layout="@layout/combined_login"
android:layout_alignParentBottom="true" />
<ImageView
android:layout_width="150dp"
android:layout_height="65dp"
android:id="@+id/eduChatLogo"
android:background="@drawable/chatlogo"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp" />
</RelativeLayout>
請提供login_main.xml佈局文件 – Dharmaraj
@Dharmaraj完成。 –