I upload a png image via Picasso via url, just before relativeLayout.setBackground throws an exception that I use the main stream, the call to SetBackground goes in the OnCreate method.

Tell me what could be the error

E / AndroidRuntime: FATAL EXCEPTION: AsyncTask # 1 java.lang.RuntimeException: An error occured while executing doInBackground () at android.os.AsyncTask $ 3.done (AsyncTask.java around99) at java.util.concur.FutureTask .innerSetException (FutureTask.java:273) at java.util.concurrent.FutureTask.setException (FutureTask.java:124) at java.util.concurrent.FutureTask $ Sync.innerRun (FutureTask.java:307) at java.util. concurrent.FutureTask.run (FutureTask.java:137) at android.os.AsyncTask $ SerialExecutor $ 1.run (AsyncTask.java:230) at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.javaada) util.concurrent.ThreadPoolExecutor $ Worker.run (ThreadPoolExecutor.javament69) at java.lang.Thread.run (Thread.java:856) Caused by: java.lang.IllegalStateException: at com.squareup.picasso.Utils.checkMain (Utils.java:136) at com.squareup.picasso.RequestCreator.into (RequestCreator.java:496) at com.example.user_android.otvchat.MainActivity $ SetBackground.doInBackground (MainActivity .java: 386) at com.example.user_android.otvchat.MainActivity $ SetBackground.doInBackground (MainActivity.java=67) at android.os.AsyncTask $ 2.call (AsyncTask.java:287) at java.util.concurrent.FutureTask $ Sync.innerRun (FutureTask.java:305) at java.util.concurrent.FutureTask.run (FutureTask.java:137) at android.os.AsyncTask $ SerialExecutor $ 1.run (AsyncTask.java:230) at java.util .concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1076) at java.util.concurrent.ThreadPoolExecutor $ Worker.run (ThreadPoolExecutor.javament69) at java.lang.Thread.run (Thread.java:856)

 public class MainActivity extends AppCompatActivity { private ImageButton btn; private TextView txtMain; private TextView txtSecond; private TextView txtSecond2; private EditText editPhone; private EditText editCode; private Button back; private boolean numberPhone = true; private boolean saveNumber = false; public static String NUMBER_PHONE = ""; public static String NUMBER_PHONE2 = ""; public static String userID; private int code; private int editCodePush; private boolean rules; SharedPreferences sPref; String url; final String SAVED_PHONE = "saved_phone"; RelativeLayout relativeLayout; String URL = "****"; /** * ATTENTION: This was auto-generated to implement the App Indexing API. * See https://g.co/AppIndexing/AndroidStudio for more information. */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = (ImageButton) findViewById(R.id.imageButton3); txtMain = (TextView) findViewById(R.id.textView5); txtSecond = (TextView) findViewById(R.id.textView3); txtSecond2 = (TextView) findViewById(R.id.textView4); editPhone = (EditText) findViewById(R.id.editText); editCode = (EditText) findViewById(R.id.editText2); back = (Button) findViewById(R.id.button2); back.setVisibility(View.INVISIBLE); editCode.setVisibility(View.INVISIBLE); txtSecond2.setVisibility(View.INVISIBLE); txtSecond.setVisibility(View.INVISIBLE); editPhone.addTextChangedListener(new MaskedWatcher("+38 (###) ###-##-##")); setTitle("Авторизация"); relativeLayout = (RelativeLayout)findViewById(R.id.activity_main); sPref = getPreferences(MODE_PRIVATE); Picasso.with(getApplicationContext()).load("http://sms.binovery.gq/img/heart-back.png").into(new Target(){ @Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { relativeLayout.setBackground(new BitmapDrawable(getApplicationContext().getResources(), bitmap)); Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_SHORT).show(); } @Override public void onBitmapFailed(final Drawable errorDrawable) { Toast.makeText(getApplicationContext(),"Error", Toast.LENGTH_SHORT).show(); } @Override public void onPrepareLoad(final Drawable placeHolderDrawable) { Toast.makeText(getApplicationContext(),"Loading...", Toast.LENGTH_SHORT).show(); } }); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (saveNumber) { if (editPhone.getText().toString().equals(sPref.getString(SAVED_PHONE, ""))) { NUMBER_PHONE = editPhone.getText().toString(); new ShowDialogAsnc().execute(); SystemClock.sleep(1000); Intent intent = new Intent(getApplicationContext(), NavigationDrawer.class); intent.putExtra("regulations_btn", R.id.button6); startActivity(intent); } } else if (true) { new ShowDialogAsnc().execute(); getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); Spannable text = new SpannableString(txtSecond.getText()); text.setSpan(new UnderlineSpan(), 100, 104, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); txtSecond.setText(text); txtSecond.setTextColor(Color.WHITE); editPhone.setVisibility(View.INVISIBLE); txtMain.setVisibility(View.INVISIBLE); back.setVisibility(View.VISIBLE); editCode.setVisibility(View.VISIBLE); txtSecond2.setVisibility(View.VISIBLE); txtSecond.setVisibility(View.VISIBLE); NUMBER_PHONE = editPhone.getText().toString(); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { try { editCodePush = Integer.parseInt(editCode.getText().toString()); if (editCodePush == code) { sPref = getPreferences(MODE_PRIVATE); SharedPreferences.Editor ed = sPref.edit(); ed.putString(SAVED_PHONE, NUMBER_PHONE.toString()); ed.commit(); if (!rules) { Intent intent = new Intent(getApplicationContext(), NavigationDrawer.class); intent.putExtra("regulations_btn", R.id.button6); startActivity(intent); } else { Intent intent = new Intent(getApplicationContext(), Regulations.class); startActivity(intent); } } else { Toast.makeText(getApplicationContext(), "Не верно введен код", Toast.LENGTH_SHORT).show(); } } catch (NumberFormatException e) { Toast.makeText(getApplicationContext(), "Не верно введен код", Toast.LENGTH_SHORT).show(); } } }); } else { Toast.makeText(getApplicationContext(), "Номер телефон введен не корректно", Toast.LENGTH_LONG).show(); } } }); back.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { back.setVisibility(View.INVISIBLE); editCode.setVisibility(View.INVISIBLE); txtSecond2.setVisibility(View.INVISIBLE); txtSecond.setVisibility(View.INVISIBLE); editPhone.setVisibility(View.VISIBLE); txtMain.setVisibility(View.VISIBLE); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (true) { new ShowDialogAsnc().execute(); getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); Spannable text = new SpannableString(txtSecond.getText()); text.setSpan(new UnderlineSpan(), 100, 104, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); txtSecond.setText(text); txtSecond.setTextColor(Color.WHITE); editPhone.setVisibility(View.INVISIBLE); txtMain.setVisibility(View.INVISIBLE); back.setVisibility(View.VISIBLE); editCode.setVisibility(View.VISIBLE); txtSecond2.setVisibility(View.VISIBLE); txtSecond.setVisibility(View.VISIBLE); NUMBER_PHONE = editPhone.getText().toString(); editCode.setText(""); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (numberPhone) { Intent intent = new Intent(getApplicationContext(), Regulations.class); startActivity(intent); } } }); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { try { editCodePush = Integer.parseInt(editCode.getText().toString()); if (editCodePush == code) { if (!rules) { Intent intent = new Intent(getApplicationContext(), NavigationDrawer.class); intent.putExtra("regulations_btn", R.id.button6); startActivity(intent); } else { Intent intent = new Intent(getApplicationContext(), Regulations.class); startActivity(intent); } } else { Toast.makeText(getApplicationContext(), "Не верно введен код", Toast.LENGTH_SHORT).show(); } } catch (NumberFormatException e) { Toast.makeText(getApplicationContext(), "Не верно введен код", Toast.LENGTH_SHORT).show(); } } }); } } }); } }); } private class ShowDialogAsnc extends AsyncTask<Void, Integer, Void> { String phone; JSONObject jsonObjectCode; @Override protected void onPreExecute() { phone = editPhone.getText().toString(); super.onPreExecute(); } @Override protected Void doInBackground(Void... voids) { try { NUMBER_PHONE = phone; jsonObjectCode = new JSONObject(new PostExample().PostExampleMethod("phone_number", phone)); SystemClock.sleep(1000); code = jsonObjectCode.getInt("code"); rules = jsonObjectCode.isNull("read_rules"); userID = jsonObjectCode.getString("id"); } catch (JSONException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Void result) { super.onPostExecute(result); } } 

    1 answer 1

    here is just before relativeLayout.setBackground throws an exception that I use main stream

    The exception just says the opposite:

    Method call should happen from the main thread

    The fact that you create and run SetBackground extends AsyncTask to onCreate (that is, from the main thread) in this case does not mean anything, since the doInBackground(...) AsyncTask is already running not in the main thread.

    When uploading images using Picasso, there is no need to create a new stream by yourself, since Picasso is already working asynchronously.

    The relativeLayout.setBackground and Toast.show() methods can only be called from the main thread, and you try to call them in the doInBackground(...) AsyncTask , which is what you are told in the stack trace.

    To solve the problem, simply remove AsyncTask and upload the images directly in the onCreate(...) method.

    UPD . Picasso stores the link to the Target instance in the form of weak reference , which can be cleaned with the Garbage collector. This problem can be solved by making the link to Target strong.

    The first way:

    Add

     private Target mTarget; 

    as a field of class MainActivity .

    Next, in the OnCreate() method, create an mTarget :

     mTarget = new Target() { @Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { relativeLayout.setBackground(new BitmapDrawable(getApplicationContext().getResources(), bitmap)); Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_SHORT).show(); } @Override public void onBitmapFailed(final Drawable errorDrawable) { Toast.makeText(getApplicationContext(),"Error", Toast.LENGTH_SHORT).show(); } @Override public void onPrepareLoad(final Drawable placeHolderDrawable) { Toast.makeText(getApplicationContext(),"Loading...", Toast.LENGTH_SHORT).show(); } }; 

    and then upload the image:

     Picasso.with(getApplicationContext()).load("http://sms.binovery.gq/img/heart-back.png").into(mTarget); 

    The second way:

    Implement the Target interface in your MainActivity :

     public class MainActivity extends AppCompatActivity implements Target { ... @Override protected void onCreate(Bundle savedInstanceState) { ... } @Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { relativeLayout.setBackground(new BitmapDrawable(getApplicationContext().getResources(), bitmap)); Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_SHORT).show(); } @Override public void onBitmapFailed(final Drawable errorDrawable) { Toast.makeText(getApplicationContext(),"Error", Toast.LENGTH_SHORT).show(); } @Override public void onPrepareLoad(final Drawable placeHolderDrawable) { Toast.makeText(getApplicationContext(),"Loading...", Toast.LENGTH_SHORT).show(); } ... } 

    And in the onCreate(...) method, load the image:

     Picasso.with(getApplicationContext()).load("http://sms.binovery.gq/img/heart-back.png").into(this); 
    • Rewritten in OnCreate, only after calling and loading the image the background does not change, only Toast is displayed which is in onPrepareLoad (Loading ...) - Heaven
    • @Heaven, Show all the code for this activation. - post_zeew Nov.
    • Updated code above - Heaven
    • @Heaven, See UPD . - post_zeew
    • Try running asynctask with this method executeOnExecutor (AsyncTask.THREAD_POOL_EXECUTOR) - pavel163