简体   繁体   English

从异步任务返回时,片段未附加到活动

[英]Fragment not attached to Activity when returning from Async Task

I have added three fragment inside A_Activity. 我在A_Activity中添加了三个片段。 These are fragment_1, fragment_2, fragment_3 这些是fragment_1,fragment_2,fragment_3

from fragment_1 I am calling an async class async_class 来自fragment_1我正在调用异步类async_class

Here is my Fragment_1: 这是我的Fragment_1:

public class Fragment_1 extends Fragment {

ListView listView;
private TrainingGetData mTrainingGetData = null;
Context mContext;

public Fragment_1(){
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_1, container, false);

    mTrainingGetData = new TrainingGetData(getContext());
    mTrainingGetData.execute((String) null);
    return view;
}


public void setTitle(Context context, ArrayList<String> ids, ArrayList<String> titles, final ArrayList<String> urls){

    final Activity activity=(Activity) context;
    final ArrayAdapter adapter=new ArrayAdapter(context, android.R.layout.simple_list_item_1, titles);
    listView=(ListView) ((Activity) context).findViewById(R.id.listView_1);
    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent myIntent = new Intent(activity, AnotherActivity.class);
            myIntent.putExtra("url", urls.get(position));
    startActivity(myIntent);
        });
}

}

In the async class I do some task in the background and in post execude I have added these code 在异步类中,我在后台执行一些任务,在执行后,我添加了以下代码

public class TrainingGetData extends AsyncTask<String, String, String> {
Fragment_1 fragment_1=new Fragment_1();

public TrainingGetData(Context context){
    mContext=context;
}

@Override
protected String doInBackground(String... params) {

    try {
        //do the processing
    }
    catch (JSONException e) {
        e.printStackTrace();
        return "JSON Data Parse Error";
    }
    catch (Exception e){
        e.printStackTrace();
        return "Unknown Error: Insert/Update Failed";
    }
    return null;
}

@Override
protected void onPostExecute(String message) {

    if(success == 0 && message.equals("No files found")){
        Toast.makeText(mContext, "Sorry No Data Found", Toast.LENGTH_SHORT).show();
    }

    if(success==1) {
        //do the processing
        fragment_1.setTitle(mContext, ids, titles, urls);
    }
}
}

But when I return to the fragment_1 setTitle method and click on any list item it shows that the fragment_1 is not attached to the Activity. 但是,当我返回fragment_1 setTitle方法并单击任何列表项时,它表明fragment_1未附加到活动中。

What went wrong? 什么地方出了错? How can I solve this? 我该如何解决?

Thanks 谢谢

But when I return to the fragment_1 setTitle method and click on any list item it shows that the fragment_1 is not attached to the Activity 但是,当我返回fragment_1 setTitle方法并单击任何列表项时,它表明fragment_1未附加到活动中

Because currently creating new object of Fragment_1 for calling setTitle method. 因为当前正在创建Fragment_1新对象来调用setTitle方法。

Pass current Fragment Context in TrainingGetData class in same way as passing Context then call setTitle method. 以与传递Context相同的方式在TrainingGetData类中传递当前的Fragment Context,然后调用setTitle方法。 like: 喜欢:

Fragment_1 fragment_1; Fragment_1fragment_1;

public TrainingGetData(Context context,Fragment_1 fragment_1){
    mContext=context;
    this.fragment_1=fragment_1;
}

and create TrainingGetData object: 并创建TrainingGetData对象:

mTrainingGetData = new TrainingGetData(getContext(),Fragment_1.this);

You're getting the error because the fragment_1 object of your Fragment Fragment_1 has been instantiated inside your AsyncTask TrainingGetData. 您收到错误消息是因为Fragment Fragment_1的fragment_1对象已在AsyncTask TrainingGetData内部实例化。 This is not the Fragment object which is connected to your Activity . 这不是连接到ActivityFragment对象。

In order to solve the issue, you need to initialise fragment_1 object as shown below: 为了解决此问题,您需要初始化fragment_1对象,如下所示:

    fragment_1 = (Fragment) mContext;

Write the above line of code in the Constructor of AsyncTask TrainingGetData. AsyncTask TrainingGetData的Constructor中编写以上代码。

As you are creating new instance of fragment. 在创建片段的新实例时。

 Fragment_1 fragment_1=new Fragment_1();

these is not going to work 这些是行不通的

use fragment instance in TrainingGetData constructor and used it 在TrainingGetData构造函数中使用片段实例并使用它

  public TrainingGetData(Context context, Fragment_1  fragment_1) {
        mContext = context;
        this.fragment_1 = fragment_1;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM