程序:BindService
时间:20175302148
错误代码LogCat:W/dalvikvm: threadid=1: threadexiting with uncaught exception (group=0x94ceeb20)
现象:停止服务的按钮在线程未进行完毕的时候再次点击,程序崩溃。
解决:
爆出,uncaughexception的原因有很多,这是java的异常机制问题。有的人说什么空指针,什么权限啊。这些都只是这个异常的原因之一。要解决这个问题,首先要知道这个异常,到底是什么。通过try catch是捕获不到这类异常的。但是java提供了一个接口,UncaghtExceptionHandler,可以调用Thread.setDefaultUncaughtExceptionHandler()来处理,在对应的接口uncaughtExcption()方法里打印异常或者存贮到对应的log文件。在这里你就知道问题到底出在哪了。
延伸:
InterruptedException e 线程异常如何处理?
try {
System.out.println(Thread.currentThread().getName()+ ":" + (count++)); Thread.sleep(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
未处理完毕
知识点:
System.currentTimeMillis()获得的是自1970-1-01 00:00:00.000 到当前时刻的时间距离,类型为long
如果需要字符串形式的时间距离
String.valueOf(System.currentTimeMillis())
long ct =System.currentTimeMillis();
String t =String.valueOf(ct);
其实上面的String t就相当于 ct+"";
使用范例:
public StringrefFormatNowDate() {
Date nowTime = newDate(System.currentTimeMillis());
SimpleDateFormat sdFormatter = newSimpleDateFormat("yyyy-MM-dd");
String retStrFormatNowDate =sdFormatter.format(nowTime);
return retStrFormatNowDate;
}
Synchronized一种同步锁。当两个并发线程访问同一个对象时,就好比是一个盒子上有一把锁,每人都会有拿到钥匙的权利,但是必须在当前人打开锁,彻底处理完盒子里面的数据(count一直加到5),关上锁,下一个人才可以访问盒子里面的数据。如果启动两个不同线程对象来访问数据的化,就相当于一个盒子共同有多把锁,相称对象之间拿到数据的几率是随机的。一把锁只对应一个对象,多个锁的产生是因为实例化了不同对象,但是这不同对象却可以公用这个盒子中的数据。
一个线程访问一个对象中的synchronized(this)同步代码块时,其他试图访问该对象的线程将被阻塞。
当一个线程访问对象的一个synchronized(this)同步代码块时,另一个线程仍然可以访问该对象中的非synchronized(this)同步代码块。
synchronized作用于一个类T时,是给这个类T加锁,T的所有对象用的是同一把锁。
A. 无论synchronized关键字加在方法上还是对象上,如果它作用的对象是非静态的,则它取得的锁是对象;如果synchronized作用的对象是一个静态方法或一个类,则它取得的锁是对类,该类所有的对象同一把锁。
B. 每个对象只有一个锁(lock)与之相关联,谁拿到这个锁谁就可以运行它所控制的那段代码。
C. 实现同步是要很大的系统开销作为代价的,甚至可能造成死锁,所以尽量避免无谓的同步控制。
详细使用范例的博客:http://blog.csdn.net/luoweifu/article/details/46613015
PendIntent
就算在执行时当前Application已经不存在了,也能通过存在PendingIntent里的Context照样执行Intent。
PendingIntent一般作为参数传给某个实例,在该实例完成某个操作后自动执行PendingIntent上的Action,也可以通过PendingIntent的send函数手动执行,并可以在send函数中设置OnFinished表示send成功后执行的动作。
详细使用范例的博客:http://blog.csdn.net/yuzhiboyi/article/details/8484771
Cursor
Cursor 是每行的集合。
使用 moveToFirst() 定位第一行。
你必须知道每一列的名称。
你必须知道每一列的数据类型。
Cursor 是一个随机的数据源。
所有的数据都是通过下标取得。
getLayoutInflater()
对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate() 来载入。
getViewById()应该熟悉的,刚接触android时最先接触到的几个方法里肯定有他。findViewById()是找xml布局文件下的具体widget控件(如Button、TextView等)。
最后说一句,对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate(),getLayoutInflater()返回LayoutInflater实例,所以,可以说getLayoutInflater().inflater() 是用来找res/layout下的 xml 布局文件,并且实例化;findViewById() 是找具体 xml 布局文件中的具体 widget 控件(如:Button、TextView 等)。
Alertdialog.builder
用Builder方法形成了一个对象链,通过一系列的设置方法,构造出我们需要的对话框,然后调用show方法显示出来,注意到Builder方法的参数self,这个其实是Activity对象的引用,根据你所处的上下文来传入相应的引用就可以了。例如在onCreate方法中调用,只需传入this即可。
详细使用范例的博客:http://www.cnblogs.com/Gaojiecai/archive/2011/12/10/2283156.html
群发短信功能
问题描述:
点击选择联系人按钮,程序崩溃。
解决:
<uses-permission android:name="android.permission.READ_CONTACTS"/>
未彻底解决,现考虑是因为版本太低导致
intent.getSerializableExtra(BluetoothTools.DATA);
Serializable愿意是可串行化的
Activity之间通过Intent传递值,支持基本数据类型和String对象及它们的数组对象byte、byte[]、char、char[]、boolean、boolean[]、short、short[]、int、int[]、long、long[]、float、float[]、double、double[]、String、String[],还有实现Serializable、Parcelable接口的类对象。
发送部分
CustomeClass cc = new CustomeClass();
cc.setAge(21);
Intent intent = new Intent(this,PersonInfo.class);
intent.putExtra("PERSON_INFO",cc);
startActivity(intent);
接收部分
Intent intent = getIntent();
CustomeClass cc = (CustomeClass)intent.getSerializableExtra("PERSON_INFO");
setTextView(R.id.id, cc.getId());
publicBluetoothServerSocket listenUsingRfcommonWithServiceRecord(String name, UUIDuuid);
作用 : 创建一个监听Rfcommon端口的蓝牙监听, 使用accept()方法监听, 并获取BluetoothSocket对象; 该系统会根据一个服务名称(name)和唯一的识别码(uuid)来创建一个SDP服务, 远程蓝牙设备可以根据唯一的UUID来连接这个SDP服务器;
参数 : name : SDP服务器名称, UUID, SDP记录下的UUID;
返回值 : 正在监听蓝牙端口;
权限 : BLUETOOTH;
------------------------------------------------------------------------------------------------------20176201730
问题描述:安装android studo3.0后新建项目出现;
com.android.tools.aapt2.Aapt2Exception:AAPT2error:check错误
解决:
找到gradle properties
添加:android.enableAapt2=false
将默认的AAPT2关闭即可
再rebuild即可
----------------------------------------------------------------------------------------------------201801211508
如何在线性布局中放两个组件,一个居中,一个靠右侧。
如何设置线性布局的的边距
Layout_gravity、gravity、padding、margin的区别和用法
----------------------------------------------------------------------------------------------------201801230904
QualityDepartment项目第一次合并添加最后一个XML文件时,遇到no resource found的问题,问题如下。
问题在于新添加的Xml文件布局有问题
解决:修改xml文件,重新添加,编译即可
----------------------------------------------------------------------------------------------------201801231119
布局常见问题:
1、 控件命名不能将下划线搞成“减号”
2、 控件不能以自己为基准
3、 控件的包名命名正确,必须是对应activity的包名
----------------------------------------------------------------------------------------------------
问题描述:
写“获取验证码”自定义工具类时,出现逻辑错误
解决:if ...else中else{}括号中应包含if未处理的所有项,不能忘记else的大括号
----------------------------------------------------------------------------------------------------
资源id找错虽然编译通过但是应用会闪退。
----------------------------------------------------------------------------------------------------
先继承Activity;
再到Manifest中注册该自定义Activity;
最后到对应的xml文件中添加包名。
----------------------------------------------------------------------------------------------------201801232306
补充知识点:
1、 android:layout_marginLeft="16dp"属性:本组件相对于父控件的左边距。
<ExpandableListView
android:id="@+id/expandableListView_feng"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/relativeLayout"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="50dp"
android:childDivider="#919191"
android:childIndicatorRight="@null"
android:dividerHeight="1dp"
android:groupIndicator="@null" />
<GridView android:id="@+id/gridView_led" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_below="@+id/relativeLayout" android:layout_centerHorizontal="true" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" android:layout_marginTop="32dp" android:background="@drawable/background_shou_ye" android:clickable="true" android:nestedScrollingEnabled="false" android:numColumns="3" android:verticalSpacing="16dp" />
<com.lcj.smartled.switchbutton.SwitchButton android:id="@+id/search_switch_mode" style="@style/ModeSwitch" android:layout_width="37dp" android:layout_height="17dp" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_gravity="center_vertical" android:layout_marginRight="16dp" /> </RelativeLayout>
获取本地图片
Bitmap decodeResource =BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_default_user_bg_34);
把本地的int类型的图片转换成drawable
Drawable drawable =context.getResources().getDrawable(R.drawable.left);
把本地的int类型的图片转换成Bitmap
Resources r = this.getContext().getResources();
Inputstream is =r.openRawResource(R.drawable.my_background_image);
BitmapDrawable bmpDraw = new BitmapDrawable(is);
Bitmap bmp = bmpDraw.getBitmap();
Bitmap转Drawable
Bitmap bm=xxx; //xxx根据你的情况获取
BitmapDrawable bd=BitmapDrawable(bm);
因为BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。
变量不正确定义可能会使该界面崩溃。
组件与手机系统不兼容,所用的组件所在的包版本比手机系统的版本高,造成APP下载之后直接闪退。201801261909
EditText输入的密文处理可以直接在其组件属性中设置。
---------------------------------------------------------------------------201801311609
Android studio项目文件中.Xml视图底部爆红:Render probloms
解决措施:
修改API版本为低版本,再进行refresh
---------------------------------------------------------------------------201802250913
ListFragmentTest在运行过程中崩溃:
修改:
view=inflater.inflate(R.layout.fragmentlist_test_layout,container,false);
布局加载必须是含有<list>子节点的布局文件而不是含有<fragment>字节点的布局文件
---------------------------------------------------------------------------201802251914
如何在Android Studio中使用git命令,将项目放到gitHub上进行 管理
http://blog.csdn.net/u013634928/article/details/53022005
---------------------------------------------------------------------------201802262119
相同的activity不能同时被startActivity();和startActivityForResult();启动。不是第一个功能运行不了就是第二个功能运行不了。
解决措施:将一个活动拆解为两个活动
---------------------------------------------------------------------------201802281030
如果出现findViewById()方法错误,那就说明所建类与Android现有类重复
修改措施:修改类名即可