7

I am new to Android development and I want to link a button with the animation. I am getting error near runOnUiThread() and getApplication(). When I add this in as an activity it is fine, but when declared in MainFragment it gives error. However when I fix the errors, it creates a method and returns false.

public class MainFragment extends Fragment {


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

    View rootView = inflater.inflate(R.layout.activity_main, container, false);
    ImageButton btnFacebook = (ImageButton)rootView.findViewById(R.id.facebook2);
    final Animation alpha = AnimationUtils.loadAnimation(getActivity(), R.anim.anim_alpha);

    btnFacebook.setOnClickListener(new Button.OnClickListener(){

        @Override
        public void onClick(View arg0) {
            arg0.startAnimation(alpha);

            Thread thread = new Thread()
            {
                @Override               
                public void run()
                {
                    try
                    {
                        Thread.sleep(1000);
                    }catch(InterruptedException e){                         
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                          startActivity(new Intent(getApplication(),FacebookActivity.class)); 
                        }

                    });
                }
            };


            thread.start();
          }}); 
    return rootView;
}}

In the XML file I only have the facebook imagebutton. When I click that, it has to trigger the animation and then onclick event has to happen, but I don't know why this error is popping up:

The method runOnUiThread(new Runnable(){}) is undefined for the type new Thread(){}

And near getapplication() method The method getApplication() is undefined for the type new Runnable(){}

If I create the two methods the error goes away, but then when I click onto the button it will not go to the facebookActivity.java file.

Can anyone tell/help what should I add to solve this issue. Thanks.

Ahmed Masud
  • 21,655
  • 3
  • 33
  • 58
Sandeep V
  • 463
  • 1
  • 11
  • 20
  • 1
    Take a look at the answer on this: [RunOnUIThread in fragment][1] [1]: http://stackoverflow.com/questions/16425146/runonuithread-in-fragment – andMarkus Apr 09 '14 at 11:40

3 Answers3

10

runOnUIThread(...) is a method of Activity.

Therefore, use this:

getActivity().runOnUIThread(...);

But, beware. You're dealing with asynchronous threads, so your Fragment might be detached from its Activity, resulting in getActivity() returning null. You might want to add a check if(isAdded()) or if(getActivity() != null) before executing.

Alternatively, use an AsyncTask, which handles this running on the ui thread by itself.

nhaarman
  • 98,571
  • 55
  • 246
  • 278
  • is it necessary to use checking up getActivity and AsyncTask can u explain little about this pls @Niek Haarman i am asking in depth because i changed the above code what u said now it is working fine will it lead to problem in future – Sandeep V Apr 09 '14 at 11:53
  • can u please help to solve this http://stackoverflow.com/questions/22986878/android-json-is-not-able-to-retrieve-any-files-from-mysql-database-it-is-empty?noredirect=1#comment35104130_22986878 – Sandeep V Apr 10 '14 at 11:56
2
runOnUiThread(new Runnable(){}) is method in Activity not in the Fragment.

so you need to change

runOnUiThread(new Runnable(){})

into

getActivity().runOnUiThread(new Runnable(){})

For your requrement it is better to use Handler instead of Thread for waiting 1000 milli seconds..

kalyan pvs
  • 14,486
  • 4
  • 41
  • 59
  • k i will check and let you know whether it is working @kalyan pvs – Sandeep V Apr 09 '14 at 11:41
  • thank you @kalyan pvs i inserted an dummy file in my project and checked it and when i click it it is going to that dummy file – Sandeep V Apr 09 '14 at 12:08
  • http://stackoverflow.com/questions/22986878/android-json-is-not-able-to-retrieve-any-files-from-mysql-database-it-is-empty?noredirect=1#comment35104130_22986878 ....pls help me solve this @kalyan pvs – Sandeep V Apr 10 '14 at 11:57
2

Please use getActivity() instead of using getApplication() .

which returns the activity associated with a fragment. The activity is a context.

Lavekush Agrawal
  • 6,040
  • 7
  • 52
  • 85