【问题标题】:ANDROID Fragment not attached to Activity In a specific deviceANDROID Fragment 未附加到特定设备中的 Activity
【发布时间】:2016-06-02 16:43:13
【问题描述】:

最近我的一位用户向我报告了在尝试部署 Dialog 片段时发生了一个非常奇怪的错误。除了技嘉AV10(Android版本4.4.2)以外的其他设备都没有出现过这个错误,我用另一台同版本的设备试了一下,运行流畅。

当用户离开对话框片段时发生错误。假设 AsyncTask 已经完成,当用户使用服务器返回的资源时,会发生错误。下面的代码会更好的解释

这是对话框片段

public class shareDialog extends DialogFragment implements ManagePool.InterfaceManagePool {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.share_dialog, null);

    ctx = MainActivity.ctx;

    getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);

    shareDialog = new ShareDialog(this);

    getVideoLink();

    return view;
}

private void getVideoLink() { //Get the video link from the server

    ManagePool newTask = new ManagePool(ctx);
    newTask.listener =this;
    newTask.execute("2",user_id,video_id);
}

public void onResume()
{super.onResume();

    youtube.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        //After this, error occurs

        startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("http://"+play_link)));

        }
    });
}

@Override
public void getVideoLink(String result) {

    if(result!=null)
    {
        play_link = result;
        link.setText(play_link);// The Stack trace says the error occurs here, But the UI displays it correctly
    }

}

这是使用 REST 从服务器获取链接的 AsyncTask:

public class ManagePool extends AsyncTask<String, Void, String> {

public interface InterfaceManagePool {

    void getVideoLink(String result);
}

public ManagePool(Context ctx) {
    context = ctx;
    dialog = new ProgressDialog(context);
}

protected void onPreExecute() {

    dialog.setTitle(context.getString(R.string.loading));
    dialog.show(); 
}

protected void onPostExecute(String result) {

    dialog.dismiss();
    listener.getVideoLink(result);
}

protected String doInBackground(String... params) {

    String Response = null;

     //REST STUFF
    Response = newConection.performCall(requestURL, postDataParams, "POST");
    //REST STUFF

    return Response;

}

堆栈跟踪:

java.lang.IllegalStateException: Fragment shareDialog{41eb6558} not attached to Activity
at android.support.v4.app.Fragment.getResources(Fragment.java:620)
at android.support.v4.app.Fragment.getString(Fragment.java:642)
at dialog.shareDialog.getVideoLink(shareDialog.java:262)
at helpers.ManagePool.onPostExecute(ManagePool.java:74)
at helpers.ManagePool.onPostExecute(ManagePool.java:19)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5323)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
at dalvik.system.NativeStart.main(Native Method)

我不明白会发生什么,因为视频链接显示正确。我已经要求用户在链接出现之前什么都不做,但错误仍然存​​在。

希望你能帮助我。

【问题讨论】:

    标签: android android-fragments android-asynctask illegalstateexception


    【解决方案1】:

    我认为您可以按如下方式更新您的getVideoLink() 方法:

    @Override
    public void getVideoLink(String result) {
        if( result!=null && !isDetached() ) {
            play_link = result;
            link.setText(play_link);// The Stack trace says the error occurs here, But the UI displays it correctly
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-20
      • 1970-01-01
      • 1970-01-01
      • 2020-02-06
      • 2012-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多