我是
Android开发的新手,我想将一个按钮与动画相关联.我在runOnUiThread()和getApplication()附近收到错误.当我把它作为一个活动添加时它很好,但是当在MainFragment中声明时它会给出错误.但是,当我修复错误时,它会创建一个方法并返回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文件中,我只有facebook图像按钮.当我点击它时,它必须触发动画,然后onclick事件必须发生,但我不知道为什么会出现这个错误:
方法runOnUiThread(new Runnable(){})未定义类型new Thread(){}
并且靠近getapplication()方法
对于new Runnable(){}类型,未定义getApplication()方法