0

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)
Community
  • 1
  • 1
styx
  • 1,852
  • 1
  • 11
  • 22

3 Answers3

0

You are probably getting an Exception because you are trying to display a Dialog from a background Thread. You should check if the email is valid inside onPreExecute() and if it is not display the dialog then since it runs on the UI Thread.

Emmanuel
  • 13,083
  • 4
  • 39
  • 53
  • `onPreExecute()` did not work, and you are right there is why im getting the `Exception` but i dont know how to solve it – styx Nov 27 '16 at 19:24
  • I don't know what you mean by "did not work". Can you be more specific? – Emmanuel Nov 27 '16 at 19:25
  • `java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.` – styx Nov 27 '16 at 19:27
  • For the AlertDialog class, import the support.v7 version instead of the standard one. When you post a question like this, please include the stactrace from your logcat. – rhari Nov 27 '16 at 19:29
  • Please post the stacktrace as part of your question. This stacktrace suggest your `AsyncTask` is not even running. – Emmanuel Nov 27 '16 at 19:29
  • i cant post the whole stack trace,it's way to long – styx Nov 27 '16 at 19:36
  • The portion of stacktrace you shared indicates that you are using the wrong `Theme` for the `Activity`. – Emmanuel Nov 27 '16 at 19:37
  • add to the original post – styx Nov 27 '16 at 19:41
0

Edit : Make sure your activity is extending AppCompatActivity and you are importing android.support.v7.app.AlertDialog instead of the standard one. Then, change the AsyncTask as below :

The doInBackground() method can perform long-running operations but cannot interact with the UI. For that, you should use the onPreExecute() and onPostExecute() methods, like this :

new AsyncTask<Void, Void, Boolean>() {
        protected Boolean doInBackground(Void... params) {
            if( !isValidEmail(email))
                return false;
            return true;
        }

        protected void onPostExecute(Boolean isEmailValid) {
            //isEmailValid is the variable received from the doInBackground() method
            if(!isEmailValid)
                LoginActivity.alertDialog(getApplicationContext(),"test","test");
        }
    }.execute();
rhari
  • 1,367
  • 1
  • 11
  • 19
  • getting the same `error java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.` at `com.example.ofir.bopofinal.LoginRegister.LoginActivity.alertDialog(LoginActivity.java:53)` – styx Nov 27 '16 at 19:28
  • @styx - please post the stacktrace of your crash. rhari's answer seems correct, concept-wise. Perhaps the crash's reason is something different. – Ridcully Nov 27 '16 at 19:32
  • @rhari my app is already extending AppCompatActivity and importing importing android.support.v7.app.AlertDialog – styx Nov 27 '16 at 20:12
0

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.

Ridcully
  • 23,362
  • 7
  • 71
  • 86
  • i did `android:theme="@style/Theme.AppCompat">` in the manifest.xml and im getting the same result, the error is pointing to the `.show()` in the `alertDialog` function – styx Nov 27 '16 at 20:09
  • I think I found the problem -- see my edited answer. – Ridcully Nov 27 '16 at 20:26
  • Yeah, you should perhaps get rid of the static method, it possible. You could perhaps move the alertDialog method to the RegisterActivity class or some base class for both Activities, if you use it in both of them. Thanks for accepting my answer, btw. – Ridcully Nov 27 '16 at 20:38
  • both activities need a major retouch – styx Nov 27 '16 at 20:40