Search code examples
javaandroidmultithreadingandroid-textwatcher

Android MultiThreading with afterTextChanged


i started to work with MultiThreading as and i wanted to go validate user input so i looked at the question here

but still from some reason my apps crashed and i can't pinpoint to the problem

RegisterActivity.Java

@Override
    public void afterTextChanged(final Editable editable) {
        email = etEmail.getText().toString();
        new AsyncTask<Void, Void, Void>() {
            protected Void doInBackground(Void... params) {
                if( !isValidEmail(email))
                    LoginActivity.alertDialog(getApplicationContext(),"test","test");
                    return null;
            }

        }.execute();
    }

LoginActivity.java (and yes i know that this function shouldn't be here)

  public static   void alertDialog(Context context, CharSequence message, CharSequence type){
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setMessage(message)
                .setNegativeButton(type, null)
                .create()
                .show();
    }

stacktrace

E/AndroidRuntime: FATAL EXCEPTION: main Process:com.example.ofir.bopofinal, PID: 3038
                                                                                  java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
                                                                                      at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:351)
                                                                                      at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:320)
                                                                                      at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:281)
                                                                                      at android.support.v7.app.AppCompatDialog.setContentView(AppCompatDialog.java:83)
                                                                                      at android.support.v7.app.AlertController.installContent(AlertController.java:214)
                                                                                      at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:258)
                                                                                      at android.app.Dialog.dispatchOnCreate(Dialog.java:394)
                                                                                      at android.app.Dialog.show(Dialog.java:295)
                                                                                      at com.example.ofir.bopofinal.LoginRegister.LoginActivity.alertDialog(LoginActivity.java:53)
                                                                                      at com.example.ofir.bopofinal.LoginRegister.RegisterActivity$3.onPostExecute(RegisterActivity.java:162)
                                                                                      at com.example.ofir.bopofinal.LoginRegister.RegisterActivity$3.onPostExecute(RegisterActivity.java:153)
                                                                                      at android.os.AsyncTask.finish(AsyncTask.java:651)
                                                                                      at android.os.AsyncTask.-wrap1(AsyncTask.java)
                                                                                      at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                      at android.os.Looper.loop(Looper.java:148)
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)

Solution

  • So, just do what the Exception tells you:

    You need to use a Theme.AppCompat theme (or descendant) with this activity.

    EDIT:

    You are passing the ApplicationContext to AlertDialog.Builder() constructor. The builder tries to get it's theme from that context and as it's the ApplicationContext and not the Activity, it will take the theme of the application itself, which seems not to be Theme.AppCompat or a descendent.

    Without rewriting to much of your code, it should work, if you pass this resp. (when calling from within AyncTask) RegisterActivity.this to alertDialog() as the context to use.