当前位置: 代码迷 >> Android >> FragmentTabHost实现底部菜单栏报错!
  详细解决方案

FragmentTabHost实现底部菜单栏报错!

热度:39   发布时间:2016-04-28 02:51:01.0
FragmentTabHost实现底部菜单栏出错!!
package com.colcie.viewpagertest3;

import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.text.StaticLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TabHost;
import android.widget.TextView;

public class MainActivity extends FragmentActivity {
public static Context context ;
/**
 * 中间内容的fragment
 */
private Fragment frag_content;

/**
 * FragmentTabHoast
 */
private FragmentTabHost mTabHost;
/**
 * 布局填充器
 */
private LayoutInflater mLayoutInflater;
/**
 * Fragment数组界面
 */
private Class mFragmentArray[] = {
Fragment1.class,Fragment2.class,Fragment3.class
};

/**
 * 存放数组图片
 */
private int mImageArray[] = {
R.drawable.yufa,R.drawable.yufaben,R.drawable.shezhi
};
/**
 * 选修卡文字
 */
private String mTextArray[] = {
"语法","语法本","设置"
};

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 initView();
 

}

/**
 * 初始化組件
 */
private void initView() {
mLayoutInflater = LayoutInflater.from(this);
//找到TabHost
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this,getSupportFragmentManager(),R.id.realtabcontent);
int count = mFragmentArray.length;
for(int i = 0; i<count;i++){
//给每个Tab按钮设置图标、文字和内容
// ImageView imageView = new ImageView(this);
// imageView.setImageDrawable(getResources().getDrawable(mImageArray[i]));
// imageView.setScaleType(ScaleType.CENTER_CROP);
//
TabSpec tabSpec =  mTabHost.newTabSpec(mTextArray[i]).setIndicator(getTabItemView(i));
mTabHost.addTab(tabSpec, mFragmentArray[i], null); 

}
mTabHost.setCurrentTab(0);


}

private View getTabItemView(int i) {
View view = mLayoutInflater.inflate(R.layout.tab_item_view, null);
ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
imageView.setImageResource(mImageArray[i]);
TextView textView = (TextView) view.findViewById(R.id.textView);
textView.setText(mTextArray[i]);

return view;
}
}
网上的代码 拿过来用 报错:
01-13 11:23:09.099: E/AndroidRuntime(31588): java.lang.ClassCastException: com.colcie.viewpagertest3.Fragment1 cannot be cast to android.support.v4.app.Fragment
求大神解答!!!!!!!!!!
------解决思路----------------------
Fragment1也是support.v4.app.Fragment 么,会不会是导包错误例如添加成了android.app.Fragment。