小编Lui*_*uis的帖子

如何使用Android Studio运行单元测试

我正在使用Jake的Android单元测试插件进行gradle:https://github.com/JakeWharton/gradle-android-test-plugin

我的build.gradle看起来像这样:

dependencies {
// analytics
compile('com.crittercism:crittercism-android:3.0.11')

// retrofit
compile('com.squareup.retrofit:retrofit:1.2.2')
compile('com.squareup.okhttp:okhttp:1.2.1')

// dagger
compile('com.squareup.dagger:dagger:1.1.0')
compile('com.squareup.dagger:dagger-compiler:1.1.0')

// compatibility
compile('android.compatibility:android-support:v4-r13')
compile('com.actionbarsherlock:actionbarsherlock:4.4.0@aar')

// Picasso
compile('com.squareup.picasso:picasso:2.1.1')

// Otto
compile('com.squareup:otto:1.3.4')

// Tests
testCompile 'junit:junit:4.10'
testCompile 'org.robolectric:robolectric:2.2'
testCompile 'org.powermock:powermock-api-mockito:1.5.1'
testCompile 'org.easytesting:fest-assert-core:2.0M10'
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,我无法运行所有或特定的单元测试表单Android Studio.我收到错误:

Exception in thread "main" java.lang.NoClassDefFoundError: junit/textui/ResultPrinter
 at java.lang.Class.forName0(Native Method)
 at java.lang.Class.forName(Class.java:171)
 at com.intellij.rt.execution.application.AppMain.main(AppMain.java:113)
Caused by: java.lang.ClassNotFoundException: junit.textui.ResultPrinter
 at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
 at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
 ... 3 more
Run Code Online (Sandbox Code Playgroud)

这是正确的,因为运行命令行不包括我的JUnit依赖项:

/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bin/java -ea -Didea.launcher.port=7533 "-Didea.launcher.bin.path=/Applications/Android Studio.app/bin" …
Run Code Online (Sandbox Code Playgroud)

junit android gradle robolectric android-studio

28
推荐指数
2
解决办法
6万
查看次数

在ndk-build期间禁用所有警告

我试图在'ndk-build'过程中禁用所有警告,以编译在Android上与JNI一起使用的CPP代码.

LOCAL_CFLAGS := -Wno-error没有成功使用.

有任何想法吗?

谢谢

java-native-interface clang compiler-warnings android-ndk

8
推荐指数
1
解决办法
6103
查看次数

webview android的自定义字体

我在我的应用程序中使用webview,我希望用波斯语显示自定义字体的文本.我将我的字体放在资产文件夹中,并在webview中编写地方文本的方法,但是在不支持波斯语的api8中.显示未知的字符而不是我的文本和支持波斯语的apies它不会通过自定义字体显示我的文本,它默认显示我的文字字体.我的方法有什么不对.谢谢我的方法

  public void setText(String text)
{
    String style2="<style type=\"text/css\">@font-face {font-family: 'myface';src: url('file:///android_asset/BYekan.ttf'); } body {font-family: 'myface';}</style>";
    this.loadDataWithBaseURL("file:///android_asset/BYekan.ttf","<html  dir=\"rtl\"><head><meta content=\"text/html; charset=utf-8\" http-equiv=\"Content-Type\"/>"+style2+"</head><body>"+text+"</body></html>","text/html","UTF-8",null);
    setClickable(false);
    setLongClickable(false);
    setFocusable(false);
    setFocusableInTouchMode(false);
}
Run Code Online (Sandbox Code Playgroud)

android font-face android-webview

8
推荐指数
1
解决办法
4124
查看次数

HttpsUrlConnection:在2.3上找不到证书路径的信任锚

那里.我需要与https://free.temafon.ru建立https连接,但我在Android 2.3及更低版本上有CertPathValidatorException.我做了什么.

  1. 使用Firefox 从https://free.temafon.ru获取所有证书.
  2. 从temefon证书到根证书依次导入密钥库中的证书.
  3. Init ssl上下文:

    final KeyStore keystore = KeyStore.getInstance("BKS");

        keystore.load(getResources().openRawResource(R.raw.temafon),
                    "W0d3Uoa5PkED".toCharArray());
        final TrustManager trustManager = new TemafonTrustManager(keystore);
    
        final SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, new TrustManager[] { trustManager }, null);
    
        HttpsURLConnection.setDefaultSSLSocketFactory(sslContext
                .getSocketFactory());
    
    Run Code Online (Sandbox Code Playgroud)

    在这里,我使用自定义TrustManager,因为服务器以错误的顺序发送证书.

这段代码在Android 4.0上工作正常,但2.3在 java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.我正在做的事情上失败了吗?

我已经创建了一个测试项目,可以在这里找到.

ssl https android ssl-certificate httpsurlconnection

5
推荐指数
1
解决办法
1124
查看次数

实现包含List的Parcelable接口的对象会抛出NullPointerException

我正在尝试List使用该Parcelable接口创建一个包含对象的对象.我无法读Parcel回来的对象.

谁能指出我正确的方向?我在这里错过了什么?

MyParcelable 宾语:

public class MyParcelable implements Parcelable {

    private int myInt = 0;
    private List<MyListClass> arrList;

    public List<MyListClass> getArrList() {
        return arrList;
    }

    public void setArrList(List<MyListClass> arrList) {
        this.arrList = arrList;
    }

    public int getMyInt() {
        return myInt;
    }

    public void setMyInt(int myInt) {
        this.myInt = myInt;
    }

    MyParcelable() {
        // initialization
        arrList = new ArrayList<MyListClass>();
    }

    public MyParcelable(Parcel in) {
        myInt = in.readInt();
        in.readTypedList(arrList, MyListClass.CREATOR);
    }

    @Override
    public int describeContents() { …
Run Code Online (Sandbox Code Playgroud)

android parcelable

3
推荐指数
1
解决办法
4150
查看次数

构造函数中预定的可能值

假设我有一个制作球的课程.在构造函数,它接受一个String指示种球,但我只希望它接受一定的值,如:"FOOTBALL","BASEBALL""SOCCERBALL",所以如果我通过我的课到另一个开发者和他们去在Eclipse中使用它,它会提示他们关于可接受的价值.我该怎么办?我不知道是否有一种我可以使用的通用技术或它可能被调用的内容,因此欢迎指向教程或示例的指针.

public class Ball {
    public Ball(String type){
        …
    }
}
Run Code Online (Sandbox Code Playgroud)

java types javadoc

1
推荐指数
1
解决办法
158
查看次数

for循环NullPointerException中的Java Iterator

我不理解为什么一个显示java.lang.NullPointerException是用下面的代码抛出(在此行:迭代= chapterKeywords [Z] .iterator();):

Iterator<String> iterator;

        for (int z = 0; z < chapterKeywords.length; z++) {

            try {

                iterator = chapterKeywords[z].iterator();
                //exception thrown here after first iteration (i.e., when z = 1) and subsequent ones

                while (iterator.hasNext()) {

                    System.out.println(iterator.next());

                }

            } catch (Exception e) {
               ;
            }

        }
Run Code Online (Sandbox Code Playgroud)

虽然我得到NullPointerException,但代码完全执行.仅供参考,chapterKeywords是LinkedList数组,其中每个数组元素是LinkedList(例如,singleChapterKeywords).

LinkedList<String>[] chapterKeywords = new LinkedList[numFiles];
LinkedList<String> singleChapterKeywords = new LinkedList<String>();
Run Code Online (Sandbox Code Playgroud)

虽然该程序似乎运行良好,但我想了解为什么抛出NullPointerException,因为我想完全消除它.基本上,在for循环中的每次迭代开始时抛出异常.我已经修改了代码来创建循环内一个新的迭代,创建迭代的阵列等,但它们都产生相同的结果:的NullPointerException迭代= chapterKeywords [Z] .iterator(); .

任何有关消除此异常的帮助或见解都非常感谢.

所以,这是我的问题:

当迭代器变量被分配给新的迭代器时,为什么在每次后续迭代时抛出NullPointerException?

如何消除此异常?

再一次,TIA!

更新: 我认为 LinkedList 已 …

java iterator linked-list nullpointerexception

1
推荐指数
1
解决办法
3340
查看次数