ANDROID片段未附加到活动在特定设备中

时间:2016-06-02 16:43:14

标签: android android-fragments android-asynctask illegalstateexception

最近我的一位用户向我报告在尝试部署Dialog片段时发生了一个非常奇怪的错误。除了Gigabyte AV10(Android版本4.4.2)之外的任何设备都没有出现此错误。我尝试使用相同版本的其他设备并顺利运行。

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

这是Dialog片段

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;

}

STACK TRACE:

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)

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

我希望你能帮助我。

1 个答案:

答案 0 :(得分:0)

我认为您可以按如下方式更新=方法:

getVideoLink()