2013-07-12 7 views
0

Je suis nouveau à Fragments dans Android. Juste essayé d'en apprendre davantage sur DialogFragment. Mais il est dit classcastException.ClassCastException dans DialogFragment dans Android

public class FragmentDialog extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.fragment_dialog); 
    } 

    void showDialog() { 

     FragmentTransaction ft = getFragmentManager().beginTransaction(); 
     Fragment prev = getFragmentManager().findFragmentByTag("dialog"); 
     if (prev != null) { 
      ft.remove(prev); 
     } 
     ft.addToBackStack(null); 
     DialogFragment newFragment = MyDialogFragment.newInstance(0); 
     newFragment.show(getFragmentManager(), "dialog"); 
    } 

    public static class MyDialogFragment extends DialogFragment { 

     static MyDialogFragment newInstance(int num) { 
      MyDialogFragment f = new MyDialogFragment(); 

      return f; 
     } 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      int style = DialogFragment.STYLE_NORMAL, theme = 0; 
      setStyle(style, theme); 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 
      View v = inflater.inflate(R.layout.fragment_dialog, container, 
        false); 

      Button button = (Button) v.findViewById(R.id.show); 
      button.setOnClickListener(new OnClickListener() { 
       public void onClick(View v) { 
        ((FragmentDialog)getActivity()).showDialog(); // Error is in this line. 
       } 
      }); 
      return v; 
     } 
    } 
} 

erreur LogCat est:

07-12 15:22:25.241: E/AndroidRuntime(6419): java.lang.ClassCastException: com.example.fragmentexample.FragmentTabs cannot be cast to com.example.fragmentexample.FragmentDialog 
07-12 15:22:25.241: E/AndroidRuntime(6419):  at com.example.fragmentexample.FragmentDialog$MyDialogFragment$1.onClick(FragmentDialog.java:74) 

Modifier 1 #

Ce FragmentDialog est l'un onglet de FragmentTabs.

public class FragmentTabs extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    final ActionBar bar = getActionBar(); 
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 
    bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE); 

    ... 
    ... 
    bar.addTab(bar.newTab() 
      .setText("Dialog") 
      .setTabListener(new TabListener<FragmentDialog.MyDialogFragment>(
        this, "Dialog", FragmentDialog.MyDialogFragment.class))); 
    ... 
    ... 
} 

C'est pourquoi ((FragmentDialog)getActivity()).showDialog(); cette ligne renvoie com.example.fragmentexample.FragmentTabs cannot be cast to com.example.fragmentexample.FragmentDialog. Comment puis-je obtenir l'activité en tant que MyDialogFragment.

Toute aide pour résoudre ce problème sera très appréciée.

Merci

+0

point sur la ligne, où exception est levée. – azizbekian

+0

@andranikAzizbekyan Erreur dans '((FragmentDialog) getActivity()). ShowDialog();'. Voir la modification – Gunaseelan

+0

Avez-vous une autre activité FragmentTabs? – Nizam

Répondre

1

Vous avez un problème dans le constructeur suivant.

static MyDialogFragment newInstance(int num) 
    { 
     MyDialogFragment f = new MyDialogFragment(); 

     return f; 
    } 

Il devrait être comme suit,

static MyDialogFragment newInstance(int num) 
    { 
     MyDialogFragment f = new DialogFragment();  // Change is here. 

     return f; 
    } 
+0

Même problème ami – Gunaseelan

+1

Sinon, vous pouvez supprimer l'importation – azizbekian

+0

@Vigbyor: Êtes-vous sûr qu'il existe une classe biltin nommée FragmentDialog? ou déplacé 'DialogFragment' – Nizam

0

Vous souhaitez afficher le dialogue sur un clic de bouton dans Fragment.I avoir mon code actuellement utilisé.

CustomDialog:

public class CustomDialog extends DialogFragment { 


static CustomDialog newInstance() { 
    return new CustomDialog(); 
} 

private ProgressDialog mProgressDialog; 

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    mProgressDialog = new ProgressDialog(getActivity()); 
    mProgressDialog.setView(new View(getActivity())); 
    mProgressDialog.getWindow().setLayout(
      ViewGroup.LayoutParams.MATCH_PARENT, 
      ViewGroup.LayoutParams.WRAP_CONTENT); 
    mProgressDialog.setCancelable(true); 
    mProgressDialog.setTitle(getResources().getString(R.string.title)); 
    mProgressDialog.setMessage(getResources().getString(R.string.message)); 
    mProgressDialog 
      .setProgressStyle(android.R.style.Theme_DeviceDefault_Light); 
    mProgressDialog.getWindow().setBackgroundDrawable(
      new ColorDrawable(android.graphics.Color.TRANSPARENT)); 


    return mProgressDialog; 

} 

}

Fragment:

public class MainFragment extends Fragment implements OnClickListener { 
Button mButton; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    View v = inflater.inflate(R.layout.fragment_layout, container, false); 
    mButton = (Button) v.findViewById(R.id.button1); 
    mButton.setOnClickListener(MainFragment.this); 
    return v; 

} 

void showDialog() { 
    CustomDialog dialog= CustomDialog.newInstance(); 
    dialog.show(getFragmentManager(), "dialog"); 
} 

@Override 
public void onClick(View v) { 
    showDialog(); 
} 
} 
+0

Aucun ami, je n'ai pas besoin de dialogue de progression, je m'attends à un fragment de dialogue personnalisé, Dans ce que je vais faire quelques opérations. – Gunaseelan

Questions connexes