Android 调用片段类时出现问题';方法
我有一门“家庭活动”课,内容如下:Android 调用片段类时出现问题';方法,android,android-fragments,Android,Android Fragments,我有一门“家庭活动”课,内容如下: public class HomeActivity extends FragmentActivity implements OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
public class HomeActivity extends FragmentActivity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentManager fm = getSupportFragmentManager();
// Create the list fragment and add it as our sole content.
if (fm.findFragmentById(android.R.id.content) == null) {
HomeFragment list = new HomeFragment();
fm.beginTransaction().add(android.R.id.content, list).commit();
}
}
public static class HomeFragment extends Fragment {
webServiceTask = WebServiceTask.getInstance(
getActivity(), Constants.METHOD_NAME_PRODUCTS,
Constants.PRODUCT_NAME, null);
public void Work() {}
}
}
final public class WebServiceTask extends AsyncTask<String, String, String> {
private WebServiceTask(final Activity activity, final String methodName,
final String productName, final String addInfo[]) {
super();
this.activity = activity;
this.methodName = methodName;
this.productName = productName;
this.addInfo = addInfo;
}
public static WebServiceTask getInstance(final Activity activity,
final String methodName, final String productName,
final String additionalInfo[]) {
webServiceTask = new WebServiceTask(activity, methodName, productName,
additionalInfo);
return webServiceTask;
}
protected void onPostExecute() {
// Here I am trying to call the work() method in HomeFragment, How can I do that?
}
我有另一个类WebServiceTask,如下所示:
public class HomeActivity extends FragmentActivity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentManager fm = getSupportFragmentManager();
// Create the list fragment and add it as our sole content.
if (fm.findFragmentById(android.R.id.content) == null) {
HomeFragment list = new HomeFragment();
fm.beginTransaction().add(android.R.id.content, list).commit();
}
}
public static class HomeFragment extends Fragment {
webServiceTask = WebServiceTask.getInstance(
getActivity(), Constants.METHOD_NAME_PRODUCTS,
Constants.PRODUCT_NAME, null);
public void Work() {}
}
}
final public class WebServiceTask extends AsyncTask<String, String, String> {
private WebServiceTask(final Activity activity, final String methodName,
final String productName, final String addInfo[]) {
super();
this.activity = activity;
this.methodName = methodName;
this.productName = productName;
this.addInfo = addInfo;
}
public static WebServiceTask getInstance(final Activity activity,
final String methodName, final String productName,
final String additionalInfo[]) {
webServiceTask = new WebServiceTask(activity, methodName, productName,
additionalInfo);
return webServiceTask;
}
protected void onPostExecute() {
// Here I am trying to call the work() method in HomeFragment, How can I do that?
}
final公共类WebServiceTask扩展异步任务{
私有WebServiceTask(最终活动、最终字符串方法名、,
最终字符串产品名称,最终字符串附加信息[]){
超级();
这个。活动=活动;
this.methodName=methodName;
this.productName=productName;
this.addInfo=addInfo;
}
公共静态WebServiceTask getInstance(最终活动,
最终字符串methodName、最终字符串productName、,
最终字符串附加信息[]){
webServiceTask=新的webServiceTask(活动、方法名、产品名称、,
其他信息);
返回webServiceTask;
}
受保护的void onPostExecute(){
//在这里,我试图调用HomeFragment中的work()方法,我该怎么做?
}
我的问题是如何从onPostExecute()调用HomeFragment类中的work()方法。这是一个方法。我不知道它是否是最好的: 将工作函数设置为publicstaticvoid。从Asynctask onpostexecute as调用它
HomeActivity.Work();
编辑:
还有一种方法(同样不确定这是否是最好的方法):
如果你不能做这个工作,考虑把你的异步任务类放在家庭活动类
这是一个方法。我不知道它是否是最好的一个: 将工作函数设置为publicstaticvoid。从Asynctask onpostexecute as调用它HomeActivity.Work();
编辑:
还有一种方法(同样不确定这是否是最好的方法):
如果不能实现这一点,请考虑使用“<代码> FragmentManger < /COD> <代码>查找FraceStfById())<代码> >或<代码>您可以获取当前片段的实例并调用片段方法。
很好地使用fragmentmanager
findFragmentById()或findFragmentByTag()
您可以获取当前片段的一个实例并调用您的片段方法。我建议为您的任务创建一个侦听器,并在执行后调用其方法。这将为您提供更大的灵活性,并在任务完成后控制要删除的内容。以下是我将使用的示例代码:
public class MyTask extend AsyncTask<Void, Void, Boolean> {
public interface MyTaskListener {
void onSuccess();
void onFailure();
void onError(Throwable t);
}
private Throwable error;
private MyTaskListener listener;
public MyTask(MyTaskListener listener) {
this.listener = listener;
}
@Overrride
public Boolean doInBackground(Void... params) {
try {
if (workCompleted()) {
//work completed without error - return true
return Boolean.TRUE;
} else {
//work failed to complete - return false
return Boolean.FALSE;
}
} catch(Exception e) {
//unexpected error happened - remember error and return null
this.error = e;
return null;
}
}
@Override
public void onPostExecute(Boolean result){
if (!isCancelled()) { //you only want to process if task wasn't cancelled
if (this.error != null && result == null) { //we have error, process it
if (listener != null) {
listener.onError(this.error);
}
}
if (Boolean.FALSE.equals(result)) { //we have faile, process it
if (listener != null) {
listener.onFail();
}
}
if (Boolean.TRUE.equals(result)) { //we have success
if (listener != null) {
listener.onSuccess();
}
}
}
}
}
我建议为您的任务创建一个侦听器,并在执行后调用其方法。这将为您提供更大的灵活性,并在任务完成后控制您想要删除的内容。以下是我将使用的示例代码:
public class MyTask extend AsyncTask<Void, Void, Boolean> {
public interface MyTaskListener {
void onSuccess();
void onFailure();
void onError(Throwable t);
}
private Throwable error;
private MyTaskListener listener;
public MyTask(MyTaskListener listener) {
this.listener = listener;
}
@Overrride
public Boolean doInBackground(Void... params) {
try {
if (workCompleted()) {
//work completed without error - return true
return Boolean.TRUE;
} else {
//work failed to complete - return false
return Boolean.FALSE;
}
} catch(Exception e) {
//unexpected error happened - remember error and return null
this.error = e;
return null;
}
}
@Override
public void onPostExecute(Boolean result){
if (!isCancelled()) { //you only want to process if task wasn't cancelled
if (this.error != null && result == null) { //we have error, process it
if (listener != null) {
listener.onError(this.error);
}
}
if (Boolean.FALSE.equals(result)) { //we have faile, process it
if (listener != null) {
listener.onFail();
}
}
if (Boolean.TRUE.equals(result)) { //we have success
if (listener != null) {
listener.onSuccess();
}
}
}
}
}
创建一个接口文件
public interface AsynAction
{
public void Work();
}
在HomeActivity中实现AsynAction
public class HomeActivity extends FragmentActivity implements OnClickListener,AsyncAction {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentManager fm = getSupportFragmentManager();
// Create the list fragment and add it as our sole content.
if (fm.findFragmentById(android.R.id.content) == null) {
HomeFragment list = new HomeFragment();
fm.beginTransaction().add(android.R.id.content, list).commit();
}
}
public static class HomeFragment extends Fragment {
webServiceTask = WebServiceTask.getInstance(
getActivity(), Constants.METHOD_NAME_PRODUCTS,
Constants.PRODUCT_NAME, null);
@Override
public void Work()
{
}
}
}
然后在asynctask中进行更改,以接收asyncAction对象作为引用
final public class WebServiceTask extends AsyncTask<String, String, String> {
private WebServiceTask(final AyscAction asycAction,final Activity activity, final String methodName,
final String productName, final String addInfo[]) {
super();
this.activity = activity;
this.asycAction=asycAction;
this.methodName = methodName;
this.productName = productName;
this.addInfo = addInfo;
}
public static WebServiceTask getInstance(final AyscAction asycAction,final Activity activity,
final String methodName, final String productName,
final String additionalInfo[]) {
webServiceTask = new WebServiceTask(asycAction,activity, methodName, productName,
additionalInfo);
return webServiceTask;
}
protected void onPostExecute() {
// You can call work from here
if(asynAction!=null)
asyncAction.Work();
}
final公共类WebServiceTask扩展异步任务{
私有WebServiceTask(最终AysAction asycation、最终Activity Activity、最终String methodName、,
最终字符串产品名称,最终字符串附加信息[]){
超级();
这个。活动=活动;
这个。asycAction=asycAction;
this.methodName=methodName;
this.productName=productName;
this.addInfo=addInfo;
}
公共静态WebServiceTask getInstance(最终AysAction asycAction,最终活动,
最终字符串methodName、最终字符串productName、,
最终字符串附加信息[]){
webServiceTask=新的webServiceTask(asycAction、activity、methodName、productName、,
其他信息);
返回webServiceTask;
}
受保护的void onPostExecute(){
//你可以在这里叫工作
if(asynAction!=null)
asyncAction.Work();
}
创建一个接口文件
public interface AsynAction
{
public void Work();
}
在HomeActivity中实现AsynAction
public class HomeActivity extends FragmentActivity implements OnClickListener,AsyncAction {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentManager fm = getSupportFragmentManager();
// Create the list fragment and add it as our sole content.
if (fm.findFragmentById(android.R.id.content) == null) {
HomeFragment list = new HomeFragment();
fm.beginTransaction().add(android.R.id.content, list).commit();
}
}
public static class HomeFragment extends Fragment {
webServiceTask = WebServiceTask.getInstance(
getActivity(), Constants.METHOD_NAME_PRODUCTS,
Constants.PRODUCT_NAME, null);
@Override
public void Work()
{
}
}
}
然后在asynctask中进行更改,以接收asyncAction对象作为引用
final public class WebServiceTask extends AsyncTask<String, String, String> {
private WebServiceTask(final AyscAction asycAction,final Activity activity, final String methodName,
final String productName, final String addInfo[]) {
super();
this.activity = activity;
this.asycAction=asycAction;
this.methodName = methodName;
this.productName = productName;
this.addInfo = addInfo;
}
public static WebServiceTask getInstance(final AyscAction asycAction,final Activity activity,
final String methodName, final String productName,
final String additionalInfo[]) {
webServiceTask = new WebServiceTask(asycAction,activity, methodName, productName,
additionalInfo);
return webServiceTask;
}
protected void onPostExecute() {
// You can call work from here
if(asynAction!=null)
asyncAction.Work();
}
final公共类WebServiceTask扩展异步任务{
私有WebServiceTask(最终AysAction asycation、最终Activity Activity、最终String methodName、,
最终字符串产品名称,最终字符串附加信息[]){
超级();
这个。活动=活动;
这个。asycAction=asycAction;
this.methodName=methodName;
this.productName=productName;
this.addInfo=addInfo;
}
公共静态WebServiceTask getInstance(最终AysAction asycAction,最终活动,
最终字符串methodName、最终字符串productName、,
最终字符串附加信息[]){
webServiceTask=新的webServiceTask(asycAction、activity、methodName、productName、,
其他信息);
返回webServiceTask;
}
受保护的void onPostExecute(){
//你可以在这里叫工作
if(asynAction!=null)
asyncAction.Work();
}
work方法在HomeFragment类中。我还有activity variable.ohh我没有看到。work方法在HomeFragment类中。还有activity variable.ohh我没有看到。无法使其成为静态。我正在方法中使用click listeners无法使其成为静态。我正在方法中使用click listeners如何获取我的案例中的Id?请尝试要将AsyncTask作为内部类引入HomeActivity,请将FragmentManager设置为类变量,现在您可以在任何位置访问fragment manager。在我的情况下,如何获取Id?尝试将AsyncTask作为内部类引入HomeActivity,将FragmentManager设置为类变量,现在您可以在任何位置访问fragment manager你想要的。