2012-04-20 73 views
1

我目前正在將我的應用程序轉換爲碎片,並遇到了一些問題。我希望能夠在你的幫助下解決所有問題,並且一旦我解決了轉換的某個部分,我將編輯我的帖子。Android的碎片問題(轉換我的應用程序)

第一個問題: 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不能得到解決。

-In第二對話的初始化:

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。

+0

你剛剛得到語法錯誤。 – Blundell 2012-04-20 14:31:31

回答

0

感謝大家試圖幫助,但它已經解決了一段時間。

但是,在這裏發佈的代碼很多,所以如果有人想知道:給我發一條消息,我會盡力幫助!

0

您有多個語法錯誤。那這個令牌

後預期的

識別符是指

你應該移動你的碎片進入有自己的類文件,這將是更容易維護。

我能發現一個錯誤我認爲但它在瀏覽器中很難:

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

應該是:

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

(是一個分號失蹤)

+0

你是什麼意思「將碎片移動到他們自己的類文件中?」另外,它沒有改變任何東西,像你說的那樣編輯那個部分... – DatBassie 2012-04-20 15:25:24

+0

是的,它可能不會改變任何東西,因爲你會在其他地方出現更多的語法錯誤! – Blundell 2012-04-20 19:44:47

+0

你所擁有的每個類文件,即'public class SomeClass {'''都可以移入它自己的'SomeClass.java'文件中,那麼如果你的某個類有編譯錯誤,它不會影響其他類。 – Blundell 2012-04-20 19:45:46

0

您發佈代碼爲活動但您的約定從繼承Sherlock片段不是SherlockFragmentActivity。你可以看看Fragment vs FragmentActivity

+0

我已經解決了,有人幫我採用正確的方式來做到這一點。然而,由於這不屬於SO,我還沒有標出正確的答案。我會看看你提到的網頁,謝謝! – DatBassie 2012-10-23 07:19:23