小编Ese*_*far的帖子

记录Crashlytics上的非致命错误

在iOS上,可以使用recordError(错误)记录Crashlytics上的非致命错误,但显然此功能不适用于Android.

我找到的唯一选择是使用logException(e).我在我的应用程序上管理错误,并且我想在返回特定错误代码时进行记录.所以,在Crashlytics上我希望errorCode引用非致命错误.但使用logException(E)方法,错误是由其中方法引用logException(五)被调用.

我怎样才能做到这一点?

android crashlytics crashlytics-android

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

更新当前焦点的EditText的ImeOptions

我有一个EditText,其ImeOptions设置为EditorInfo.IME_ACTION_NEXT。因此,当该字段聚焦时,键盘上将显示“下一步”按钮。我希望用户输入时(出于某些原因)将按钮更改为“完成”。因此,我有一个TextWatcher,我尝试将ImeOptions更改为“ afterTextChanged”上的EditorInfo.IME_ACTION_DONE,但键盘上的键没有更改。我试图隐藏键盘,更改ImeOptions并再次显示键盘,但是它不起作用(此解决方案适用于iOS)。

有人知道该怎么做吗?

android android-edittext imeoptions

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

恢复具有两个具有相同ID的视图的片段

我有一个复杂的布局来实现.它有19个部分可以根据用户先前输入的大量参数显示或不显示.为了简化代码并且不显示未使用的部分,动态创建布局.

一切都在碎片里面.片段有一个用作容器的LinearLayout,当创建片段时,我生成所有必要的部分.

每个部分由其自己的本地适配器管理,该适配器负责扩展此部分的布局并将其添加到容器中.

一切都很好.问题是2个部分具有完全相同的结构,因此它们共享相同的xml布局.因此,两个部分的内部视图具有相同的ID.这不是问题,因为该部分在其适配器中本地管理.当我转到下一个片段然后再回到这个片段时,会出现问题.系统尝试恢复视图的先前状态,并且由于这两个部分具有相同的ID,因此在恢复第二部分时,其值也将设置为第一部分.

是否有任何解决方案来管理它或告诉片段不恢复其状态(因为无论如何都要手动重新加载).

这是当前结构的一个很好的例子:

片段xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
Run Code Online (Sandbox Code Playgroud)

部分xml

<EditText xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/section_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
Run Code Online (Sandbox Code Playgroud)

片段代码

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_layout, container, false);

    if (<condition>)
       createSection1(getContext(),view);

    if (<condition>)        
       createSection2(getContext(),view);

    return view;
}


private void createSection1(Context context, ViewGroup root){
    Section1Adapter adapter = new Section1Adapter(context, root);
    // ...
}

private void createSection2(Context context, ViewGroup root){
    Section2Adapter adapter = new Section2Adapter(context, root);
    // ... …
Run Code Online (Sandbox Code Playgroud)

android android-fragments onsaveinstancestate

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

显示对话框后,使用Retrofit 2和RxJava2重试呼叫

我正在使用Retrofit 2和RxJava2调用API。如果呼叫失败,在某些情况下(例如,没有Internet连接),我想向用户显示错误对话框,然后让他重试。

当我使用RxJava时,我正在考虑使用它,.retryWhen(...)但是我不知道该怎么做,因为它需要等待用户按下对话框上的按钮。

目前,我显示该对话框,但在用户按下任何按钮之前会重试。另外,我希望在用户按下“取消”时不重试该呼叫。

这是我目前的代码:

private void displayDialog(DialogInterface.OnClickListener positive, DialogInterface.OnClickListener negative) {
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setMessage("Unexpected error, do you want to retry?")
            .setPositiveButton("Retry", positive)
            .setNegativeButton("Cancel", negative)
            .show();
}

private Observable<Boolean> notifyUser() {
    final PublishSubject<Boolean> subject = PublishSubject.create();
    displayDialog(
            (dialogInterface, i) -> subject.onNext(true),
            (dialogInterface, i) -> subject.onNext(false)
    );

    return subject;
}

private void onClick() {
    Log.d(TAG, "onClick");
    getData()
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.io())
            .retryWhen(attempts -> {
                return attempts.zipWith(
                        notifyUser(),
                        (throwable, res) -> res);
            })
            .subscribe(
                    s -> {
                        Log.d(TAG, "success"); …
Run Code Online (Sandbox Code Playgroud)

android rx-java2 retrywhen

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

Android 19模拟器上的API调用:isConnected失败:EHOSTUNREACH(无路由到主机)

我正在从API检索数据。当我尝试在运行Android API 19的Android模拟器上启动应用程序时,请求失败,并出现以下错误。使用其他版本的模拟器(例如Android API 27),它可以很好地工作。

我将URL更改为以先前项目中的另一个API为目标,并且可以正常工作。所以似乎问题出在这个特定的API上,但我不明白为什么,尤其是当我将URL传递到仿真器的浏览器中时,它运行良好。

我见过有人建议这是计算机防火墙的问题,但我的防火墙上没有启用防火墙。

翻新

interface SpaceXApi {
    @GET("rockets")
    fun getRockets(): Observable<MutableList<RocketDto>>
}

object SpaceXApiConstants {
    const val BASE_URL = "https://api.spacexdata.com/v3/"
}

// Interceptor passed to OkHttpClient Builder
class ConnectivityInterceptor(private val context: Context) : Interceptor {

    override fun intercept(chain: Interceptor.Chain): Response {
        if (isConnected()) {
            return chain.proceed(chain.request())
        } else {
            throw NoNetworkException()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

错误

D/OkHttp: --> GET https://api.spacexdata.com/v3/rockets
D/OkHttp: <-- HTTP FAILED: java.net.ConnectException: Failed to connect to api.spacexdata.com/2606:4700:30::681f:5749:443
W/System.err: java.net.ConnectException: Failed to connect to api.spacexdata.com/2606:4700:30::681f:5749:443 …
Run Code Online (Sandbox Code Playgroud)

android retrofit2

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

Retrofit 2 - 管理 http “204 No content” 和 “200” 具有相同请求的内容

我正在使用 Retrofit 2 和 OkHttp 3,以及 RxJava2

我正在使用一些参数调用服务器。根据参数,服务器返回一个带有对象列表的 JSON。但是如果没有什么可返回,则服务器不会返回带有空列表的 JSON,而是返回“204 - 无内容”。结果我得到一个 NullPointerException。

我知道如何管理 JSON 响应,我知道如何管理 204 响应,但不能一起管理同一个调用。

我可以使用拦截器,但由于我有很多调用,我的 Retrofit 客户端创建一次,然后在必要时使用 Dagger2 注入。

我该如何处理这个案例?

android dagger-2 retrofit2 okhttp3 rx-java2

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

当我将SimpleDateFormat类与“ YYYY”一起使用时,应用程序崩溃

我正在使用SimpleDateFormat类来获取这样的当前年份

SimpleDateFormat currentYear = new SimpleDateFormat("YYYY");
Date thisYear = new Date();
String stringThisYear  = currentYear .format(thisYear );
Run Code Online (Sandbox Code Playgroud)

它在这里(印度)工作正常,但是在其他人在马来西亚运行时崩溃了。经过大量调试和搜索,我发现由于没有提及语言环境而发生了问题,因此我将代码更改为此

SimpleDateFormat currentDate = new SimpleDateFormat("YYYY", Locale.ENGLISH);
Run Code Online (Sandbox Code Playgroud)

仍然很崩溃,所以我使用日历实例来获取这样的当前年份

 int thisYear=Calendar.getInstance().get(Calendar.YEAR);
Run Code Online (Sandbox Code Playgroud)

但是我想知道simpleDateFormat是什么问题,因为我的大多数代码已经具有simpleDateFormat类。所以我想知道崩溃的原因,如果有人有解决方案,请帮助我。谢谢

java android simpledateformat

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

每x个句子后打破一个字符串

我有一个很长的文本,我试着在每3个句子后打破它.

资源:

"Sentence 1. Sentence 2? Sentence 3! Sentence 4. Sentence 5. Sentence 6. Sentence 7. Sentence 8. Sentence 9. Sentence 10."

应该返回:

"Sentence 1. Sentence 2? Sentence 3! Sentence 4. Sentence 5. Sentence 6. Sentence 7. Sentence 8. Sentence 9. Sentence 10."

目前我的正则表达式(?<=[\.?!])\s匹配句子之间的所有空格.所以我可以使用它来拆分String然后迭代以添加换行符:

String[] splits = src.split(regex);
StringBuilder b = new StringBuilder();
int index = 0;
for (String s : splits) {
    if (index == 3) {
        b.append("\n");
        index = 0;
    } else if (index > …
Run Code Online (Sandbox Code Playgroud)

java regex android replaceall

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