1. Attempt to invoke virtual method 'int android.view.View.getImportantForAccessibility()' on a null object reference
使用listview的时候getView()返回的参数为 null 时报该错误
2.vector android:fillType gradient android:endX attribute not found
compileSdkVersion最低要24
3.android.os.NetworkOnMainThreadException
原因:Android 4.0以上,网络连接不能放在主线程上,不然就会报错android.os.NetworkOnMainThreadException。但是4.0下版本可以不会报错。
Android连接网络异常:android.os.NetworkOnMainThreadException
package com.ccl.getimage;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText et_path;
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_path = (EditText) findViewById(R.id.et_path);
iv = (ImageView) findViewById(R.id.iv);
}
public void getImage(View view){
String path = et_path.getText().toString().trim();
if(TextUtils.isEmpty(path)){
Toast.makeText(this, "地址不能为空", 0).show();
return;
}
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
System.out.println("响应码是--"+conn.getResponseCode());
if(conn.getResponseCode()==200){
//获取服务器返回的流数据
InputStream in = conn.getInputStream();
//将返回的流数据解析成图片
Bitmap bitmap = BitmapFactory.decodeStream(in);
//显示图片
iv.setImageBitmap(bitmap);
in.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Android模拟器版本4.1.2
异常解释:在主线程中的网络异常
原因:Android2.3版本后不允许在主线程中直接请求网络获取数据
解决方法(两种):
一:onCreate方法中加入如下代码(不推荐使用,有可能阻塞Android主线程)
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
二、启动一条子线程进行你的网络请求,推荐使用这种
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection urlcon = null;
try {
URL u = new URL(url);
urlcon = (HttpURLConnection) u.openConnection();
} catch (IOException e) {
e.printStackTrace();
}
int fileLength = urlcon.getContentLength();
tvRight.setText(""+fileLength);
seekBar.setProgress(fileLength);
}
}).start();
4、ERROR: Manifest merger failed : Attribute application@appComponentFactory value=(android.support.v4.app.CoreComponentFactory) from [com.android.support:support-compat:28.0.0] AndroidManifest.xml:22:18-91
is also present at [androidx.core:core:1.0.1] AndroidManifest.xml:22:18-86 value=(androidx.core.app.CoreComponentFactory).
Suggestion: add 'tools:replace="android:appComponentFactory"' to <application> element at AndroidManifest.xml:5:5-19:19 to override.
我是在使用android 组件 Exoplayer 的2.10.4版本时候遇到的问题 ,查资料发现是 AndroidX和Android support 共存导致
解决:
、按照日志提示
添加tools:replace="android:appComponentFactory" 这个没用
方法一、最终还是这样解决的:
在根项目build.gradle文件中添加
中间subprojects中的这一段
buildscript { repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.4.1' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } subprojects { project.configurations.all { resolutionStrategy.eachDependency { details -> if (details.requested.group == 'com.android.support' && !details.requested.name.contains('multidex') ) { details.useVersion "27.1.1" } if (details.requested.group == 'androidx.core' && !details.requested.name.contains('androidx') ) { details.useVersion "1.0.1" } } } } }
方法二、升级你的库为androidX(下面方法只适用于Android Studio 3.2及更高版本)
手动升级可参考:http://www.anddaily.com/index.php/2019/01/17/androidx%E4%BA%86%E8%A7%A3%E4%B8%80%E4%B8%8B/
1,使用Android Studio 3.2及更高版本,可以通过从菜单栏中选择Refactor> Migrate to AndroidX,快速迁移现有项目以使用AndroidX 。
2,勾选Backup project as Zip file 选项(即:在开始迁移之前备份项目的选项),点击Migrate备份项目代码,一路下一步;中间会涉及选择存储路径,选择备份项目的存储路径即可;我建立了一个androidXBackUp用来保存自己迁移之前的文件
3、运行Migrate to AndroidX的重构预览窗口,点击Do Refactor迁移;