1

我目前正在将我的应用程序转换为 Fragments,并且遇到了许多问题。我希望能够在您的帮助下解决所有问题,并在解决转换的某个部分后编辑我的帖子。

第一个问题: AlertDialogs。当用户点击一个按钮时,也许稍后是一个 ListView 项目,他们会看到一个 AlertDialog 来选择他们想要查看的帐户。我为 Facebook 和 Twitter 做这个。然而,双重结构让我感到困惑,ListDialog 似乎加载其内容的方式不同。

所以,我的活动代码:

package bas.sie.Antonius;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.actionbarsherlock.app.SherlockFragment;

public class External extends SherlockFragment implements OnClickListener {

    Button mBtnOuderPortaal;
    Button mBtnTeletop;
    Button mBtnWebmail;
    Button mBtnInfobord;
    Button mBtnTwitter;
    Button mBtnFacebook;
    Button mBtnYouTube;
    String URLhome;
    String Title;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mBtnOuderPortaal = (Button) getActivity().findViewById(
                R.id.btnOuderPortaal);
        mBtnOuderPortaal.setOnClickListener(this);
        mBtnTeletop = (Button) getActivity().findViewById(R.id.btnTeletop);
        mBtnTeletop.setOnClickListener(this);
        mBtnWebmail = (Button) getActivity().findViewById(R.id.btnWebmail);
        mBtnWebmail.setOnClickListener(this);
        mBtnInfobord = (Button) getActivity().findViewById(R.id.btnInfobord);
        mBtnInfobord.setOnClickListener(this);
        mBtnTwitter = (Button) getActivity().findViewById(R.id.btnTwitter);
        mBtnTwitter.setOnClickListener(this);
        mBtnFacebook = (Button) getActivity().findViewById(R.id.btnFacebook);
        mBtnFacebook.setOnClickListener(this);
        mBtnYouTube = (Button) getActivity().findViewById(R.id.btnYouTube);
        mBtnYouTube.setOnClickListener(this);

    }

    void showDialog() {
        DialogFragment newFragment = MyAlertDialogFragment
                .newInstance(R.string.twitter_dialog_title);
        newFragment.show(getFragmentManager(), "dialog");
    }

    void showFBDialog() {
        DialogFragment newFragment = MyFBAlertDialogFragment
                .newInstance(R.string.facebook_dialog_title);
        newFragment.show(getFragmentManager(), "dialog");
    }

    public static class MyFBAlertDialogFragment extends DialogFragment {

        String URLhome;
        String Title;
        final CharSequence[] Facebook = { "Spot Antoniuscollege",
                "ACG Mediagroep" };

        public static MyAlertDialogFragment newInstance(int title) {
            MyAlertDialogFragment frag = new MyAlertDialogFragment();
            Bundle args = new Bundle();
            args.putInt("Kies Facebook-account", title);
            frag.setArguments(args);
            return frag;
        }

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            int title = getArguments().getInt("title");

            return new AlertDialog.Builder(getActivity())
            .setTitle(title)
            .setItems(Facebook, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int item) {
                    switch (item) {
                    case 0:
                        URLhome = "http://m.facebook.com/spotcarmelcollege";
                        Title = "Facebook ACG Spot";

                        Intent i = new Intent();
                        i.setClass(getActivity(), MyWebView.class);
                        i.putExtra("home", URLhome);
                        i.putExtra("title", Title);
                        startActivityForResult(i, 0);
                        break;
                    case 1:
                        URLhome = "http://m.facebook.com/pages/ACG-Media/128125633969183";
                        Title = "Facebook ACG Media";

                        Intent i1 = new Intent();
                        i1.setClass(getActivity(), MyWebView.class);
                        i1.putExtra("home", URLhome);
                        i1.putExtra("title", Title);
                        startActivityForResult(i1, 0);
                    default:
                        return;
                    }
                }
                getDialog.create();
            )
        }

    }

        public static class MyAlertDialogFragment extends DialogFragment {

            final CharSequence[] Twitter = { "@ACGouda", "@Spot_ACG",
                    "@ACGmediatheek" };
            String URLhome;

            public static MyAlertDialogFragment newInstance(int title) {
                MyAlertDialogFragment frag = new MyAlertDialogFragment();
                Bundle args = new Bundle();
                args.putInt("title", title);
                frag.setArguments(args);
                return frag;
            }

            @Override
            public Dialog onCreateDialog(Bundle savedInstanceState) {
                int title = getArguments().getInt("title");

                return new AlertDialog.Builder(getActivity())
                .setTitle(title)
                s.setItems(Twitter,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int item) {
                                switch (item) {
                                case 0:
                                    URLhome = "https://mobile.twitter.com/#!/ACGouda";

                                    Intent intent = new Intent(
                                            Intent.ACTION_VIEW);
                                    intent.setData(Uri.parse(URLhome));
                                    startActivity(intent);
                                    break;
                                case 1:
                                    URLhome = "https://mobile.twitter.com/#!/Spot_ACG";

                                    Intent intent1 = new Intent(
                                            Intent.ACTION_VIEW);
                                    intent1.setData(Uri.parse(URLhome));
                                    startActivity(intent1);
                                    break;
                                case 2:
                                    URLhome = "https://mobile.twitter.com/#!/ACGmediatheek";

                                    Intent intent2 = new Intent(
                                            Intent.ACTION_VIEW);
                                    intent2.setData(Uri.parse(URLhome));
                                    startActivity(intent2);
                                    break;
                                default:
                                    return;
                                }
                            }
                        });
                AlertDialog alert = builder.create();
                alert.show();
            }

        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.btnFacebook:
                showFBDialog();
                break;
            case R.id.btnTwitter:
                showDialog();
                break;
            case R.id.btnInfobord:
                URLhome = "http://carmelcollegegouda.nl/site_ant/index.php?option=com_content&view=article&id=182&Itemid=131&lang=nl";
                Title = "Infobord";

                Intent bord = new Intent();
                bord.setClass(getActivity(), MyWebView.class);
                bord.putExtra("home", URLhome);
                bord.putExtra("title", Title);
                startActivityForResult(bord, 0);
                break;
            case R.id.btnOuderPortaal:
                URLhome = "https://portaal.mijnsom.nl/login/ccg";
                Title = "Onderwijsportaal";

                Intent portaal = new Intent();
                portaal.setClass(getActivity(), MyWebView.class);
                portaal.putExtra("home", URLhome);
                portaal.putExtra("title", Title);
                startActivityForResult(portaal, 0);
                break;
            case R.id.btnTeletop:
                URLhome = "http://antoniuscollege.teletop.nl/";

                Intent teletop = new Intent(Intent.ACTION_VIEW);
                teletop.setData(Uri.parse(URLhome));
                startActivity(teletop);
                break;
            case R.id.btnMail:
                URLhome = "https://webmail.carmelcollegegouda.nl/";
                Title = "Webmail";

                Intent mail = new Intent();
                mail.setClass(getActivity(), MyWebView.class);
                mail.putExtra("home", URLhome);
                mail.putExtra("title", Title);
                startActivityForResult(mail, 0);
                break;
            case R.id.btnYouTube:
                URLhome = "http://m.youtube.com/results?search_query=Antoniuscollege&oq=Antoniuscollege&aq=f&aqi=&aql=&gs_l=youtube-psuggest.3...4430l12422l0l12750l31l31l9l1l2l0l122l1657l20j1l21l0.";

                Intent YT = new Intent();
                YT.setClass(getActivity(), MyWebView.class);
                YT.putExtra("home", URLhome);
                startActivityForResult(YT, 0);
                break;
            }
        }
    }

}

错误在:

-.create();在第一个对话框中,它说

Syntax error on token "create", Identifier expected after this token

Syntax error, insert ";" to complete MethodDeclaration

Syntax error, insert ";" to complete ReturnStatement

Syntax error, insert ")" to complete MethodInvocation

Syntax error, insert "}" to complete ClassBody

-MyAlertDialogFragment首先showDialog();,说MyAlertDialogFragment不能解决。

-在第二个Dialog的初始化中:

Syntax error on token ".", delete this token

The method setItems(CharSequence[], new DialogInterface.OnClickListener(){}) is undefined for the type External.MyFBAlertDialogFragment.MyAlertDialogFragment

Type mismatch: cannot convert from AlertDialog.Builder to Dialog

然后也builder从创建第二个 Dialog: 开始builder cannot be resolved。这显然是合法的,但我没有解决方案。

它还抱怨资源目录名称无效。

我刚刚解决的最后一个问题:)。

我正在使用 ABS,但这是 ABS 中的选项卡之一。这意味着它需要是一个 Fragment,但不显示 ActionBar。

4

3 回答 3

0

您有多个语法错误。那是什么

此令牌之后需要的标识符

方法

您应该将您的片段移动到自己的类文件中,这样更容易维护。

我可以发现一个我认为的错误,但在浏览器中很难:

                default:
                    return;
                }
            }
            getDialog.create();
        )
    }

应该:

                default:
                    return;
                }
            }
            getDialog.create();
        }
      );        
}

(是的,少了一个分号)

于 2012-04-20T14:33:56.563 回答
0

您发布活动的代码,但您的约定继承自SherlockFragment而不是SherlockFragmentActivity

您可能应该看看Fragment vs FragmentActivity

于 2012-10-23T03:19:56.753 回答
0

感谢大家的帮助,但它已经解决了一段时间。

但是,这里发布的代码很多,所以如果有人想知道:给我发消息,我会尽力提供帮助!

于 2012-11-22T23:52:28.327 回答