Android 问题解决方法(一)

来源:互联网 发布:托福口语模板 知乎 编辑:程序博客网 时间:2024/06/05 05:12

1、以使用android文件系统加载视频文件

1)视频文件存放在/res/raw下

2)

String videoName = "video";int id = getResources().getIdentifier(videoName, "raw", getBaseContext().getPackageName());final String path = "android.resource://" +getBaseContext().getPackageName() + "/" + id;videoView.setVideoURI(Uri.parse(path));

2、Fragment传参方法

·Fragment由系统控制,类中中不能写构造函数,其传入参数方法如下:

·用Bundle传入参数

private static final String ARG_SECTION_NUMBER = "section_number";public static HistoryListFragment newInstance(int sectionNumber) {    RippleDrawable drawable ;    HistoryListFragment fragment = new HistoryListFragment();    Bundle args = new Bundle();    args.putInt(ARG_SECTION_NUMBER, sectionNumber);    fragment.setArguments(args);    return fragment;}

3、Context 尽量不要被定义为静态的

如果被定义成静态的,要在onDestroy()置空,进行回收,避免内存溢出。

4、单例工具类

尽量不要写把方法写成静态的,可以单提出写一个工具类,工具类使用单例模式,保障内存中只有一个。

public static HistoryRecordUtils historyRecordUtils;static ViewRecordUpdateBean viewRecordUpdateBean = ViewRecordUpdateBean.getInstance();public synchronized static  HistoryRecordUtils getInstance(){    if(historyRecordUtils==null){        historyRecordUtils = new HistoryRecordUtils();    }    return historyRecordUtils;}

5、Handler添加到MainLooper中

1)不带参数
new Handler(Looper.myLooper()).post(new Runnable() {    @Override    public void run() {        //在UI线程中处理     }});

判断一个线程是否是主线程:
Looper.getLooper() == Looper.getMainLooper()来判断

2)带参数
Looper myLooper, mainLooper;myLooper= Looper.myLooper(); //获得自己的LoopermainLooper= Looper.getMainLooper(); //获得自己的main的looperprivate EventHandler mNoLooperThreadHandler = new EventHandler(mainLooper); class EventHandler extends Handler {   public EventHandler(Looper looper) {    super(looper);    }    public EventHandler() {    super();   }  public void handleMessage(Message msg) {    // 可以根据msg.what执行不同的处理,这里没有这么做 something...  }   }  

6、INSTALL_FAILED_OLDER_SDK

Android apk运行所需要的最低版本高于你的真机的android版本。

在AndroidMainfest.xml中添加

 <uses-sdk        android:minSdkVersion="14"        android:targetSdkVersion="16" />

7、获得屏幕宽和高

    WindowManager windowManager = getWindowManager();    Display display = windowManager.getDefaultDisplay();    int screenWidth = display.getWidth();    int screenHeight = display.getHeight();

8、Android Icon尺寸

LDPI (Low Density Screen,120 DPI),其图标大小为 36 x 36 px。
MDPI (Medium Density Screen, 160 DPI),其图标大小为 48 x 48 px。
HDPI (High Density Screen, 240 DPI),其图标大小为 72 x 72 px。
xhdpi (Extra-high density screen, 320 DPI),其图标大小为 96 x 96 px。
xxhdpi(xx-high density screen, 480 DPI),其图标大小为144 x 144 px。

9、代码规范

    1. 所有的页面承载是在 Fragment 中 而非 Activity 中。
    2. Fragment  Activity  中最好不要定义 public 方法互相调用。需要公用的东西提取到公共的工具类或者父类中。
    3. Activity Fragment 等 不要作为参数随意传递 需要 Context 的地方使用 getApplicationContext() ;
    4. 所有的数据库调用最好封装在 ContentProvider 中 ,避免多线程多进程操作同一个数据库时产生异常。
    5. 空指针异常的判断,在使用别人穿进的参数或者通过别人的方法取得返回值时,在无法确认参数一定不为空的情况下,一定要进行空指针判断。
    6. 注意 Handler 产生的内存泄漏 。
    7. 图片加载使用 Fresco  Fresco 使用介绍 http://www.fresco-cn.org/

10、通知栏打通:

   @Override    protected void onCreate(Bundle savedInstanceState) {        requestWindowFeature(Window.FEATURE_NO_TITLE);        super.onCreate(savedInstanceState);        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {            Window window = getWindow();            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);            window.setStatusBarColor(Color.TRANSPARENT);        }        setContentView(R.layout.activity_personal_detail);//        try {////            Method setM = Window.class.getDeclaredMethod("setDarkStatusIcon",//                    boolean.class);////            setM.invoke(getWindow(), true);////        } catch (Exception e) {////        }    }

11、清除callback方法:

private void clearTouchHelper() {//mItemTouchHelper是 implements RecyclerView.OnChildAttachStateChangeListener        if (mItemTouchHelper != null) {            Class clazz = mItemTouchHelper.getClass();            try {                Method destroyCallbacks = clazz.getDeclaredMethod("destroyCallbacks");                destroyCallbacks.setAccessible(true);                destroyCallbacks.invoke(mItemTouchHelper);  //调用清理方法                mItemTouchHelper = null;            } catch (Exception e) {                e.printStackTrace();            }        }    }





0 0