0

I'm new to android and Its programming. I want to implement AsyncTask inside android fragment, here I want to get my toast work.(for testing purpose) please help me and find below the code I used.When I try this I got the following Errors

log cat

 03-15 19:52:25.666: E/AndroidRuntime(18845): FATAL EXCEPTION: AsyncTask #1
    03-15 19:52:25.666: E/AndroidRuntime(18845): java.lang.RuntimeException: An error occured while executing doInBackground()
    03-15 19:52:25.666: E/AndroidRuntime(18845):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
    03-15 19:52:25.666: E/AndroidRuntime(18845):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
    03-15 19:52:25.666: E/AndroidRuntime(18845):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)

My Code

public class Login extends Fragment   {
        View view;
        ImageView button,pic;

        @SuppressLint("ValidFragment")
        public Login() {

        }
         @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            view= inflater.inflate(R.layout.login, container, false);
                    return view;
        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onActivityCreated(savedInstanceState);
            button=(ImageView) view.findViewById(R.id.login);
            pic=(ImageView) view.findViewById(R.id.pic);
            button.setOnClickListener(btnClick);

        }

        ImageView.OnClickListener btnClick=new ImageView.OnClickListener()
        {

            @SuppressWarnings("deprecation")
            @Override
            public void onClick(View v) {

                // my code begins here  
                if(fb.isSessionValid())
                {
                new MyAsyncTask().execute("");  

                }
                else
                {

                }
                        }

        };

        class MyAsyncTask extends AsyncTask<String, String, String>
        {

            @Override
            protected String doInBackground(String... params) {

                    Toast.makeText(getActivity(),"session valid" , Toast.LENGTH_LONG).show(); 

                return null;
            }

        }

    }
user3423301
  • 43
  • 1
  • 6

2 Answers2

0

You cant make a Toast in Background. Move your Toast to onPostExecute() or run the toast on the UI thread like this: (3rd answer)

How to raise a toast in AsyncTask, I am prompted to used the Looper

Community
  • 1
  • 1
Markus
  • 1,565
  • 6
  • 26
  • 57
0

The Toast is executed in the UI thread whereas the doInBackGround() method is run on a background thread. This is the reason for the error. Move the Toast to the onPostExecute() method. The onPostExecute() method takes the result of the doInBackGround() method nad hence for you it would look something like:

@Override
public void onPostExecute(String result) {
    // make Toast
}

Keep in mind that the doInBackGround method must mandatorily be overriden. So you will still need it.

ucsunil
  • 7,378
  • 1
  • 27
  • 32