JuniorWizard 2015-01-23 02:35 采纳率: 50%
浏览 3223

Cannot cast from Fragment to ToDoListFragment的错误

Fragment的代码:
public class NewItemFragment extends Fragment
{
private OnNewItemAddedListener onNewItemAddedListener;

public interface OnNewItemAddedListener
{
    public void onNewItemAdded(String newItem);
}
public void onAttach(Activity activity)
{
    super.onAttach(activity);
    try
    {
        onNewItemAddedListener=(OnNewItemAddedListener)activity;
    }
    catch(ClassCastException e)
    {
        throw new ClassCastException(activity.toString()+"must implement OnNewItemAddedListener");
    }
}
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState)
{
    View view = inflater.inflate(R.layout.new_item_fragment, container,false);

    final EditText myEditText = (EditText)view.findViewById(R.id.myEditText);

    myEditText.setOnKeyListener(new View.OnKeyListener(){
        public boolean onKey(View v,int keyCode,KeyEvent event){
            if(event.getAction()==KeyEvent.ACTION_DOWN)
                if((keyCode == KeyEvent.KEYCODE_DPAD_CENTER)||
                    (keyCode == KeyEvent.KEYCODE_ENTER)){
                    String newItem = myEditText.getText().toString();
                    onNewItemAddedListener.onNewItemAdded(newItem);
                    myEditText.setText("");
                    return true;
                }
            return false;
        }
    });
    return view;
}

}

main.xml的代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <fragment android:name="com.example.dolist.NewItemFragment"
                android:id="@+id/NewItemFragment"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
        />

        <fragment android:name="com.example.dolist.ToDoListFragment"
                android:id="@+id/TodoListFragment"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
        />
    </LinearLayout>

ToDolistActivity的代码:
public class ToDoListActivity extends FragmentActivity {

private ArrayAdapter<String> aa;
private ArrayList<String> todoItems;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    FragmentManager fm = getFragmentManager();
    ToDoListFragment todoListFragment = (ToDoListFragment)fm.findFragmentById(R.id.TodoListFragment);

    todoItems = new ArrayList<String>();
    aa = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,todoItems);
    todoListFragment.setListAdapter(aa);
}

}
就在ToDListActivity中ToDoListFragment todoListFragment = (ToDoListFragment)fm.findFragmentById(R.id.TodoListFragment);
会报出Cannot cast from Fragment to ToDoListFragment的错误
新手,只是照书上打的,实在没看明白哪里错了?这个地方不可转换吗???

  • 写回答

1条回答

  • danielinbiti 2015-01-23 02:51
    关注

    main.xml贴出来的不全,关键在main.xml上

    评论

报告相同问题?

悬赏问题

  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题
  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题
  • ¥15 51单片机显示器问题
  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案