不兼容的类型:DetailOneFragment无法转换为Fragment

时间:2016-07-04 14:34:47

标签: java android android-fragments

我的代码中存在问题

错误是

  

不兼容的类型:无法将DetailOneFragment转换为Fragment   在Android

     

不兼容的类型:DetailTwoFragment无法转换为Fragment   在Android

哪个错了?

这是DetailActivity.java

import android.content.Intnet;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import org.adnroidtown.albaplanet.R;
import org.adnroidtown.albaplanet.review_view.Review1Activity;

public class DetailActivity extends AppCompatActivity {

    ViewPager pager;
    TabLayout tabLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);

        //메인 액티비티에서 넘겨받은 인텐트 -시작-
        ImageView iv = (ImageView) findViewById(R.id.detail_img);
        TextView tvName = (TextView) findViewById(R.id.detail_store);

        Intent intent = getIntent();
        iv.setImageResource(intent.getIntExtra("img", 0));
        tvName.setText(intent.getStringExtra("name"));

        pager = (ViewPager) findViewById(R.id.detail_viewpager);
        tabLayout = (TabLayout) findViewById(R.id.tab_layout);

        FragmentManager manager = getSupportFragmentManager();
        PagerAdapter adapter = new PagerAdapter(manager);
        pager.setAdapter(adapter);

        tabLayout.setupWithViewPager(pager);
        // mTabLayout.setupWithViewPager(mPager1);
        pager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

        tabLayout.setTabsFromPagerAdapter(adapter);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {@Override
            public void onClick(View view) {
                Intent intent = new Intent(DetailActivity.this, Review1Activity.class);
                startActivity(intent);
            }
        });
    }
}

这是PagerAdapter.java

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

public class PagerAdapter extends FragmentStatePagerAdapter {
    public PagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {

        Fragment frag = null;
        switch (position) {
        case 0:
            frag = new DetailOneFragment();
            break;
        case 1:
            frag = new DetailTwoFragment();
            break;
        }
        return frag;
    }

    @Override
    public int getCount() {
        return 2;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        String title = " ";
        switch (position) {
        case 0:
            title = "Game";
            break;
        case 1:
            title = "Movie";
            break;
        }
        return title;
    }
}

这是DetailOneFragment.java

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import org.adnroidtown.albaplanet.R;

public class DetailOneFragment extends Fragment {

    public DetailOneFragment(){
    // Required empty public constructor
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_detail_one, container, false);
    }
}

这是错误Scre​​enShot

enter image description here

3 个答案:

答案 0 :(得分:1)

查看截图。您在DetailOneFragment中导入/扩展android.app.Fragment;并在其他类中使用android.app.support...Fragment

答案 1 :(得分:0)

替换

QObject::sender()

dynamic_cast
你片段中的

答案 2 :(得分:0)

截至 2021 年 2 月,上述解决方案对我有用,但我不得不更换:

import pyspark.sql.functions as F

df_joined = df1.join(df2, df1.ID == df2.ID2, 'left').select(
    'ID',
    F.coalesce(df1.Value, df2.Value2).alias('Value'),
    F.coalesce(df1.type, df2.type2).alias('type')
)

df_joined.show()
+-----+-----+------+
|   ID|Value|  type|
+-----+-----+------+
|user0|  100|   Car|
|user1|  102|   Car|
|user2|  109|   Dog|
|user3|  103|    NA|
|user4|  110|   Dog|
|user5|  115|  Cell|
|user6|  103|  Cell|
|user7|  100|Fridge|
+-----+-----+------+

import android.app.Fragment;

代替