Android应用程序崩溃,同时在改装响应中显示吐司

时间:2017-11-20 15:04:29

标签: android retrofit2

我正在使用Retrofit2进行Api通话。在改造Api响应时,我试图在收到错误消息时显示Toast。但该应用程序因抛出以下异常而崩溃。它工作正常,直到昨天。

FATAL EXCEPTION: main
    Process: com.manthra.geopulze, PID: 4502
    android.view.InflateException: Binary XML file line #27: Binary XML file line #27: Error inflating class TextView
    Caused by: android.view.InflateException: Binary XML file line #27: Error inflating class TextView
    Caused by: java.lang.ArrayIndexOutOfBoundsException: length=16; index=1297
    at android.content.res.StringBlock.get(StringBlock.java:65)
    at android.content.res.XmlBlock$Parser.getPooledString(XmlBlock.java:458)
    at android.content.res.TypedArray.loadStringValueAt(TypedArray.java:1212)
    at android.content.res.TypedArray.getString(TypedArray.java:202)
    at android.support.v7.widget.TintTypedArray.getString(TintTypedArray.java:143)
    at android.support.v7.widget.AppCompatTextHelper.updateTypefaceAndStyle(AppCompatTextHelper.java:215)
    at android.support.v7.widget.AppCompatTextHelper.loadFromAttributes(AppCompatTextHelper.java:152)
    at android.support.v7.widget.AppCompatTextHelperV17.loadFromAttributes(AppCompatTextHelperV17.java:38)
    at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:81)
    at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:71)
    at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:103)
    at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1024)
    at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1081)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:769)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
    at android.widget.Toast.makeText(Toast.java:264)
    at com.example.MainActivity.LoginFragment$4.onResponse(LoginFragment.java:228)
    at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

以下是Retrofit Api电话的代码片段。

Api.instance().login(requestLogin).enqueue(new Callback<Login>() {
        @Override
        public void onResponse(Call<Login> call, Response<Login> response) {

            if(response.code() == 200){
                // Success
            }else if(response.code() == 401) {
                Error error = ErrorHandler.parse(response);
                String message = error!=null?error.getMessage():"";
                Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show();//I am getting error in this line.
            }else{
                Toast.makeText(mActivity, "Authentication failed. Check your credentials and try again.", Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public void onFailure(Call<Login> call, Throwable t) {
            mProgressContainer.setVisibility(View.GONE);

        }
    });

1 个答案:

答案 0 :(得分:1)

android中的每个UI更新都必须在UI线程中执行:

mActivity.runOnUiThread(new Runnable() {
    @Override
    public void run() {
        Toast.makeText(mActivity, "Authentication failed. Check your credentials and try again.", Toast.LENGTH_LONG).show();
    }
});