我已经创建了一个片段,Fragment01,扩展了MapFragment
。问题是,因为它不再扩展Fragment
,现在我的MainActivity的onSelectFragment
部分不再工作了。我需要改变我的MainActivity让它像以前一样工作吗?我得到的错误是在newFragment = new Fragment01();
上,它说类型不匹配:无法从Fragment01转换为Fragment。作为一个新手,我只是尝试了newMapFragment = new Fragment01();
,但没有做到。
MainActivity
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
StartFragment startfragment = new StartFragment();
transaction.add(R.id.fragment_placeholder, startfragment);
transaction.commit();
}
public void onSelectFragment(View view){
Fragment newFragment;
if (view == findViewById(R.id.btnstartfragment)) {
newFragment = new StartFragment();
} else if (view == findViewById(R.id.btnfragment01)) {
newFragment = new Fragment01();
} else if (view == findViewById(R.id.btnfragment02)) {
newFragment = new Fragment02();
} else {
newFragment = new StartFragment();
}
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_placeholder, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
不同的Fragment
层次结构可能是错误的:来自支持库和平台。检查MapFragment
的导入和层次结构。
有两种不同的类型:
android.support.v4.app.Fragment
android.app.Fragment