java.lang.String无法强制转换为Bean.Course_Bean

时间:2017-05-04 06:36:42

标签: java android spring

我想从Course_Bean.java获取school_id。我试过但错误是:

  

java.lang.String无法强制转换为Bean.Course_Bean

这是AllTopic_Fragment.java

 public AllTopic_Fragment(Context mContext, Object obj) {
    this.mContext = mContext;
    mNavigationDeleget = (NavigationDeleget) mContext;
    this.object = ((Course_Bean)obj).getId(); //in this line was error
    this.object2 = ((Course_Bean)obj).getSchool_id();
    Bitmap default_bitmap = Util.drawableToBitmap(mContext.getResources().getDrawable(R.mipmap.profile_default));

这是TopicMainFragment.java

public Topic_MainFragment(Context mContext, Object object) {
        this.mContext = mContext;
        mNavigationDeleget = (NavigationDeleget) mContext;
        this.obj = object;
    }     

private void displayView(String fragmentName, Object obj) {
        //deatachFragment(fragTransaction);
        // mFragmentManager = getSupportFragmentManager();
        fragmentTransaction = mFragmentManager.beginTransaction();
        if (fragmentName.equals(VerlinConstant.ALLTOPIC_FRAGMENT)) {
            mFragment = new AllTopic_Fragment(mContext, obj); //this line was error
        } else if (fragmentName.equals(VerlinConstant.RECOMMENDED_FRAGMENT)) {
            mFragment = new Recommended_Fragment(mContext, obj);

并在此脚本中,TopicMainFragment.java

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

    mFragmentManager = getChildFragmentManager();
    if (view == null) {
        view = inflater.inflate(R.layout.topic_main_fragment, container, false);
        intializeGUI(view);

        if (VerlinConstant.SELECT_FRAG.equalsIgnoreCase("")) {
            displayView(VerlinConstant.ALLTOPIC_FRAGMENT, obj); //this line was error
            changeViewColor(alltopic_tab_txt);
        } else if (VerlinConstant.SELECT_FRAG.equalsIgnoreCase("all")) {
            displayView(VerlinConstant.ALLTOPIC_FRAGMENT, obj);
            changeViewColor(alltopic_tab_txt);

请帮助

1 个答案:

答案 0 :(得分:0)

错误表示在行

this.object = ((Course_Bean)obj).getId(); //in this line was error

obj.getId()方法返回一个String对象,同时向其投射Course_Bean类型。您应该使用一个没有任何强制转换的简单String来检索id值:

String id = obj.getId();

但是,您应该在getId()上调用Object方法,而不是在Course_Bean个实例上(假设您已在此处声明此类方法),因为Object并非{&} #39; t有这样的方法。你需要重构整个代码,因为它在Object上大量撒谎 - 这不是OOP应该如何使用的方式。 另外,我建议将longint用于id字段,因为它是一个数字。