概述
一、ViewPager报错
1 错误描述
Process: xxx, PID: 11390
android.view.InflateException: Binary XML file line #10 in xxx:layout/fragment_hy_repair_manager: Binary XML file line #10 in xxx:layout/fragment_hy_repair_manager: Error inflating class android.support.v4.view.ViewPager
Caused by: android.view.InflateException: Binary XML file line #10 in xxx:layout/fragment_hy_repair_manager: Error inflating class android.support.v4.view.ViewPager
Caused by: java.lang.ClassNotFoundException: android.support.v4.view.ViewPager
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:454)
at android.view.LayoutInflater.createView(LayoutInflater.java:825)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1016)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:971)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1133)
发生于编译时,报错信息:Error inflating class android.support.v4.view.ViewPager。原项目使用Android Studio老版本(2.3)开发,近期将其升级至最新版本(并顺带更新了androidX),而新版本的.xml文件中不再使用android.support.v4.view.ViewPager包了,需修改为使用androidx.viewpager.widget.ViewPager包。
详见文章 【android】报错Error inflating class android.support.v4.view.ViewPager解决(附完整代码)
2 修改方法
2.1 原代码
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
2.2 新代码
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.viewpager.widget.ViewPager>
二、蓝牙权限更新
1 错误描述
Android X更新后,安卓11版本在原代码的权限下对使用经典蓝牙的设备有影响,无法连接与扫描,需修改Mainfest.xml权限与权限确认的Activity代码。
2 修改方法
2.1 Mainfest.xml文件
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- 更新androidX后新增权限 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" android:usesPermissionFlags="neverForLocation" tools:targetApi="s" />
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
2.2 权限确认Activity文件
private void checkPermission() {
List<String> mPermissionList = new ArrayList<>();
boolean isOK = true;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) {
mPermissionList.add(Manifest.permission.BLUETOOTH_SCAN);
isOK = false;
}
if (ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_ADVERTISE) != PackageManager.PERMISSION_GRANTED) {
mPermissionList.add(Manifest.permission.BLUETOOTH_ADVERTISE);
isOK = false;
}
if (ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
mPermissionList.add(Manifest.permission.BLUETOOTH_CONNECT);
isOK = false;
}
if (ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) {
mPermissionList.add(Manifest.permission.BLUETOOTH_SCAN);
isOK = false;
}
} else {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
mPermissionList.add(Manifest.permission.ACCESS_FINE_LOCATION);
isOK = false;
}
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
mPermissionList.add(Manifest.permission.ACCESS_COARSE_LOCATION);
isOK = false;
}
if (!isOK) {
mPermissionList.add(Manifest.permission.BLUETOOTH);
mPermissionList.add(Manifest.permission.BLUETOOTH_ADMIN);
}
}
if (isOK) {
setup();
}else{
String[] permissions = mPermissionList.toArray(new String[mPermissionList.size()]);
ActivityCompat.requestPermissions(this, permissions, REQUEST_ACCESS_COARSE_LOCATION);
}
}
三、PictureSelector照片不显示
1 错误描述
Android X更新后,使用PictureSelector时照片全部为空白图片,点击预览同样无法查看,选择并确认后在UI上可以显示。
2 修改方法
2.1 原代码
static public void openCamera(final Activity context, int count, boolean enableCrop, int cropX, int cropY) {
PictureSelector.create(context)
.openCamera(PictureMimeType.ofImage())
.imageEngine(GlideEngine.createGlideEngine())
.maxSelectNum(count)
.minSelectNum(1)
.previewEggs(false)
.enableCrop(true)
.withAspectRatio(cropX,cropY)
.minimumCompressSize(100)
.forResult(PictureConfig.CHOOSE_REQUEST);
}
2.2 新代码
static public void openCamera(final Activity context, int count, boolean enableCrop, int cropX, int cropY) {
PictureSelector.create(context)
.openCamera(PictureMimeType.ofImage())
.imageEngine(GlideEngine.createGlideEngine())
.maxSelectNum(count)
.minSelectNum(1)
.previewEggs(false)
.enableCrop(true)
.withAspectRatio(cropX,cropY)
.minimumCompressSize(100)
.loadImageEngine(GlideEngine.createGlideEngine())
.forResult(PictureConfig.CHOOSE_REQUEST);
}
四、获取图片路径错误(上传图片失败)
1 错误描述
Android X更新后,原先批量选中照片,并通过各照片getPath方法循环获取并new file的方法失效,原因在于getPath方法获取路径信息有误,需修改为getRealPath方法。
2 修改方法
2.1 原代码
for (LocalMedia media : selectList) {
imagePaths.add(media.getPath());
}
2.2 新代码
for (LocalMedia media : selectList) {
imagePaths.add(media.getRealPath());
}
最后
以上就是孤独花瓣为你收集整理的【android】项目升级Android X带来的问题解决方式的全部内容,希望文章能够帮你解决【android】项目升级Android X带来的问题解决方式所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复