Scheduler.Worker线程抛出致命异常

时间:2018-03-04 23:01:07

标签: java android android-studio gson rx-java

我正在进行大学四年级的最后一年项目,而且我似乎一直遇到同样的问题。我正在创建一个帮助用户戒烟的应用程序。我正在使用我在互联网上找到的登录系统。当我运行代码时,它给了我这个错误。当我尝试以现有用户身份登录或创建新用户时,会发生错误。我正在为我的项目使用Node.js后端。

我很快就需要帮助。

    E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.learn2crack, PID: 3609
                  java.lang.IllegalStateException: Fatal Exception thrown on Scheduler.Worker thread.
                      at rx.android.schedulers.LooperScheduler$ScheduledAction.run(LooperScheduler.java:114)
                      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)
                   Caused by: rx.exceptions.OnErrorFailedException: Error occurred when trying to propagate error to Observer.onError
                      at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:186)
                      at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:115)
                      at rx.internal.operators.OperatorSubscribeOn$1$1.onError(OperatorSubscribeOn.java:59)
                      at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.checkTerminated(OperatorObserveOn.java:276)
                      at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.call(OperatorObserveOn.java:219)
                      at rx.android.schedulers.LooperScheduler$ScheduledAction.run(LooperScheduler.java:107)
                      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) 
                   Caused by: rx.exceptions.CompositeException: 2 exceptions occurred. 
                      at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:186) 
                      at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:115) 
                      at rx.internal.operators.OperatorSubscribeOn$1$1.onError(OperatorSubscribeOn.java:59) 
                      at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.checkTerminated(OperatorObserveOn.java:276) 
                      at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.call(OperatorObserveOn.java:219) 
                      at rx.android.schedulers.LooperScheduler$ScheduledAction.run(LooperScheduler.java:107) 
                      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) 
                   Caused by: rx.exceptions.CompositeException$CompositeExceptionCausalChain: Chain of Causes for CompositeException In Order Received =>
                      at android.util.Log.printlns(Log.java:415)
                      at com.android.internal.os.RuntimeInit.Clog_e(RuntimeInit.java:61)
                      at com.android.internal.os.RuntimeInit.-wrap0(RuntimeInit.java)
                      at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:86)
                      at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:1068)
                      at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:1063)
                      at rx.android.schedulers.LooperScheduler$ScheduledAction.run(LooperScheduler.java:118)
                      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) 
                   Caused by: retrofit2.adapter.rxjava.HttpException: HTTP 404 Not Found
                      at retrofit2.adapter.rxjava.OperatorMapResponseToBodyOrError$1.onNext(OperatorMapResponseToBodyOrError.java:43)
                      at retrofit2.adapter.rxjava.OperatorMapResponseToBodyOrError$1.onNext(OperatorMapResponseToBodyOrError.java:38)
                      at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$RequestArbiter.request(RxJavaCallAdapterFactory.java:173)
                      at rx.internal.operators.OperatorSubscribeOn$1$1$1.request(OperatorSubscribeOn.java:80)
                      at rx.Subscriber.setProducer(Subscriber.java:211)
                      at rx.internal.operators.OperatorSubscribeOn$1$1.setProducer(OperatorSubscribeOn.java:76)
                      at rx.Subscriber.setProducer(Subscriber.java:205)
                      at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:152)
                      at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:138)
                      at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
                      at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
                      at rx.Observable.unsafeSubscribe(Observable.java:10151)
                      at rx.internal.operators.OperatorSubscribeOn$1.call(OperatorSubscribeOn.java:94)
                      at rx.internal.schedulers.CachedThreadScheduler$EventLoopWorker$1.call(CachedThreadScheduler.java:228)
                      at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
                      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:428)
                      at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                      at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:272)
                      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
                      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
                      at java.lang.Thread.run(Thread.java:761)
                   Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
E/AndroidRuntime:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:224)
                      at com.google.gson.Gson.fromJson(Gson.java:887)
                      at com.google.gson.Gson.fromJson(Gson.java:852)
                      at com.google.gson.Gson.fromJson(Gson.java:801)
                      at com.google.gson.Gson.fromJson(Gson.java:773)
                      at com.learn2crack.fragments.LoginFragment.handleError(LoginFragment.java:157)
                      at com.learn2crack.fragments.LoginFragment.-com_learn2crack_fragments_LoginFragment-mthref-1(LoginFragment.java:126)
                      at com.learn2crack.fragments.LoginFragment$-void_loginProcess_java_lang_String_email_java_lang_String_password_LambdaImpl1.call(LoginFragment.java)
                      at rx.internal.util.ActionSubscriber.onError(ActionSubscriber.java:44)
                      at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:152)
                      at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:115)
                      at rx.internal.operators.OperatorSubscribeOn$1$1.onError(OperatorSubscribeOn.java:59)
                      at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.checkTerminated(OperatorObserveOn.java:276)
                      at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.call(OperatorObserveOn.java:219)
                      at rx.android.schedulers.LooperScheduler$ScheduledAction.run(LooperScheduler.java:107)
                      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)
                   Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
                      at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:385)
                      at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:213)
                      at com.google.gson.Gson.fromJson(Gson.java:887) 
                      at com.google.gson.Gson.fromJson(Gson.java:852) 
                      at com.google.gson.Gson.fromJson(Gson.java:801) 
                      at com.google.gson.Gson.fromJson(Gson.java:773) 
                      at com.learn2crack.fragments.LoginFragment.handleError(LoginFragment.java:157) 
                      at com.learn2crack.fragments.LoginFragment.-com_learn2crack_fragments_LoginFragment-mthref-1(LoginFragment.java:126) 
                      at com.learn2crack.fragments.LoginFragment$-void_loginProcess_java_lang_String_email_java_lang_String_password_LambdaImpl1.call(LoginFragment.java) 
                      at rx.internal.util.ActionSubscriber.onError(ActionSubscriber.java:44) 
                      at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:152) 
                      at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:115) 
                      at rx.internal.operators.OperatorSubscribeOn$1$1.onError(OperatorSubscribeOn.java:59) 
                      at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.checkTerminated(OperatorObserveOn.java:276) 
                      at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.call(OperatorObserveOn.java:219) 
                      at rx.android.schedulers.LooperScheduler$ScheduledAction.run(LooperScheduler.java:107) 
                      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) 
Application terminated.





package com.learn2crack.fragments;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.learn2crack.MainActivity;
import com.learn2crack.ProfileActivity;
import com.learn2crack.R;
import com.learn2crack.model.Response;
import com.learn2crack.model.User;
import com.learn2crack.network.NetworkUtil;
import com.learn2crack.socialmediaapp.mainMenuPage;
import com.learn2crack.socialmediaapp.signUpRequirementsPage;
import com.learn2crack.socialmediaapp.welcomePage;

import java.io.IOException;

import retrofit2.adapter.rxjava.HttpException;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

import static com.learn2crack.utils.Validation.validateEmail;
import static com.learn2crack.utils.Validation.validateFields;

public class RegisterFragment extends Fragment{

    public static final String TAG = RegisterFragment.class.getSimpleName();

    private EditText mEtName;
    private EditText mEtEmail;
    private EditText mEtPassword;
    private Button   mBtRegister;
    private TextView mTvLogin;
    private TextInputLayout mTiName;
    private TextInputLayout mTiEmail;
    private TextInputLayout mTiPassword;
    private ProgressBar mProgressbar;

    private CompositeSubscription mSubscriptions;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_register,container,false);
        mSubscriptions = new CompositeSubscription();
        initViews(view);
        return view;
    }

    private void initViews(View v) {

        mEtName = (EditText) v.findViewById(R.id.et_name);
        mEtEmail = (EditText) v.findViewById(R.id.et_email);
        mEtPassword = (EditText) v.findViewById(R.id.et_password);
        mBtRegister = (Button) v.findViewById(R.id.btn_register);
        mTvLogin = (TextView) v.findViewById(R.id.tv_login);
        mTiName = (TextInputLayout) v.findViewById(R.id.ti_name);
        mTiEmail = (TextInputLayout) v.findViewById(R.id.ti_email);
        mTiPassword = (TextInputLayout) v.findViewById(R.id.ti_password);
        mProgressbar = (ProgressBar) v.findViewById(R.id.progress);

        mBtRegister.setOnClickListener(view -> register());
        mTvLogin.setOnClickListener(view -> goToLogin());
    }

    private void register() {

        setError();

        String name = mEtName.getText().toString();
        String email = mEtEmail.getText().toString();
        String password = mEtPassword.getText().toString();

        int err = 0;

        if (!validateFields(name)) {

            err++;
            mTiName.setError("Name should not be empty !");
        }

        if (!validateEmail(email)) {

            err++;
            mTiEmail.setError("Email should be valid !");
        }

        if (!validateFields(password)) {

            err++;
            mTiPassword.setError("Password should not be empty !");
        }

        if (err == 0) {

            User user = new User();
            user.setName(name);
            user.setEmail(email);
            user.setPassword(password);

            mProgressbar.setVisibility(View.VISIBLE);
            registerProcess(user);
/*            Intent signUpContinueIntent;
            signUpContinueIntent = new Intent(RegisterFragment.this, signUpRequirementsPage.class);
            startActivity(signUpContinueIntent);*/
        } else {

            showSnackBarMessage("Enter Valid Details !");
        }
    }

    private void setError() {

        mTiName.setError(null);
        mTiEmail.setError(null);
        mTiPassword.setError(null);
    }

    private void registerProcess(User user) {

        mSubscriptions.add(NetworkUtil.getRetrofit().register(user)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(this::handleResponse,this::handleError));
    }

    private void handleResponse(Response response) {

        mProgressbar.setVisibility(View.GONE);
        showSnackBarMessage(response.getMessage());
    }

    private void handleError(Throwable error) {

        mProgressbar.setVisibility(View.GONE);

        if (error instanceof HttpException) {

            Gson gson = new GsonBuilder().create();

            try {

                String errorBody = ((HttpException) error).response().errorBody().string();
                Response response = gson.fromJson(errorBody,Response.class);
                showSnackBarMessage(response.getMessage());

            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {

            showSnackBarMessage("Network Error !");
        }
    }

    private void showSnackBarMessage(String message) {

        if (getView() != null) {

            Snackbar.make(getView(),message,Snackbar.LENGTH_SHORT).show();
        }
    }

    private void goToLogin(){

        FragmentTransaction ft = getFragmentManager().beginTransaction();
        LoginFragment fragment = new LoginFragment();
        ft.replace(R.id.fragmentFrame, fragment, LoginFragment.TAG);
        ft.commit();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mSubscriptions.unsubscribe();
    }
}

0 个答案:

没有答案