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.
我是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;
}}
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:
在XML文件中,我只有facebook图像按钮。当我点击它时,它必须触发动画,然后onclick事件必须发生,但我不知道为什么会出现这个错误:
The method runOnUiThread(new Runnable(){}) is undefined for the type new Thread(){}
方法runOnUiThread(new Runnable(){})未定义类型new Thread(){}
And near getapplication() method The method getApplication() is undefined for the type new Runnable(){}
并且接近getapplication()方法对于new Runnable(){}类型,未定义getApplication()方法
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.
如果我创建了两个方法,则错误消失,但是当我单击按钮时,它将不会转到facebookActivity.java文件。
Can anyone tell/help what should I add to solve this issue. Thanks.
任何人都可以告诉/帮助我应该添加什么来解决这个问题。谢谢。
3 个解决方案
#1
8
runOnUIThread(...)
is a method of Activity
.
runOnUIThread(...)是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.
但是,要小心。您正在处理异步线程,因此您的Fragment可能与其Activity分离,导致getActivity()返回null。您可能希望在执行之前添加检查if(isAdded())或if(getActivity()!= null)。
Alternatively, use an AsyncTask
, which handles this running on the ui thread by itself.
或者,使用AsyncTask,它自己处理在ui线程上运行的这个。
#2
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..
对于您的要求,最好使用Handler而不是Thread来等待1000毫秒..
#3
2
Please use getActivity()
instead of using getApplication()
.
请使用getActivity()而不是使用getApplication()。
which returns the activity associated with a fragment. The activity is a context.
返回与片段关联的活动。活动是一个背景。
#1
8
runOnUIThread(...)
is a method of Activity
.
runOnUIThread(...)是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.
但是,要小心。您正在处理异步线程,因此您的Fragment可能与其Activity分离,导致getActivity()返回null。您可能希望在执行之前添加检查if(isAdded())或if(getActivity()!= null)。
Alternatively, use an AsyncTask
, which handles this running on the ui thread by itself.
或者,使用AsyncTask,它自己处理在ui线程上运行的这个。
#2
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..
对于您的要求,最好使用Handler而不是Thread来等待1000毫秒..
#3
2
Please use getActivity()
instead of using getApplication()
.
请使用getActivity()而不是使用getApplication()。
which returns the activity associated with a fragment. The activity is a context.
返回与片段关联的活动。活动是一个背景。