黑马程序员技术交流社区
标题:
java.lang.ClassCastException
[打印本页]
作者:
战狼
时间:
2015-4-24 21:54
标题:
java.lang.ClassCastException
import android.os.Bundle;
import android.provider.Browser.BookmarkColumns;
import android.app.Activity;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MainActivity extends Fragment {
private static final String ITEM_ID = "item_id";
private BookContent.Book book;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments().containsKey(ITEM_ID)) {
book = BookContent.map.get(getArguments().getInt(ITEM_ID));
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_main, container,false);
if (book !=null) {
((TextView)rootView.findViewById(R.id.tv_book_title)).setText(book.title);
((TextView)rootView.findViewById(R.id.tv_book_desc)).setText(book.desc);
}
return rootView;
}
}
复制代码
package com.example.action;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.location.Address;
public class BookContent {
public static class Book{
public Integer id;
public String title;
public String desc;
public Book(Integer id, String title, String desc) {
super();
this.id = id;
this.title = title;
this.desc = desc;
}
@Override
public String toString() {
return "Book [title=" + title + "]";
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
public static List<Book> ITEMS = new ArrayList<BookContent.Book>();
public static Map<Integer, Book> map = new HashMap<Integer, BookContent.Book>();
static{
addItems(new Book(1, "疯狂Java讲义", "一本全面、深入了解java学习图书"));
addItems(new Book(2, "疯狂Android讲义", "一本全面、深入了解Android学习图书"));
addItems(new Book(3, "JavaEE企业级", "一本全面介绍Struts2、hibernate、spring的学习图书"));
}
private static void addItems(Book book){
ITEMS.add(book);
map.put(book.id, book);
}
}
复制代码
会报java.lang.ClassCastException异常,求各位解决。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2