Android 遇到的一些问题

20 . Observable.interval()不起作用的解决办法 2016-11-25

When you use the default scheduler (Schedulers.computation()) the
 observable emits on another thread. If your program exits just after 
the subscribe then the observable is not given a chance to run. Put in 
a long sleep just after the subscribe() call and you will see it working.

当您使用默认调度程序(Schedulers.computation())observable发
出在另一个线程。 如果你的程序在subscribe之后退出,那么
observable不会有机会运行。 在subscribe()调用之后进入长睡眠,
你会看到它工作。

19.android studio 引入aar包

1.将aar包拷贝到model的libs文件下
2.在model的build.gradle文件中加入
  /*和android配置同级*/
    repositories{  
        flatDir { 
             dirs 'libs'
              }}
      dependencies {  
              compile(name: 'aar文件名', ext: 'aar')
      }

备注:如果aar引用了第三方的开发包,则第三方开发包是不会打包在aar文件中的,需要单独引入

18.java中静态块的相关知识:

  1.static{}这个程序运行的时候只会执行一次,而且是优先执行。
  2、对于{}程序每次运行的时候都会执行一次,落后于static{},但优先于构造方法。       
  3、A()构造方法最后执行,每次创建对象(new)的时候就会执行一次。

17.webView中存在编辑框时,软键盘弹出时,布局不会自动顶上去?

当我们在布局中有webView的Activity中,设置了支持软键盘弹出后布局
可以被顶上去,发现无效时,此时可能是我们设置的Activity的主题
(theme)出现问题了,主题中的android:windowTranslucentStatus要设置成false.

16.使用Intent传递数据量大时(尤其是bitmap对象),app没反应?

   android四大组件之间Intent传递数据,数据不能过大,基本要小于1M,不然会导致程序黑屏,ANR.

15.this.requestWindowFeature(Window.FEATURE_NO_TITLE);代码中去掉标题栏使用时报错?

    当我们的Activity是继承自Activity或者是FragmentActivity时不会有问题;但当我们继承的是AppCompatActivity时就会报错,
解决方法是 getSupportActionBar().hide()或者是这是主题为Theme.AppCompat.Light.NoActionBar;

14.在androidAPI16中NoScrollGridView的item布局的根布局背景色设置为白色时候,在代码中调用setTopBarBgColor()设置topBar的背景色时,item的背景会和topBar背景一样而在API16以上不会出现。

可以在xml直接设置,不用代码设置来避免

13.在使用第三方库经常报有一些v4,v7包冲突问题?

扫描二维码关注公众号,回复: 2521829 查看本文章
   在封装library的时候,尽量不要引入第三方包,v4,v7等自带的包也是一样尽量不要引入,避免以后工程依赖的时候,包或者包内文件产生冲突。
   对于冲突的包,改成一样的就可以解决了。

12.手机屏幕的高度包括哪些?

android手机的屏幕的高度不包括[状态栏],包括[标题栏(actionBar)]和[显示区];方法getWindowVisibleDisplayFrame(rect)获取的是显示区的范围

11.static修饰的class、代码块、变量什么时候执行?

static 修饰的代码块,变量和方法在编译的时候就已经执行了,(在程序开始执行前)

10.ContentProvider的onCreate()什么时候执行?

contentProvider的oncreate()方法在程序Application的oncreate()方法前执行。

9.Can't load native library.CPU arch invalid for this build?

 CPU_ABI : armeabi, armeabi-v7a, arm64-v8a, x86, x86_64, mips, mips64.
 
 复制你的.so文件到<project>/libs/(armeabi|armeabi-v7a|x86|...)
 Android Studio复制到jniLibs文件夹下,eclipse 复制到libs文件夹下。
 
注意当你使用多个第三方库的时候时,而且这些库有使用.so文件,创建文件夹时应保证【最少原则】。比如:一个库中.so文件支持armeabi,armeabi-v7a,x86,另外一个库却只支持armeabi-v7a这样也会造成该问题的产生,因为支持x86的机器会在另一个库中找不到.so文件而保错。


8.Parcelable接口使用时,数据传递错误原因

//实体类的属性的写入和读取顺序不一致,造成的。例如:
  public class ImageItem implements Parcelable {
    public String imageId;
    public String imagePath;//原图的路径
    public boolean isSelected;
    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
    //写入的顺序
        dest.writeString(imageId);
        dest.writeString(imagePath);
        dest.writeByte((byte) (isSelected ? 1 : 0));
    }

    public static final Parcelable.Creator<ImageItem> CREATOR = new Parcelable.Creator<ImageItem>() {
        @Override
        public ImageItem createFromParcel(Parcel source) {
            //读取要和写入的顺序一致,否则会造成数据读取错乱
            ImageItem item = new ImageItem();
            item.imageId = source.readString();
            item.imagePath = source.readString();
            item.isSelected = (source.readByte() != 0);
            return item;
        }

        @Override
        public ImageItem[] newArray(int size) {
            return new ImageItem[size];
        }
    };
}

7,如何不让任务栈显示在最近任务栈列表里面?

  • 将任务栈的rootActivity的excludeFromRecents设置为true
  • 官网对excludeFromRecents的解释如下:
android:excludeFromRecents
Whether or not the task initiated by this activity should be 
excluded from the list of recently used applications, the 
overview screen. That is, when this activity is the root activity 
of a new task, this attribute determines whether the task 
should not appear in the list of recent apps. Set "true" if the 
task should be excluded from the list; set "false" if it should be
 included. The default value is "false".

6.Android应用第一次安装成功点击“打开”后Home键切出应用后再点击桌面图标返回导致应用重启问题

/*在应用程序设置<action android:name="android.intent.action.MAIN" />应用程序入口
Activity的onCreate方法中加入上面的判断,完美解决应用程序多次重
启问题。
*/
if((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0){
   finish();
   return;
}

5.Fragment中点击事件使用隐式绑定会报错?

//fragment中不能使用隐式绑定点击事件。

1.建议使用显示绑定
2.参考网址:
http://stackoverflow.com/questions/21192386/android-
fragment-onclick-button-method

4.Fragment中的setUserVisibleHint不执行?

1.viewpager+fragment模式setUserVisibleHint()方法是执行的,其
中第一个fragment的该方法执行两次,第一次在onAttch()方法执行之
前,第二次在onCreate()方法执行之后,在onCreateView()方法执行
之前。当我们把fragment缓存起来时,该方法还是会执行的。


2.当我们自己切换fragment时,transition.add(R.id.main_content, 
mainFragment);(获取用replace),fragment 中的
setUserVisibleHint()是不执行的。

3.android开发中的图标大小?

drawable-mdpi,hdpi,xhdpi,xxhdpi,对应的icon大小分别是24px,32px,48px,72px

2.ScrollView嵌套RecyclerView,recyclerView不显示问题?

建议使用是其他方式,本人使用NoScrollGridView ,NoScrollListView来替换RecyclerView。

1.布局中CoordinatorLayout中包含自定义ViewGroup并且ViewGroup的宽高设置为Match_parent情况下,onLayout调用时在Api21以上,返回过来的高度是全屏的高度(包括状态栏),而在api21以下正常(高度不包含状态栏)?

//问题布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.myapplication.MainActivity">

 <com.myapplication.MyLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#cccccc">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="这是按钮" />
    </com.myapplication.MyLayout>

</android.support.design.widget.CoordinatorLayout>


//正常布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.myapplication.MainActivity">


  <include layout="@layout/content_main"/>

</android.support.design.widget.CoordinatorLayout>

//布局content_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.myapplication.MainActivity"
    tools:showIn="@layout/activity_main">

    <com.myapplication.MyLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#cccccc">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="这是按钮" />
    </com.myapplication.MyLayout>
</RelativeLayout>

猜你喜欢

转载自blog.csdn.net/Keepsty/article/details/81359682