小编stk*_*ent的帖子

如何在JAVA中检查空XML元素

我有一个XML,如下所示:

<info>
    <userId>Admin</userId>
    <userNotes></userNotes>         
</info>
Run Code Online (Sandbox Code Playgroud)

我试图用JAVA解析。这是我的代码段:

NodeList userIdNodeList = document.getElementsByTagName("userId");

if (userIdNodeList!=null) {
    userId = userIdNodeList.item(0).getChildNodes().item(0).getNodeValue().trim();
}            

NodeList userNotesNodeList = document.getElementsByTagName("userNotes");

if (userNotesNodeList!=null) {
    userNotes = userNotesNodeList.item(0).getChildNodes().item(0).getNodeValue().trim();
}            
Run Code Online (Sandbox Code Playgroud)

但是上面的代码抛出NULL指针错误,因为该userNotes元素为空。

有想法该怎么解决这个吗?

java xml-parsing

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

如何从OkHttp Response对象中提取原始JSON字符串?

private static final String URL = "http://www.livroandroid.com.br/livro/carros/carros_{tipo}.json";

public static List<Carro> getCarros(Context context, String tipo) throws IOException {
    String url = URL.replace("{tipo}", tipo);
    OkHttpClient okHttpClient = new OkHttpClient();
    Request request = new Request.Builder()
            .url(URL)
            .build();
    Response response = okHttpClient.newCall(request).execute();
    String json = response.body().toString();
    List<Carro> carros = parserJSON(context, json);
    return carros;
}
Run Code Online (Sandbox Code Playgroud)

如果我json在调用getCarros方法时打印出变量的值,我在logcat中看到以下消息:

com.squareup.okhttp.internal.http.RealResponseBody@1e11866

如何记录我收到的实际JSON字符串?

android json okhttp

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

在模块guava-20.0.jar(com.google.guava:guava:20.0)中找到的重复类com.google.common.util.concurrent.ListenableFuture

在我的中使用implementation 'com.google.firebase:firebase-inappmessaging-display:17.2.0'app/build.gradle,出现以下错误:

Duplicate class com.google.common.util.concurrent.ListenableFuture found in modules guava-20.0.jar (com.google.guava:guava:20.0) and listenablefuture-1.0.jar (com.google.guava:listenablefuture:1.0)

Go to the documentation to learn how to Fix dependency resolution errors.
Run Code Online (Sandbox Code Playgroud)

我的内在app/build.gradle是:

implementation 'com.google.android.gms:play-services-base:16.1.0'
implementation 'com.google.android.gms:play-services-analytics:16.0.8'
implementation 'com.google.android.gms:play-services-awareness:16.0.0'
implementation 'com.google.android.gms:play-services-cast:16.2.0'
implementation 'com.google.android.gms:play-services-gcm:16.1.0'
implementation 'com.google.android.gms:play-services-location:16.0.0'
implementation 'com.google.android.gms:play-services-maps:16.1.0'
implementation 'com.google.firebase:firebase-core:16.0.8'
implementation 'com.google.firebase:firebase-iid:17.1.2'
implementation 'com.google.firebase:firebase-messaging:17.6.0'
implementation 'android.arch.work:work-runtime:1.0.1'
implementation 'com.android.support:multidex:1.0.3'
apply plugin: 'com.google.gms.google-services'
Run Code Online (Sandbox Code Playgroud)

也许我正在使用的库之一已经包含对In-App Messaging依赖的支持,然后它变得多余了?谢谢。

android guava firebase firebase-in-app-messaging

3
推荐指数
8
解决办法
1350
查看次数

如何在android中的布局中对齐文本

我想在ListView行中显示一些具有以下对齐方式的文本:

Mike      10pt $1,233,500
Mike2    200pt $1,2,500
Holly     10pt $4500
Run Code Online (Sandbox Code Playgroud)

但是当我尝试时,我得到了这个:

   Mike        10pt   $1,233,500
    Mike2    200pt       $12,500
    Holly            10pt  $4500
Run Code Online (Sandbox Code Playgroud)

我的布局文件如下.我究竟做错了什么?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        android:background="@drawable/dl005_textfield_selected"
        android:paddingBottom="5dp" >

        <TextView
            android:id="@+id/Name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="Mike"
            android:layout_weight="1"
            android:textColor="#888888" />

        <TextView
            android:id="@+id/score"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:gravity="right"
            android:text="10pt"
            android:textColor="#3498db" />

        <TextView
            android:id="@+id/myid"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:text="$1,233,500"
            android:textStyle="bold"
            android:textSize="18sp"
            android:layout_weight="1"
            android:gravity="right"
            android:textColor="#3498db" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
    </LinearLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

android android-layout android-layout-weight

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

为什么android studio需要CalendarContract.Calendars而不是Calendar?

我正在尝试使用我的应用程序读取和写入日历.所以我正在使用android开发者指南中的代码:

我复制了这个:

// Run query
Cursor cur = null;
ContentResolver cr = getContentResolver();
Uri uri = Calendars.CONTENT_URI;   
String selection = "((" + Calendars.ACCOUNT_NAME + " = ?) AND (" 
                    + Calendars.ACCOUNT_TYPE + " = ?) AND ("
                    + Calendars.OWNER_ACCOUNT + " = ?))";
String[] selectionArgs = new String[] {"sampleuser@gmail.com", "com.google",
    "sampleuser@gmail.com"}; 
// Submit the query and get a Cursor object back. 
cur = cr.query(uri, EVENT_PROJECTION, selection, selectionArgs, null);
Run Code Online (Sandbox Code Playgroud)

但是我得到的错误无法解析符号日历.android studio提供的解决方案是Calendars改为CalendarContract.Calendars.

我只是好奇有什么区别?可能为什么开发人员指南包含一个缺陷?

android calendar

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

Android:计算任务图错误

更新项目后,我似乎收到此 Gradle 错误:

Configuration on demand is not supported by the current version of the Android Gradle plugin since you are using Gradle version 4.6 or above. Suggestion: disable configuration on demand by setting org.gradle.configureondemand=false in your gradle.properties file or use a Gradle version less than 4.6.
Run Code Online (Sandbox Code Playgroud)

打开选项卡进行进一步检查时,如下所示:

org.gradle.api.ProjectConfigurationException: A problem occurred configuring project ':app'.
Caused by: org.gradle.api.GradleScriptException: A problem occurred evaluating project ':app'.
Caused by: org.gradle.api.internal.plugins.PluginApplicationException: Failed to apply plugin [id 'com.android.application']
Caused by: org.gradle.api.tasks.StopExecutionException: Configuration on demand is …
Run Code Online (Sandbox Code Playgroud)

android gradle android-gradle-plugin

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

使用CMTime而不进行舍入?

我正在编写一个使用AVFoundation和CMTime的应用程序.我已经记录了CMTime使用创建的实例的值CMTimeMake().此值似乎四舍五入到最接近的整数.我需要一个CMTime具有精确值的实例,而不需要舍入.

我在CMTime参考文献中看到了舍入常量:

enum {
   kCMTimeRoundingMethod_RoundHalfAwayFromZero = 1,
   kCMTimeRoundingMethod_RoundTowardZero = 2,
   kCMTimeRoundingMethod_RoundAwayFromZero = 3,
   kCMTimeRoundingMethod_QuickTime = 4,
   kCMTimeRoundingMethod_RoundTowardPositiveInfinity = 5,
   kCMTimeRoundingMethod_RoundTowardNegativeInfinity = 6,

   kCMTimeRoundingMethod_Default = kCMTimeRoundingMethod_RoundHalfAwayFromZero
};
Run Code Online (Sandbox Code Playgroud)

没有任何关于如何控制这些策略应用于CMTime实例的示例?或者,如果这不是正确的方法,我如何从CMTime实例中提取精确值?


编辑:

我找到并测试过了CMTIME_HAS_BEEN_ROUNDED().我将我的CMTime实例传递给了这个函数并返回No(表示该值尚未舍入).那为什么我会失去精确度呢?

iphone avfoundation core-media ios avmutablecomposition

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

Android Studio 中“View v”的含义

我正在尝试在我的 Android 应用程序中实现按钮点击处理。

在包含我的按钮的 XML 布局文件中,我向我的ButtonXML 元素添加了以下行:

android:onClick="handleClick"
Run Code Online (Sandbox Code Playgroud)

我还在Activity使用此布局的 中定义了具有以下签名的方法:

public void handleClick() { /* ... */ }
Run Code Online (Sandbox Code Playgroud)

但是,当我使用此代码运行我的应用程序时,它崩溃了。我能够通过将我的方法签名更新为以下内容来修复此崩溃:

public void handleClick(View v) { /* ... */ }
Run Code Online (Sandbox Code Playgroud)

但我不明白为什么我需要包含这个View参数?

java android android-view

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