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 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果