onCreateView 中的 mainLayout 不适用于 4.4 及更低版本
mainLayout in onCreateView is not working for 4.4 & lower
我的一个项目中有以下内容
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
try {
Log.d("errrrr", "making mainlayout-1");
mainLayout = (RelativeLayout) inflater.inflate(R.layout.home, null);
Log.d("errrrr", "making mainlayout-1.1");
我在 inStart 中也有以下内容
if (mainLayout==null) {
Log.d("errrr", "22--mainlayout is not okay");
} else {
Log.d("errrr", "33--mainlayout is okay");
}
mloading.setVisibility(View.VISIBLE);
下面是 Android 4.2
的输出
D/errrrr: making mainlayout-1
D/errrr: 22--mainlayout is okay
现在由于 mainLayout 为 null,mloading.setVisibility(View.VISIBLE);
正在崩溃调用 NullPointerException。
这是因为在 createView 中我有下面的内容。
mloading = (ProgressBar) mainLayout.findViewById(R.id.loading);
但由于 mainLayout
为 null,因此未定义任何内容。
知道出了什么问题吗?
注意:这适用于 OS 5.0 及更高版本
编辑 1
以下是例外
E/HomeFragment onCreate>>LineNumber: 162: 二进制 XML 文件行 #14: 膨胀错误 class org.lucasr.twowayview.TwoWayView
Home.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/rel_cats"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#ff343431">
<org.lucasr.twowayview.TwoWayView
android:id="@+id/lst_newsCats"
style="@style/TwoWayView"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_gravity="right"
android:background="#ff343431"
android:choiceMode="singleChoice"
android:drawSelectorOnTop="false"
android:fadingEdge="none"
android:gravity="right"
android:layoutDirection="rtl" />
</RelativeLayout>
<ScrollView
android:id="@+id/scrollView_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/pager_urgent"
android:layout_below="@+id/rel_cats"
android:layout_marginTop="1dp"
android:fadingEdge="none">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<cn.trinea.android.view.autoscrollviewpager.AutoScrollViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@drawable/img_slider"
android:fadingEdge="none"
android:visibility="invisible" />
<com.viewpagerindicator.CirclePageIndicator
android:id="@+id/indicator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/pager"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:background="@android:color/transparent"
android:gravity="center"
android:visibility="invisible"
app:centered="true"
app:fillColor="#ff9e9e9e"
app:pageColor="#ffeaeaea"
app:radius="5dp"
app:snap="true" />
<com.abc.app.classes.LoadMoreListView
android:id="@+id/lst_mainNews"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/indicator"
android:layout_marginTop="10dp"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="20dp"
android:fadingEdge="none"
android:visibility="invisible" />
<TextView
android:id="@+id/xcv"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@+id/lst_mainNews"
android:gravity="center"
android:text="Load More"
android:visibility="gone" />
</RelativeLayout>
</ScrollView>
<cn.trinea.android.view.autoscrollviewpager.AutoScrollViewPager
android:layoutDirection="rtl"
android:layout_marginBottom="2dp"
android:id="@+id/pager_urgent"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#E41F1F"
android:visibility="gone"
android:fadingEdge="none" />
<ProgressBar
android:id="@+id/loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center_horizontal"
android:indeterminateDrawable="@drawable/costum_progress_bar"
android:visibility="gone" />
</RelativeLayout>
编辑 2
gradle 文件
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
packagingOptions {
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/DEPENDENCIES.txt'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/notice.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/dependencies.txt'
exclude 'META-INF/LGPL2.1'
}
defaultConfig {
applicationId "com.abc.app"
minSdkVersion 17
targetSdkVersion 22
versionCode 5
versionName "5.0"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
multiDexEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile files('libs/gcm.jar')
compile files('libs/gson-2.2.4.jar')
compile files('libs/gson-2.2.4-javadoc.jar')
compile files('libs/gson-2.2.4-sources.jar')
compile files('libs/nineoldandroids-2.4.0.jar')
compile files('libs/PhotoView.jar')
compile files('libs/picasso.jar')
compile files('libs/universal-image-loader-1.8.6.jar')
compile files('libs/urlimageviewhelper-1.0.4.jar')
compile files('libs/volley.jar')
compile('cn.trinea.android.view.autoscrollviewpager:android-auto-scroll-view-pager:1.1.2') {
exclude module: 'support-v4'
}
compile files('libs/httpclient-4.3.2.jar')
compile files('libs/httpclient-cache-4.3.1.jar')
compile files('libs/httpcore-4.3.2.jar')
compile files('libs/httpcore-nio-4.3.2.jar')
compile files('libs/httpmime-4.3.1.jar')
compile files('libs/twitter4j-core-4.0.4.jar')
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.facebook.android:facebook-android-sdk:4.1.0'
compile 'com.twotoasters.jazzylistview:library:1.2.1'
compile 'com.twotoasters.jazzylistview:library-recyclerview:1.2.1'
compile 'com.google.android.gms:play-services:8.3.0'
compile 'eu.inmite.android.lib:android-styled-dialogs:1.2.0'
compile 'com.daimajia.easing:library:1.0.1@aar'
compile 'com.daimajia.androidanimations:library:1.1.3@aar'
compile 'com.viewpagerindicator:library:2.4.1@aar'
compile 'com.daimajia.slider:library:1.1.2@aar'
compile 'it.sephiroth.android.library.horizontallistview:hlistview:1.2.2'
compile 'org.lucasr.twowayview:twowayview:0.1.4'
compile 'com.android.support:support-v4:22.2.1'
}
这是已知的 issue:
尝试改变:
compile 'com.android.support:support-v4:22.2.1'
至:
compile 'com.android.support:support-v4:22.1.1'
我的一个项目中有以下内容
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
try {
Log.d("errrrr", "making mainlayout-1");
mainLayout = (RelativeLayout) inflater.inflate(R.layout.home, null);
Log.d("errrrr", "making mainlayout-1.1");
我在 inStart 中也有以下内容
if (mainLayout==null) {
Log.d("errrr", "22--mainlayout is not okay");
} else {
Log.d("errrr", "33--mainlayout is okay");
}
mloading.setVisibility(View.VISIBLE);
下面是 Android 4.2
的输出D/errrrr: making mainlayout-1
D/errrr: 22--mainlayout is okay
现在由于 mainLayout 为 null,mloading.setVisibility(View.VISIBLE);
正在崩溃调用 NullPointerException。
这是因为在 createView 中我有下面的内容。
mloading = (ProgressBar) mainLayout.findViewById(R.id.loading);
但由于 mainLayout
为 null,因此未定义任何内容。
知道出了什么问题吗?
注意:这适用于 OS 5.0 及更高版本
编辑 1
以下是例外
E/HomeFragment onCreate>>LineNumber: 162: 二进制 XML 文件行 #14: 膨胀错误 class org.lucasr.twowayview.TwoWayView
Home.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/rel_cats"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#ff343431">
<org.lucasr.twowayview.TwoWayView
android:id="@+id/lst_newsCats"
style="@style/TwoWayView"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_gravity="right"
android:background="#ff343431"
android:choiceMode="singleChoice"
android:drawSelectorOnTop="false"
android:fadingEdge="none"
android:gravity="right"
android:layoutDirection="rtl" />
</RelativeLayout>
<ScrollView
android:id="@+id/scrollView_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/pager_urgent"
android:layout_below="@+id/rel_cats"
android:layout_marginTop="1dp"
android:fadingEdge="none">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<cn.trinea.android.view.autoscrollviewpager.AutoScrollViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@drawable/img_slider"
android:fadingEdge="none"
android:visibility="invisible" />
<com.viewpagerindicator.CirclePageIndicator
android:id="@+id/indicator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/pager"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:background="@android:color/transparent"
android:gravity="center"
android:visibility="invisible"
app:centered="true"
app:fillColor="#ff9e9e9e"
app:pageColor="#ffeaeaea"
app:radius="5dp"
app:snap="true" />
<com.abc.app.classes.LoadMoreListView
android:id="@+id/lst_mainNews"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/indicator"
android:layout_marginTop="10dp"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="20dp"
android:fadingEdge="none"
android:visibility="invisible" />
<TextView
android:id="@+id/xcv"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@+id/lst_mainNews"
android:gravity="center"
android:text="Load More"
android:visibility="gone" />
</RelativeLayout>
</ScrollView>
<cn.trinea.android.view.autoscrollviewpager.AutoScrollViewPager
android:layoutDirection="rtl"
android:layout_marginBottom="2dp"
android:id="@+id/pager_urgent"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#E41F1F"
android:visibility="gone"
android:fadingEdge="none" />
<ProgressBar
android:id="@+id/loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center_horizontal"
android:indeterminateDrawable="@drawable/costum_progress_bar"
android:visibility="gone" />
</RelativeLayout>
编辑 2
gradle 文件
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
packagingOptions {
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/DEPENDENCIES.txt'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/notice.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/dependencies.txt'
exclude 'META-INF/LGPL2.1'
}
defaultConfig {
applicationId "com.abc.app"
minSdkVersion 17
targetSdkVersion 22
versionCode 5
versionName "5.0"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
multiDexEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile files('libs/gcm.jar')
compile files('libs/gson-2.2.4.jar')
compile files('libs/gson-2.2.4-javadoc.jar')
compile files('libs/gson-2.2.4-sources.jar')
compile files('libs/nineoldandroids-2.4.0.jar')
compile files('libs/PhotoView.jar')
compile files('libs/picasso.jar')
compile files('libs/universal-image-loader-1.8.6.jar')
compile files('libs/urlimageviewhelper-1.0.4.jar')
compile files('libs/volley.jar')
compile('cn.trinea.android.view.autoscrollviewpager:android-auto-scroll-view-pager:1.1.2') {
exclude module: 'support-v4'
}
compile files('libs/httpclient-4.3.2.jar')
compile files('libs/httpclient-cache-4.3.1.jar')
compile files('libs/httpcore-4.3.2.jar')
compile files('libs/httpcore-nio-4.3.2.jar')
compile files('libs/httpmime-4.3.1.jar')
compile files('libs/twitter4j-core-4.0.4.jar')
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.facebook.android:facebook-android-sdk:4.1.0'
compile 'com.twotoasters.jazzylistview:library:1.2.1'
compile 'com.twotoasters.jazzylistview:library-recyclerview:1.2.1'
compile 'com.google.android.gms:play-services:8.3.0'
compile 'eu.inmite.android.lib:android-styled-dialogs:1.2.0'
compile 'com.daimajia.easing:library:1.0.1@aar'
compile 'com.daimajia.androidanimations:library:1.1.3@aar'
compile 'com.viewpagerindicator:library:2.4.1@aar'
compile 'com.daimajia.slider:library:1.1.2@aar'
compile 'it.sephiroth.android.library.horizontallistview:hlistview:1.2.2'
compile 'org.lucasr.twowayview:twowayview:0.1.4'
compile 'com.android.support:support-v4:22.2.1'
}
这是已知的 issue:
尝试改变:
compile 'com.android.support:support-v4:22.2.1'
至:
compile 'com.android.support:support-v4:22.1.1'