在我的示例项目中,我使用activity实现了代码,运行正常。
之后,我在我的主App中使用Fragment实现了该代码。
为此,我扩展了Fragment,制作了一个默认构造函数,制作了onCreateView
&在内部夸大了布局,然后将getApplicationContext()
更改为getActivity()
。多数民众赞成我如何设法删除我的主应用程序中的错误。
在我的MainActivity中,我们通过调用构造函数&更换片段来更新主要内容。将它分配给片段:
/**
* Diplaying fragment view for selected nav drawer list item
* */
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new IntroductionFragment();
break;
case 1:
fragment = new PrefaceFragment();
break;
case 2:
fragment = new PreambleFragment();
break;
case 3:
fragment = new ContentsFragment();//ERROR here: because I modified this class
break;
case 4:
fragment = new SchedulesFragment();
break;
case 5:
fragment = new AppendixFragment();
break;
case 6:
fragment = new AmendmentFragment();
break;
case 7:
fragment = new PhotoFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
该行显示Type mismatch: cannot convert from ContentsFragment to Fragment
我希望问题很清楚。请告诉我如何避免此错误...
这是ContentsFragment.java
public class ContentsFragment extends Fragment// implements OnTouchListener
{
public ContentsFragment() {
// TODO Auto-generated constructor stub
}
public static ArrayList<GS> q = new ArrayList<GS>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DBAdapter db = DBAdapter.getDBAdapter(getActivity());
if (!db.checkDatabase())
db.createDatabase(getActivity());
db.openDatabase();
q = db.getData();
// setContentView(R.layout.sample_activity);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v =inflater.inflate(R.layout.sample_activity, container, false);
final FloatingGroupExpandableListView list = (FloatingGroupExpandableListView) v.findViewById(R.id.sample_activity_list);
// final LayoutInflater inflater = getLayoutInflater();
// Even though the child divider has already been set on the layout file, we have to set it again here
// This prevents a bug where the background turns to the color of the child divider when the list is expanded
list.setChildDivider(new ColorDrawable(Color.BLACK));
final ArticlesAdapter adapter = new ArticlesAdapter(getActivity());
final WrapperExpandableListAdapter wrapperAdapter = new WrapperExpandableListAdapter(adapter);
list.setAdapter(wrapperAdapter);
for(int i = 0; i < wrapperAdapter.getGroupCount(); i++) {
list.collapseGroup(i);//expandGroup(i);
}
list.setOnScrollFloatingGroupListener(new FloatingGroupExpandableListView.OnScrollFloatingGroupListener() {
@Override
public void onScrollFloatingGroupListener(View floatingGroupView, int scrollY) {
float interpolation = - scrollY / (float) floatingGroupView.getHeight();
// Changing from RGB(162,201,85) to RGB(255,255,255)
final int greenToWhiteRed = (int) (0 + 3 * interpolation);
final int greenToWhiteGreen = (int) (0 + 6* interpolation);
final int greenToWhiteBlue = (int) ( 0+ 20 * interpolation);
final int greenToWhiteColor = Color.argb(200, greenToWhiteRed, greenToWhiteGreen, greenToWhiteBlue);
// Changing from RGB(255,255,255) to RGB(0,0,0)
final int whiteToBlackRed = (int) (255 - 1 * interpolation);
final int whiteToBlackGreen = (int) (255 - 1 * interpolation);
final int whiteToBlackBlue = (int) (255 - 1 * interpolation);
final int whiteToBlackColor = Color.argb(255, whiteToBlackRed, whiteToBlackGreen, whiteToBlackBlue);
final View background = floatingGroupView.findViewById(R.id.sample_activity_list_group_item_background);
background.setBackgroundColor(greenToWhiteColor);
final TextView text = (TextView) floatingGroupView.findViewById(R.id.sample_activity_list_group_item_text);
text.setTextColor(whiteToBlackColor);
final ImageView expanded = (ImageView) floatingGroupView.findViewById(R.id.sample_activity_list_group_expanded_image);
final Drawable expandedDrawable = expanded.getDrawable().mutate();
expandedDrawable.setColorFilter(whiteToBlackColor, PorterDuff.Mode.SRC_ATOP);
}
});
return v;
}
}
答案 0 :(得分:2)
我认为问题是因为从两个不同类别的不同来源导入Fragment
,即。 Your_Activity
和ContentsFragment.java
。一个来自android.support.v4.app.Fragment
,一个来自android.app.Fragment
。
请确保您从支持库中导入Fragment,即android.support.v4.app.Fragment
或android.app.Fragment
。