我有一个扩展TabActivity
的类由于TabActivity已弃用,我想使用FragmentTabHost进行转换。 问题是当我启动应用程序时,我收到此错误。 因为我还是Android上的新手,有人可以帮我解决这个问题。 TabActivity可以在新设备中给我一些问题,还是我仍然可以使用whiteout问题?
谢谢
这是日志猫
05-15 22:22:10.365: E/AndroidRuntime(2452): FATAL EXCEPTION: main
05-15 22:22:10.365: E/AndroidRuntime(2452): java.lang.ClassCastException: com.wilbur.app.ui.CatalogLocalListActivity cannot be cast to android.support.v4.app.Fragment
05-15 22:22:10.365: E/AndroidRuntime(2452): at android.support.v4.app.Fragment.instantiate(Fragment.java:402)
05-15 22:22:10.365: E/AndroidRuntime(2452): at android.support.v4.app.FragmentTabHost.doTabChanged(FragmentTabHost.java:343)
05-15 22:22:10.365: E/AndroidRuntime(2452): at android.support.v4.app.FragmentTabHost.onAttachedToWindow(FragmentTabHost.java:280)
05-15 22:22:10.365: E/AndroidRuntime(2452): at android.view.View.dispatchAttachedToWindow(View.java:12761)
05-15 22:22:10.365: E/AndroidRuntime(2452): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2642)
05-15 22:22:10.365: E/AndroidRuntime(2452): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2649)
05-15 22:22:10.365: E/AndroidRuntime(2452): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2649)
05-15 22:22:10.365: E/AndroidRuntime(2452): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2649)
05-15 22:22:10.365: E/AndroidRuntime(2452): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1470)
05-15 22:22:10.365: E/AndroidRuntime(2452): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1230)
05-15 22:22:10.365: E/AndroidRuntime(2452): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5089)
05-15 22:22:10.365: E/AndroidRuntime(2452): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
05-15 22:22:10.365: E/AndroidRuntime(2452): at android.view.Choreographer.doCallbacks(Choreographer.java:591)
05-15 22:22:10.365: E/AndroidRuntime(2452): at android.view.Choreographer.doFrame(Choreographer.java:561)
05-15 22:22:10.365: E/AndroidRuntime(2452): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
05-15 22:22:10.365: E/AndroidRuntime(2452): at android.os.Handler.handleCallback(Handler.java:725)
05-15 22:22:10.365: E/AndroidRuntime(2452): at android.os.Handler.dispatchMessage(Handler.java:92)
05-15 22:22:10.365: E/AndroidRuntime(2452): at android.os.Looper.loop(Looper.java:137)
05-15 22:22:10.365: E/AndroidRuntime(2452): at android.app.ActivityThread.main(ActivityThread.java:5283)
05-15 22:22:10.365: E/AndroidRuntime(2452): at java.lang.reflect.Method.invokeNative(Native Method)
05-15 22:22:10.365: E/AndroidRuntime(2452): at java.lang.reflect.Method.invoke(Method.java:511)
05-15 22:22:10.365: E/AndroidRuntime(2452): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
05-15 22:22:10.365: E/AndroidRuntime(2452): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
05-15 22:22:10.365: E/AndroidRuntime(2452): at dalvik.system.NativeStart.main(Native Method)
这是在FragmentTabHost中转换的活动
public class CatalogTabHostActivity extends FragmentActivity {
public static final int RESULT_EULA_CANCELED = 100;
private static final String TAB_LOCAL = "local";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.catalog_tab_host);
mInstance = this;
Intent i = getIntent();
Bundle extras = null;
if(i!=null){
extras = i.getExtras();
}
FragmentTabHost mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(CatalogTabHostActivity.this, getSupportFragmentManager(), android.R.id.tabcontent);
Intent intentLocalCatalog = new Intent(this, CatalogLocalListActivity.class);
if(extras!=null){
intentLocalCatalog.putExtras(extras);
}
mTabHost.addTab( mTabHost.newTabSpec(TAB_LOCAL).setIndicator(""), CatalogLocalListActivity.class, null);
mTabHost.getTabWidget().getChildTabViewAt(0).setVisibility(View.GONE);
}
protected void onResume() {
if(!GlobalSettings.isAcceptedEULA(this)){
}else if(!GlobalSettings.isChangeLogShowed(this)){
// ChangeLogDialog.show(this);
ApplicationEx.getInstance().invalidateAutoUpdate();
GlobalSettings.setChangeLogShowed(this);
}
super.onResume();
}
private static CatalogTabHostActivity mInstance;
public static Activity getInstance()
{
return mInstance;
}
}
这是CatalogLocalListActivity
public class CatalogLocalListActivity extends BaseCatalogActivity {
public static final String EXTRA_INVOKE_LOCAL_UPDATE_LIST = "EXTRA_INVOKE_LOCAL_UPDATE_LIST";
private boolean mInvokeSelectCurrent;
private boolean mInvokeUpdateList;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mInvokeSelectCurrent = true;
Intent data = getIntent();
if(data!=null){
mInvokeUpdateList = data.getBooleanExtra(EXTRA_INVOKE_LOCAL_UPDATE_LIST, false);
}
}
protected void setListView() {
super.setListView();
if(mInvokeSelectCurrent){
mInvokeSelectCurrent = false;
GlobalSettings.MapPath path = GlobalSettings.getCurrentMap(this);
String systemMapName = path.getSystemMapName();
if(systemMapName!=null){
int position = mAdapter.findItemPosition(systemMapName);
if(position!=-1){
mList.setSelection(position);
}
}
}
if(mInvokeUpdateList){
mInvokeUpdateList = false;
invokeSelectUpdateMaps();
}
}
protected boolean isCatalogProgressEnabled(int catalogId) {
return catalogId == CatalogStorage.LOCAL;
}
protected int getEmptyListHeader() {
return R.string.msg_no_maps_in_local_header;
}
protected int getEmptyListMessage() {
return R.string.msg_no_maps_in_local;
}
public int getCatalogState(CatalogMap local, CatalogMap remote) {
return mRemote!=null && mLocal!=null ? mStorageState.getLocalCatalogState(local, remote) : CatalogMapState.CALCULATING;
}
public boolean onCatalogMapClick(CatalogMap local, CatalogMap remote, int state) {
switch(state){
case CALCULATING:
if(local.isAvailable()){
invokeFinish(local);
}else{
invokeMapDetails(local,remote,state);
}
return true;
case OFFLINE:
case INSTALLED:
case UPDATE:
invokeFinish(local);
return true;
case IMPORT:
case DOWNLOAD:
case DOWNLOAD_PENDING:
case DOWNLOADING:
case IMPORT_PENDING:
case IMPORTING:
case NEED_TO_UPDATE:
case NOT_SUPPORTED:
case UPDATE_NOT_SUPPORTED:
case CORRUPTED:
invokeMapDetails(local,remote,state);
return true;
// do nothing
}
return true;
}
protected int getDiffMode() {
return CatalogMapPair.DIFF_MODE_LOCAL;
}
protected int getLocalCatalogId() {
return CatalogStorage.LOCAL;
}
protected int getRemoteCatalogId() {
return CatalogStorage.ONLINE;
}
protected int getDiffColors() {
return R.array.local_catalog_map_state_colors;
}
/****************** MAIN MENU ********************/
private final int MAIN_MENU_UPDATE_MAPS = 1;
private final int MAIN_MENU_DELETE_MAPS = 2;
private final static int REQUEST_UPDATE = 1;
private final static int REQUEST_DELETE = 2;
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, MAIN_MENU_UPDATE_MAPS, 5, R.string.menu_update_maps).setIcon(R.drawable.icon_tab_import_selected);
menu.add(0, MAIN_MENU_DELETE_MAPS, 6, R.string.menu_delete_maps).setIcon(android.R.drawable.ic_menu_delete);
return true;
}
public boolean onPrepareOptionsMenu(Menu menu) {
menu.findItem(MAIN_MENU_UPDATE_MAPS).setEnabled(mMode == MODE_LIST && !mStorage.hasTasks());
menu.findItem(MAIN_MENU_DELETE_MAPS).setEnabled(mMode == MODE_LIST && !mStorage.hasTasks());
return super.onPrepareOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MAIN_MENU_UPDATE_MAPS:
invokeSelectUpdateMaps();
return true;
case MAIN_MENU_DELETE_MAPS:
invokeSelectDeleteMaps();
return true;
}
return super.onOptionsItemSelected(item);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_UPDATE:
if(resultCode == RESULT_OK && data!=null){
String[] names = data.getStringArrayExtra(CatalogMapSelectionActivity.EXTRA_SELECTION);
for (String systemName : names) {
mStorage.requestDownload(systemName);
}
}
if(resultCode == CatalogMapSelectionActivity.RESULT_MAP_LIST_EMPTY){
Toast.makeText(this, R.string.msg_no_maps_to_update, Toast.LENGTH_SHORT).show();
}
break;
case REQUEST_DELETE:
if(resultCode == RESULT_OK && data!=null){
String[] names = data.getStringArrayExtra(CatalogMapSelectionActivity.EXTRA_SELECTION);
for (String systemName : names) {
mStorage.deleteLocalMap(systemName);
}
}
if(resultCode == CatalogMapSelectionActivity.RESULT_MAP_LIST_EMPTY){
Toast.makeText(this, R.string.msg_no_maps_to_delete, Toast.LENGTH_SHORT).show();
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
private void invokeSelectDeleteMaps() {
Intent i = new Intent(this, CatalogMapSelectionActivity.class);
i.putExtra(CatalogMapSelectionActivity.EXTRA_TITLE, getText(R.string.menu_delete_maps));
i.putExtra(CatalogMapSelectionActivity.EXTRA_REMOTE_ID, CatalogStorage.ONLINE);
i.putExtra(CatalogMapSelectionActivity.EXTRA_REMOTE_MODE, CatalogMapPair.DIFF_MODE_LOCAL);
i.putExtra(CatalogMapSelectionActivity.EXTRA_FILTER, mActionBarEditText.getText().toString());
i.putExtra(CatalogMapSelectionActivity.EXTRA_SORT_MODE, CheckedCatalogAdapter.SORT_MODE_COUNTRY);
startActivityForResult(i, REQUEST_DELETE);
}
private void invokeSelectUpdateMaps() {
Intent i = new Intent(this, CatalogMapSelectionActivity.class);
i.putExtra(CatalogMapSelectionActivity.EXTRA_TITLE, getText(R.string.menu_update_maps));
i.putExtra(CatalogMapSelectionActivity.EXTRA_REMOTE_ID, CatalogStorage.ONLINE);
i.putExtra(CatalogMapSelectionActivity.EXTRA_FILTER, mActionBarEditText.getText().toString());
i.putExtra(CatalogMapSelectionActivity.EXTRA_SORT_MODE, CheckedCatalogAdapter.SORT_MODE_COUNTRY);
i.putExtra(CatalogMapSelectionActivity.EXTRA_CHECKABLE_STATES, new int[]{ CatalogMapState.UPDATE, CatalogMapState.NEED_TO_UPDATE } );
i.putExtra(CatalogMapSelectionActivity.EXTRA_VISIBLE_STATES, new int[]{ CatalogMapState.UPDATE, CatalogMapState.NEED_TO_UPDATE } );
startActivityForResult(i, REQUEST_UPDATE);
}
}
答案 0 :(得分:0)
引用the documentation for FragmentTabHost
:
特殊TabHost,允许将Fragment对象用于其选项卡内容。
您正在尝试将Activity
作为其标签内容。请将其转换为Fragment
。