在活动

时间:2016-03-17 20:50:40

标签: android android-fragments asynchronous

我有一个Android应用程序,它有一个DialogFragment调用异步进程(在下面的代码示例中称为“api.Async”)。完成后,我想显示结果的另一个活动。我一直收到“片段[名称]没有附加到活动。”我该如何解决这个问题?一旦完成,我怎样才能将信息发送到新活动?

public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    LayoutInflater inflater = getActivity().getLayoutInflater();

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

    builder.setView(view)
            .setPositiveButton(R.string.lookup, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    EditText text = (EditText) view.findViewById(R.id.username);

                    api.Async("GetByID/", text.getText().toString(), new badgerAPI.IApiResultRunnable() {
                        @Override
                        public void run(CMAPIResult apiResult) {
                            if (!apiResult.lmSuccess) {
                                showError(view.getContext(), "User not found", apiResult.lmErrorMessage).show();
                            } else {
                                String badgeuri = apiResult.lmResultDict.optString("badgeuri");

//APPLICATION ERRORS IN FOLLOWING FUNCTION
                                showBadge(view.getContext(), badgeuri);
                            }
                        }
                    });
                }
            })
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                }
            });
    ;

    return builder.create();
}

“showBadge”功能非常简单:

private void showBadge(final Context act, String uri){
    Intent intent = new Intent(act, DisplayBadge.class);
    startActivity(intent);
}

我尝试了几种不同的方式:

getActivity()

view.GetContext();

[Class] .this(甚至不会编译)

我可以通过什么方式绕过片段(在活动完成时关闭)并在结果中显示函数的视图?

谢谢!

1 个答案:

答案 0 :(得分:1)

看起来您的DialogFragent在异步过程完成时已被破坏。要使其正常工作,请从Activity而不是DialogFragment启动异步过程。

因此,在DialogFragment中只需回调一下Activity中的一个公共方法,它将执行繁重的工作(本例中为doPositiveClick()):

public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    LayoutInflater inflater = getActivity().getLayoutInflater();

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

    builder.setView(view)
            .setPositiveButton(R.string.lookup, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    EditText text = (EditText) view.findViewById(R.id.username);

                    //call into the Activity and pass in the name                        
                    ((MainActivity)getActivity()).doPositiveClick(text.getText().toString());

                }
            })
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                }
            });

    return builder.create();
}

然后,在调用DialogFragment的Activity中,启动异步过程,并在完成后启动DisplayBadge活动。

注意我已经更改了方法定义和一些代码,因为不再需要传递上下文了:

public void doPositiveClick(String name) {
   api.Async("GetByID/", name, new badgerAPI.IApiResultRunnable() {
     @Override
     public void run(CMAPIResult apiResult) {
       if (!apiResult.lmSuccess) {
         showError(this, "User not found", apiResult.lmErrorMessage).show();
       } else {
         String badgeuri = apiResult.lmResultDict.optString("badgeuri");

         showBadge(badgeuri);
       }
     }
   });

}

private void showBadge(String uri){
    Intent intent = new Intent(this, DisplayBadge.class);
    startActivity(intent);
}