Android 记录-点滴

1.这里 是 styleable 要加上后缀

 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ScaleViewPager, 0, 0);
        mMarginLeftRight = a.getDimension(R.styleable.ScaleViewPager_magin_left, 0);
        mGutterSize = a.getDimensionPixelSize(R.styleable.ScaleViewPager_scale, 0);
        a.recycle();
        init(context);

2.改变文字点击 样式 颜色

 ShuoMClickableSpan extends ClickableSpan
    @Override
    public void updateDrawState(TextPaint ds) {
        super.updateDrawState(ds);
        ds.setUnderlineText(false);
    }

3.自定义Viewgroup~~SlideViewPager
多揣摩下
4.android 自定义TextView支持微博功能后在ListView占用了Item点击的解决办法
请看连接~别人解决办法
5.getDrawable 已过时 在22已被废弃了
Resources类中有两个方法在API level 22中被废弃:

废弃接口:Drawable getDrawable(int id)

替代接口:Drawable getDrawable(int id, Resources.Theme theme), 第二个参数@theme可以为空值.

       或Context.getDrawable(int)

废弃原因:在JELLY_BEAN(也就是Android 4.1版本)之前, 当这里传递的资源ID是另一个Drawable资源的别名, 则该函数不能正确地获取到最终配置density。即别名资源(alias resource)的density配置不同于实际资源的, 返回的Drawable对象的density将不正确, 这样缩放时将出错。

6.
dialog.show()引起的android.view.WindowManager$BadTokenException错误
错误日志

android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@427b7270 is not valid; is your activity running?
    at android.view.ViewRootImpl.setView(ViewRootImpl.java:653)
    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:326)
    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
    at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
    at android.view.Window$LocalWindowManager.addView(Window.java:558)
    at android.app.Dialog.show(Dialog.java:316)

错误原因
错误原因是Dialog在show的时候必须要有一个activity作为窗口载体,上面的日志的意思是承载Dialog的activity已经被销毁了,不存在了

解决办法
1、在show之前加判断activity是否被销毁了
if(!isFinishing()){
dialog.show();
}
2、直接try catch(不推荐)
错误日志
android.view.WindowManager$BadTokenException: Unable to add window – token null is not for an application
错误原因
先说说上下文的使用
对话框它是我们的Activity的一部分,对话框它挂载在我们的Activity上;
getApplicationContext()这个方法得到的是Context
Activity.this 得到Context的一个子类
也就是说 Activity.this 相当于是getApplicationContext()的子类
父类有的子类一定有 - 没有 token
子类有的父类不一定有 –有 token
this 还有Activity.this和我们的getApplicationContext();
大多数情况推荐:Activity.this
解决办法
上下文大多数情况推荐:Activity.this

二、dialog.dismiss()引起的java.lang.IllegalArgumentException错误

错误日志

java.lang.IllegalArgumentException: View not attached to window manager
    at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:383)
    at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:285)
    at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:104)
    at android.app.Dialog.dismissDialog(Dialog.java:332)
    at android.app.Dialog.dismiss(Dialog.java:315)

错误原因
这个错误测试是测不出来的,我是加了第三方的错误统计才得以发现的,原因是由于某种原因导致Activity被杀死后又重新创建
常发生这类Exception的情形都是,有一个费时的线程操作,需要在显示一个ProgressDialog,在任务开始的时候显示一个对话框,然后当任务完成了再Dismiss对话框,如果在此期间如果Activity因为某种原因被杀掉且又重新启动了,那么当Dismiss的时候WindowManager检查发现Dialog所属的Activity已经不存在了,所以会报IllegalArgumentException: View not attached to window manager.
解决办法
从网上找了好些解决方案都不是太理想,然后就尝试着自己解决, 我是这么解决的,反正加上之后这个错误就没有再出现过,如有不对还请赐教。
重写Activity的onDestroy,将dialog置为空。
@Override
public void onDestroy() {
super.onDestroy();
dialog=null;
}

12.Anroid 异常:is not valid; is your activity running?

Android利用ActivityGroup加载子Activity,子Activity调用对话框,弹出:
ERROR/AndroidRuntime(10104): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@45a58ee0 is not valid; is your activity running?

原因分析:
因为new对话框的时候,参数content 指定成了this,即指向当前子Activity的content。但子Activity是动态创建的,不能保证一直存在。其父Activity的content是稳定存在的,所以有下面的解决办法。
由于我那个是单例 context还是用之前的context 但那个 Activity已经被销毁了
所以show的时候 传入新的当前context
13.仿淘宝的下拉加载商品详情分页是由于 后台线程一直在跑,定时器未关闭~

public void cancel() {
            if (mTask != null) {
                mTask.cancel();
                mTask = null;
            }
        }

这里只是将线程的 引用地址制空了 ,但 线程还是依旧在跑,会导致View 绘制 花屏

14.引用Fragment一定要看 attach Activity 是不是运行

ProductFinancingFragment

public class ProductFinancingFragment extends BaseProductFragment {

    /**
     * 理财产品
     */
    private Button mBtnToList;
    /**
     * 我的产品
     */
    private Button mBtnToUser;
    /**
     * 返回按钮
     */
    private View mViewBack;
    /**
     * 理财产品fragment
     */
    private ProductFinancingFragment mProductListFragment;  (PS 这里无限循环嵌套了)
    /**
     * 我的产品fragment
     */
    private UserSelfProductFragment mUserSelfProductFragment;

Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Deleg


10-01 13:36:23.549: E/AndroidRuntime(14188): Process: com.android.settings, PID: 14188
10-01 13:36:23.549: E/AndroidRuntime(14188): android.view.InflateException: Binary XML file line #43: Error inflating class fragment

进入StackOverflowException的死循环的异常的去了·~~~GG了
主的Fragment 一定要看是否循环嵌套进去
15.String [] 转list方法

String[] months_big = { "1", "3", "5", "7", "8", "10", "12" };
        String[] months_little = { "4", "6", "9", "11" };

        final List<String> list_big = Arrays.asList(months_big);
        final List<String> list_little = Arrays.asList(months_little);

16.dailog 未能水平铺满
由于应该 dailog的默认的style样式 中有加了属性 值设置,所以不能 用Contex的构造方法

 public ShareDialog(Context context,boolean isSeleted) {
        super(context,R.style.rollerWindowStyle);
        this.context=context;
        this.isSeleted =isSeleted;
        this.setCanceledOnTouchOutside(true);
    }


  WindowManager.LayoutParams wl = window.getAttributes();
        // 以下这两句是为了  dialog保证按钮可以水平满屏
        Log.e("test","height"+wl.y+"width"+((Activity)context).getWindowManager().getDefaultDisplay().getWidth());

        wl.width = WindowManager.LayoutParams.MATCH_PARENT;
        wl.height = WindowManager.LayoutParams.WRAP_CONTENT;
        // 设置显示位置
        window.setAttributes(wl);
        onWindowAttributesChanged(wl);

17.readLine()是读取流读数据的时候用的,当读到换行标记’\n’、’\r’(回车)时,会跟着换行,同时会以字符串形式返回这一行的数据,当读取完所有的数据时会返回null
18.
toolbar增加menu的item,改变字体颜色方法终于找到了 QAQA

 <item name="actionMenuTextColor">#ffffff</item>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值