安卓:越来越runOnUiThread附近的错误,当我用它附近片段 [英] Android: getting error near runOnUiThread when i use it near fragment

查看:647
本文介绍了安卓:越来越runOnUiThread附近的错误,当我用它附近片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新Android开发,我想与动画链接按钮。我越来越近 runOnUiThread错误() getApplication()。当我添加这作为一项活动是好的,但在 MainFragment 宣布时,它提供错误。然而,当我修复错误,它创建了一个方法,并返回false。

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;
}}

在XML文件中我只有脸谱的ImageButton。当我点击,它有触发动画,然后onclick事件已发生,但我不知道为什么这个错误弹出:

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:

的方法runOnUiThread(新的Runnable(){})是未定义该类型新的Thread(){}

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

和附近getapplication()方法
的方法getApplication()是未定义该类型新的Runnable(){}

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

如果我创建了两个方法错误消失,但后来当我点击到按钮,它会不会去facebookActivity.java文件。

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.

推荐答案

runOnUIThread(...)活动的方法

因此​​,用这个:

getActivity().runOnUIThread(...);

不过,要小心了。你处​​理异步线程,所以你的片段可能是从活动超脱,致使 getActivity()返回。您可能需要之前增加一个检查如果(isAdded())如果(getActivity()!= NULL)执行

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.

另外,使用的AsyncTask ,这本身处理UI线程上运行这个

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

这篇关于安卓:越来越runOnUiThread附近的错误,当我用它附近片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆