Android Fragment问题(转换我的应用)

时间:2012-04-20 14:19:05

标签: android list dialog alertdialog fragment

我目前正在将我的应用转换为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无法解析。

- 初始化第二个对话框:

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上创建第二个对话框:builder cannot be resolved。这个显然是合法的,但我没有解决方案。

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

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

我正在使用ABS,但这是ABS中的一个标签。这意味着它需要是一个片段,但不显示ActionBar。

3 个答案:

答案 0 :(得分:0)

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

  

此令牌后预期的标识符

装置

你应该将你的片段移动到那些更容易维护的类文件

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

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

应该是:

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

(是的,一个分号丢失)

答案 1 :(得分:0)

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

您可以查看Fragment vs FragmentActivity

答案 2 :(得分:0)

感谢大家尝试提供帮助,但它已经解决了一段时间。

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