android杂记
1、ArrayAdapter requires the resource ID to be a TextView问题
listView.setAdapter(new ArrayAdapter<String>( this,R.layout.textlayout, strs ));
布局容易出现:ArrayAdapter requires the resource ID to be a TextView 报错
textlayout.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:orientation="vertical" > <TextView android:id="@+id/text1" android:textSize="16sp" android:textStyle="bold" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout>
问题出在:
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/text1" android:textSize="16sp" android:textStyle="bold" android:layout_width="fill_parent" android:layout_height="wrap_content"/>
xmlns:android=http://schemas.android.com/apk/res/android 就是这句了....
添加到自己的TextView中就ok.
因为根节点必须是TextView
2、Cannot resolve symbol HttpGet,HttpClient,HttpResponce in Android Studio
处理方式:
在依赖配置中添加如下代码:
compile 'org.apache.httpcomponents:httpcore:4.4.1'
compile 'org.apache.httpcomponents:httpclient:4.5'
最终结果:
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' compile 'org.apache.httpcomponents:httpcore:4.4.1' compile 'org.apache.httpcomponents:httpclient:4.5' }
然后添加:
android { useLibrary 'org.apache.http.legacy' }
图例:

3、出现Android.os.NetworkOnMainThreadException错误提示的原因
原因:不允许在主线程中进行网络访问
解决办法:将网络访问的操作单独放到一个线程中,代码如下:
new Thread(){ @Override public void run() { //把网络访问的代码放在这里 } }.start();
4、W/ResponseProcessCookies: Invalid cookie header:无效的cookie头
HttpClient httpClient = new DefaultHttpClient(); // 加入此项设置 HttpClientParams.setCookiePolicy(httpClient.getParams(), CookiePolicy.BROWSER_COMPATIBILITY);
5、http post请求工具类
import org.json.JSONArray; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.HttpURLConnection; import java.net.URL; /** * Created by qinshang on 2016/7/12. */ public final class HttpUtil { public static InputStream sendPost(String url,String params) throws IOException { URL realurl = null; InputStream in = null; HttpURLConnection conn = null; try{ realurl = new URL(url); conn = (HttpURLConnection)realurl.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); PrintWriter pw = new PrintWriter(conn.getOutputStream()); pw.print(params); pw.flush(); pw.close(); in = conn.getInputStream(); }catch(Exception eio){ throw eio; } return in; } public static JSONArray getJSON(String url,String params){ JSONArray json = null; try { InputStream inputStream = sendPost(url,params); InputStreamReader in = new InputStreamReader(inputStream); // 获得读取的内容 BufferedReader buffer = new BufferedReader(in); // 获取输入流对象 String inputLine = null; String result = ""; while ((inputLine = buffer.readLine()) != null) { result += inputLine ; } if (!"".equals(in)){ json = new JSONArray(result); } } catch (Exception e) { e.printStackTrace(); } return json; } public static String getContentTxt(String url,String params){ String inputLine = null; String result = ""; try { InputStream inputStream = sendPost(url,params); InputStreamReader in = new InputStreamReader(inputStream); // 获得读取的内容 BufferedReader buffer = new BufferedReader(in); // 获取输入流对象 while ((inputLine = buffer.readLine()) != null) { result += inputLine + "\n"; } } catch (Exception e) { e.printStackTrace(); } return result; } }
6、错误:Only the original thread that created a view hierarchy can touch its views——Handler的深入解析
根本原因是view等控件是非线程安全的,android所有的更新UI操作都需要在主线程(即UI线程)中完成,而不能在新开的子线程中操作。
.Handler的使用场合:
1、 to schedule messages
and runnables to be executed as some point in the future;
安排messages和runnables在将来的某个时间点执行。
2、 to enqueue an action
to be performed on a different thread than your own.
将action入队以备在一个不同的线程中执行。即可以实现线程间通信。比如当你创建子线程时,你可以再你的子线程中拿到父线程中创建的Handler对象,就可以通过该对象向父线程的消息队列发送消息了。由于Android要求在UI线程中更新界面,因此,可以通过该方法在其它线程中更新界面。
通过Handler更新UI实例,步骤:
1、创建Handler对象(在主线程中创建便于更新UI)。
2、构建Runnable对象,在Runnable中更新UI。
3、在子线程的run方法中向UI线程post【runnable对象】来更新UI。
package djx.android; import djx.downLoad.DownFiles; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class updateUI extends Activity { private Button button_update=null; private TextView textView=null; private String content=null; private Handler handler=null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //创建属于主线程的handler handler=new Handler(); button_update=(Button)findViewById(R.id.button_update); textView=(TextView)findViewById(R.id.text_view); button_update.setOnClickListener(new submitOnClieckListener()); } //为按钮添加监听器 class submitOnClieckListener implements OnClickListener{ @Override public void onClick(View v) { //从本地服务下载resource.txt文件内容在textView上显示 String url = "http://192.168.0.1:8080/downLoadServer/resource.txt"; textView.setText("正在加载......"); new Thread(){ public void run(){ content = HttpUtil.getContentTxt(url,null); handler.post(runnableUi); } }.start(); } } // 构建Runnable对象,在runnable中更新界面 Runnable runnableUi=new Runnable(){ @Override public void run() { //更新界面 textView.setText("the Content is:"+content); } }; }
7、Android Studio R类莫名丢失
找不到R文件的原因有如下两类:
1:IDE或代码问题,非个人原因;
2:个人误操作导致IDE不予提示R文件;
解决办法:
-
第一种
①首先确保资源文件是否存在错误,包括layout文件以及图片资源等文件
②然后执行IDE菜单栏的Build -> Clean Project -
第二钟
Android Studio有包自动导入功能,可以自定义某些类不自动导入,也就是屏蔽掉某些类。可能自己在导包时候跳出一个窗口(自动导包)自己没看清楚直接按了回车把自己的R文件屏蔽掉了。①打开Android Studio设置界面 File -> Settings -> Editor -> General -> Auto Import 打开自动引用设置界面
②然后删掉被屏蔽的R文件。
8、java.lang.SecurityException
1、Manifest file 没有配置:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
2、模拟器匹配不对
我之前用模拟器API_23时老是报此错,后来切换低一版本的API_22没有问题
1、android studio 开发平台:
在Terminal输入命令:keytool -list -v -keystore D:/Android/API-key/map.jks
D:/Android/API-key/map.jks是签名文件的存放路径
在Android模拟器上安装apk的时候出现
INSTALL_FAILED_NO_MATCHING_ABIS
这个错误提示的解决办法。
是由于使用了native libraries 。该native libraries 不支持当前的cpu的体系结构。
INSTALL_FAILED_NO_MATCHING_ABIS is when you are trying to install an app that has native libraries and it doesn't have a native library for your cpu architecture. For example if you compiled an app for armv7 and are trying to install it on an emulator that uses the Intel architecture instead it will not work.
现在安卓模拟器的CPU/ABI一般有三种类型,INTEL X86,ARM,MIPS,
如果选择用INTEL X86出现INSTALL_FAILED_NO_MATCHING_ABIS的错误,那就改用ARM的
首先,要看一下自己的项目使用 “Gradle版本”
接着要看一下项目根目录的build.gradle文件中的“dependencies”的 classpath ‘com.github.dcendents:android-maven-gradle-plugin:1.3‘
是1.3还是1.2?
1.2和1.3是很大区别的。
1.3的项目名是“android-maven-gradle-plugin/”
1.2的项目名是"android-maven-plugin"
所以别傻乎乎的,以为改了一个数字就能用。

浙公网安备 33010602011771号