android开发中遇到的问题汇总【八】

一. 工具使用

245.使用androidstudio时,不知道什么原因svn不见了

Android Studio missing Subversion plugin

Please make sure that the “SubversionIntegration” plugin is enabled in Preferences > Plugins

248.androidstudio 如何自动import用到的类或接口?
For Windows/Linux, you can go to File -> Settings -> Editor -> General -> Auto Import -> Java and make the following changes:

change Insert imports on paste value to All

markAdd unambigious imports on the fly option as checked
On a Mac, do the same thing in Android Studio -> Preferences

参考What is the shortcut to Auto import all in Android Studio?

251. ubuntu下删除.svn的方法
find -type d -name '.svn' -exec rm -rfv {} \;

参考 http://blog.csdn.net/zhaoyu7777777/article/details/9445717
252. Fatal : Could not read from remote repository.

git配置使用,已经把公钥发给发给服务端,在终端命令行也是可以正常的pull push,但是在androidstudio push或者pull的时候确出现上述错误
解决方式
setting –> Version Control –>Git ,In the SSH executable dropdown, choose Native

258.手机root后 还会出现下述情况Android: adb: copy file to /system (Permission denied)

解决方式,需要remount /system
mount -o remount,rw /system

261.anroid几个很不错的快捷键
  1. Ctrl+Shift+Alt+T 重构代码 change name
  2. Ctrl+I 水平分屏显示【需要在keymap中搜索split 设置move right的快捷键】
  3. shift+alt+L 变量生成
  4. ctrl+shift+v 自动生成变量
  5. ctrl+o 查看文件中的函数
  6. ctrl+shift+t 或者ctrl+shift+r 模糊查找类
  7. ctrl+e 查看切换最近打开的文件

二.java及android基础知识

246.Error:Execution failed for task ‘:app:dexDebug’.> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process ‘command ‘/home/xxx/tools/android/jdk1.7.0_71/bin/java” finished with non-zero exit value 2

检查下是否多次引用同一个jar包
以下情况
1. module下jar包版本不同

  1. 同一个module 在libs中包含乐.jar,而在src下又把相应的source页加入了

  2. gradle中是否重复编译,

比如
已经加了compile fileTree(include: [‘*.jar’], dir: ‘libs’)
然而在下面又加一句compile files(‘libs/xxx.jar’)

参考 Error:Execution failed for task ‘:app:dexDebug’. com.android.ide.common.process.ProcessException

249.Android NDK: Could not find application project directory ! Android NDK: Please define the NDK_PROJECT_PATH variable to point to it.
/home/cenuser/android/android-ndk-r7b/build/core/build-local.mk:130: *** Android NDK: Aborting    .  Stop.

cd到jni目录。或者 ndk-build -C your_project_path
256.Android中如何设置RadioButton在文字的右边,图标在左边

解决方法 :
第一步:
android:button=”@null”这条语句将原来系统的RadioButton图标给隐藏起来。
第二步:
android:drawableRight=”@android:drawable/btn_radio”这条语句
参考 http://blog.csdn.net/sunnyfans/article/details/7901592

257.java报“非法字符: \65279 ”错误的解决方法

众所周知,在跨程序的工程中,统一编码是至关重要的,而目前最普遍的则是统一采用“utf8”编码方案。
但是在采用utf8方案的时候,请注意编辑器的自作聪明。
比如editplus。
原因就在于某些编辑器会往utf8文件中添加utf8标记(editplus称其为签名),它会在文件开始的地方插入三个不可见的字符(0xEF 0xBB 0xBF,即BOM),它的表示的是 Unicode 标记(BOM)。
参考 http://hyl198611.iteye.com/blog/1336981

260.android双击back退出
public class MainActivity extends Activity {


    private Toast toast;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toast = Toast.makeText(getApplicationContext(), "确定退出?", 0);

    }
    public void onBackPressed() {
        quitToast();
    }

    private void quitToast() {
        if(null == toast.getView().getParent()){
            toast.show();
        }else{
            System.exit(0);
        }
    }
}

或者

private Toast toast;
 protected void onCreate(Bundle savedInstanceState) {
    ...
         toast = Toast.makeText(this, "再按一次退出应用", Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.BOTTOM, 0, ConversionUtil.dip2px(this, 150));
 }
@Override 
public void onBackPressed() { 
    if (doubleBackToExitPressedOnce) { 
        if(toast!=null){
            toast.cancel();
        }
        super.onBackPressed(); 
        return; 
    } 

    this.doubleBackToExitPressedOnce = true;
    toast.show();

    new Handler().postDelayed(new Runnable() {

        @Override 
        public void run() { 
            doubleBackToExitPressedOnce=false;                        
        } 
    }, 2000); 
}  

参考 Android关于双击退出应用的问题

262.在旧项目中引入android materialdesign 时 出现如下问题

android.view.InflateException: Binary XML file line #17: Error inflating class android.support.design.internal.NavigationMenuView
Caused by: java.lang.reflect.InvocationTargetException
Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x2/d=0x7f0100c5 a=-1}
You need to use a Theme.AppCompat theme (or descendant) with this activity.

解决方法 :使用NavigationMenuView的Activity【一般都是mainActivity】继承自AppCompatActivity,并且修改AndroidManifest.xml中对应activity的theme,使用继承自@style/Theme.AppCompat的主题。

262.How to get key and value of HashMap in java
public class AccessKeyValueOfHashMap {
        public static void main(String[] args) {
                // Create a Empty HashMap 
                HashMap obHashMap = new HashMap();
                // Put values in hash map
                obHashMap.put("AB", "1");
                obHashMap.put("EF", "2");
                obHashMap.put("Gh", "3");
                obHashMap.put("CD", "4");
                //Store entry (Key/Value)of HashMap in set
                Set mapSet = (Set) obHashMap.entrySet();
                //Create iterator on Set 
                Iterator mapIterator = mapSet.iterator();
                System.out.println("Display the key/value of HashMap.");
                while (mapIterator.hasNext()) {
                        Map.Entry mapEntry = (Map.Entry) mapIterator.next();
                        // getKey Method of HashMap access a key of map
                        String keyValue = (String) mapEntry.getKey();
                        //getValue method returns corresponding key's value
                        String value = (String) mapEntry.getValue();
                        System.out.println("Key : " + keyValue + "= Value : " + value);
                }
        }
}

三.android性能

246.android handler的警告Handler Class Should be Static or Leaks Occur

在使用Handler更新UI的时候public class SampleActivity extends Activity {

private final Handler mLeakyHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO
}
}
}会包上述warning 会导致内存泄露
原因在于匿名内部类handler持有activity的引用,当activity finish后 handler还没有处理完,导致activity的view和resource资源不能得到释放,导致内存泄露
针对这个问题google官方给出了正确的做法
通过静态内部类 包含activity的弱引用来处理。
public class SampleActivity extends Activity {

/**
* Instances of static inner classes do not hold an implicit
* reference to their outer class.
*/
private static class MyHandler extends Handler {
private final WeakReference mActivity;

public MyHandler(SampleActivity activity) {
  mActivity = new WeakReference(activity);
}

@Override
public void handleMessage(Message msg) {
  SampleActivity activity = mActivity.get();
  if (activity != null) {
    // ...
  }
}

}

private final MyHandler mHandler = new MyHandler(this);

/**
* Instances of anonymous classes do not hold an implicit
* reference to their outer class when they are “static”.
*/
private static final Runnable sRunnable = new Runnable() {
@Override
public void run() { }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Post a message and delay its execution for 10 minutes.
mHandler.postDelayed(sRunnable, 60 * 10 * 1000);

// Go back to the previous Activity.
finish();

}
}

参考android handler的警告Handler Class Should be Static or Leaks Occur

250 .Why do I want to avoid non-default constructors in fragments? fragment设置参数正确的做法
Make a bundle object and insert your data (in this example your Category object). Be careful, you can't pass this object directly into the bundle, unless it's serializable. I think it's better to build your object in the fragment, and put only an id or something else into bundle. This is the code to create and attach a bundle:

Bundle args = new Bundle();
args.putLong("key", value);
yourFragment.setArguments(args);
After that, in your fragment access data:

Type value = getArguments().getType("key");
That's all.

四. 代码混淆

255.代码混淆时 报如下错误 Error:Execution failed for task ‘:app:proguarxxxRelease’.

java.io.IOException: Can’t read [/libs/xxx.jar] (No such file or directory)
http://stackoverflow.com/questions/26028171/android-studio-proguard-java-io-ioexception-bin-classes-no-such-file-or-d

解答 proguard-android.txt文件中不用在指定 -injars, -outjars, or -libraryjars or libs.

The Android Gradle plugin already specifies all input and output for you, so you must not specify -injars, -outjars, or -libraryjars.

Moreover, the file proguard-android.txt in the Android SDK specifies all generic Android settings for you, so you shouldn’t specify them again.

Essentially, your file proguard-rules.txt can be empty, except for any application-specific settings to make sure any reflection continues to work

四.杂谈

243.架构师编写详细设计的重要性。

详细设计,这是考验技术专家设计思维的重要关卡,详细设计说明书应当把具体的模块以最’干净’的方式(黑箱结构)提供给编码者,使得系统整体模块化达到最大;一份好的详细设计说明书,可以使编码的复杂性减低到最低,实际上,严格的讲详细设计说明书应当把每个函数的每个参数的定义都精精细细的提供出来,从需求分析到概要设计到完成详细设计说明书,一个软件项目就应当说完成了一半了。换言之,一个大型软 件系统在完成了一半的时候,其实还没有开始一行代码工作。

更多问题请关注 android开发遇到问题点滴

你可能感兴趣的:(android开发中遇到的问题)