问题描述
就像我的标题所说,我在找的等效 getActivity()
在我的 ActionBarActivity
班我的 Android项目
。
Like my title says, i'm looking for an equivalent of getActivity()
in my ActionBarActivity
class in my Android project
.
我想传递一个活动
参数的AsyncTask
申报对象,因为我使用了<$ C在我的自定义$ C>活动对象的AsyncTask
扩展分类
I want to pass an Activity
parameter in AsyncTask
declaration object, because i'm using an Activity
object in my custom AsyncTask
extended class
下面我的项目的一个例子简单的code
Here an example simplest code of my project
public class EventCreator extends ActionBarActivity {
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_even_creator);
View v = getLayoutInflater().inflate(R.layout.activity_even_creator,null);
this.context = this.getBaseContext();
final Button createButton = (Button)findViewById(R.id.createEventButton);
createButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AsyncTask<Void,Void,Boolean> eventCreatorSend = new SendEvents(/* here need activity object */);
eventCreatorSend.execute();
}
});
}
class SendEvents extends AsyncTask<Void,Void,Boolean> {
public Activity act;
SendEvents(Activity a) {
this.act = a;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
((LinearLayout)act.findViewById(R.id.layout_loader_create_event)).setVisibility(View.VISIBLE);
}
@Override
protected Boolean doInBackground(Void... params) {
SystemClock.sleep(5000);
return true;
}
@Override
protected void onPostExecute(Boolean params) {
if (params){
((LinearLayout)act.findViewById(R.id.layout_loader_create_event)).setVisibility(View.GONE);
act.finish();
}
else {
((LinearLayout)act.findViewById(R.id.layout_loader_create_event)).setVisibility(View.VISIBLE);
Toast.makeText(act,"Fail to send event",Toast.LENGTH_SHORT).show();
}
}
};
}
在一个时间,我想用的getParent()
从 ActionBarActivity
类,但它返回一个空的对象。那么如何得到我想要的 ActionBarActivity
的活动对象类?
In a time, i thought use getParent()
from ActionBarActivity
class, but it return a null object.So how to get the Activity object i want in ActionBarActivity
class ?
推荐答案
尝试nameofactivity.this代替 getActivity()
Try nameofactivity.this instead getActivity()
我总是用 getActivity()
在片段
活动及其他任何形式的。这活动
。
I always use getActivity()
in Fragments
Activities and .this in any other kind of Activity
.
这篇关于从/在ActionBarActivity的Android相当于getActivity()的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!