2013-07-12 94 views
0

我是Android中的新手Fragments。試圖瞭解DialogFragment。但它說classcastExceptionAndroid中的DialogFragment中的ClassCastException

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; 
     } 
    } 
} 

logcat的錯誤是:

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) 

編輯1#

FragmentDialogFragmentTabs的一個選項卡。

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))); 
    ... 
    ... 
} 

這就是爲什麼((FragmentDialog)getActivity()).showDialog();這一行返回com.example.fragmentexample.FragmentTabs cannot be cast to com.example.fragmentexample.FragmentDialog。我怎樣才能得到MyDialogFragment的活動。

任何幫助解決這個問題將不勝感激。

謝謝

+0

指出行,其中拋出異常。 ((FragmentDialog)getActivity())。showDialog();'中出現錯誤@andranikAzizbekyan( – azizbekian

+0

)。查看編輯 – Gunaseelan

+0

你有另一個活動FragmentTabs嗎? – Nizam

回答

1

您在下面的構造函數中有問題。

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

     return f; 
    } 

它應該是如下,

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

     return f; 
    } 
+0

同樣問題的朋友 – Gunaseelan

+1

否則你可以刪除導入 – azizbekian

+0

@Vigbyor:你確定有一個名爲FragmentDialog的biltin類嗎?或錯位'DialogFragment' – Nizam

0

要顯示在Fragment.I點擊按鈕對話框有目前正在使用我的代碼。

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; 

} 

}

片段:

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

沒有朋友,我不需要進度對話框,我期待自定義對話框片段,在那我會做一些操作。 – Gunaseelan

相關問題