片段无法转换为android.app.activity [英] Fragment cannot be cast to an android.app.activity
问题描述
我创建了一个非常fragment
来测试我的应用,并且收到以下错误消息:
I created a very fragment
to test my app and I got the following error message:
03-28 11:11:58.079:E/AndroidRuntime(11760): java.lang.RuntimeException:无法实例化活动 ComponentInfo {com.android.demoresponsevision/com.android.demoresponsevision.fragment.BarcodeScreen}: java.lang.ClassCastException: com.android.demoresponsevision.fragment.BarcodeScreen无法投射 到android.app.Activity
03-28 11:11:58.079: E/AndroidRuntime(11760): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.android.demoresponsevision/com.android.demoresponsevision.fragment.BarcodeScreen}: java.lang.ClassCastException: com.android.demoresponsevision.fragment.BarcodeScreen cannot be cast to android.app.Activity
片段代码:-
public class BarcodeScreen extends Fragment {
TextView tvStatus;
TextView tvResult;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.barcode, container, false);
tvStatus = (TextView) view.findViewById(R.id.tvStatus);
tvResult = (TextView) view.findViewById(R.id.tvResult);
Button scanBtn = (Button) view.findViewById(R.id.btnScan);
// in some trigger function e.g. button press within your code you
// should add:
scanBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
Utilites.showToast(getActivity(), "Try");
Intent intent = new Intent(
"com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE,PRODUCT_MODE");
startActivityForResult(intent, 0);
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getActivity(), "ERROR:" + e, 1).show();
}
}
});
return view;
}
// In the same activity you’ll need the following to retrieve the results:
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
Toast.makeText(getActivity(), "result ", 1000).show();
if (requestCode == 0) {
if (resultCode == getActivity().RESULT_OK) {
tvStatus.setText(intent.getStringExtra("SCAN_RESULT_FORMAT"));
tvResult.setText(intent.getStringExtra("SCAN_RESULT"));
}
else if (resultCode == getActivity().RESULT_CANCELED) {
tvStatus.setText("Press a button to start a scan.");
tvResult.setText("Scan cancelled.");
}
}
}
}
这是我的片段活动
public class TabActivity extends FragmentActivity implements
OnTabChangeListener {
TextView txt;
private TabHost mTabHost;
private HashMap mapTabInfo = new HashMap();
private TabInfo mLastTab = null;
private class TabInfo {
private String tag;
private Class clss;
private Bundle args;
private Fragment fragment;
TabInfo(String tag, Class clazz, Bundle args) {
this.tag = tag;
this.clss = clazz;
this.args = args;
}
}
class TabFactory implements TabContentFactory {
private final Context mContext;
/** * @param context */
public TabFactory(Context context) {
mContext = context;
}
public View createTabContent(String tag) {
View v = new View(mContext);
v.setMinimumWidth(0);
v.setMinimumHeight(0);
return v;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs);
// Step 2: Setup TabHost
initialiseTabHost(savedInstanceState);
if (savedInstanceState != null) {
mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
}
}
protected void onSaveInstanceState(Bundle outState) {
outState.putString("tab", mTabHost.getCurrentTabTag()); // save the tab
// selected
super.onSaveInstanceState(outState);
}
/**
* Step 2: Setup TabHost
*/
private void initialiseTabHost(Bundle args) {
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
TabInfo tabInfo = null;
TabActivity.addTab(this, this.mTabHost, this.mTabHost
.newTabSpec("Tab1").setIndicator("HomeScreen"),
(tabInfo = new TabInfo("Tab1", HomeScreen.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
TabActivity.addTab(this, this.mTabHost, this.mTabHost
.newTabSpec("Tab2").setIndicator("SecondScreen"),
(tabInfo = new TabInfo("Tab2", SecondHomeScreen.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
// Default to first tab
this.onTabChanged("Tab1");
//
mTabHost.setOnTabChangedListener((OnTabChangeListener) this);
}
private static void addTab(TabActivity activity, TabHost tabHost,
TabHost.TabSpec tabSpec, TabInfo tabInfo) { // Attach a Tab view
// factory to the spec
tabSpec.setContent(activity.new TabFactory(activity));
String tag = tabSpec.getTag();
// Check to see if we already have a fragment for this tab, probably
// from a previously saved state. If so, deactivate it, because our
// initial state is that a tab isn't shown.
tabInfo.fragment = activity.getSupportFragmentManager()
.findFragmentByTag(tag);
if (tabInfo.fragment != null && !tabInfo.fragment.isDetached()) {
FragmentTransaction ft = activity.getSupportFragmentManager()
.beginTransaction();
ft.detach(tabInfo.fragment);
ft.commit();
activity.getSupportFragmentManager().executePendingTransactions();
}
tabHost.addTab(tabSpec);
}
/**
* (non-Javadoc) * @see
* android.widget.TabHost.OnTabChangeListener#onTabChanged(java.lang.String)
*/
public void onTabChanged(String tag) {
TabInfo newTab = (TabInfo) this.mapTabInfo.get(tag);
if (mLastTab != newTab) {
FragmentTransaction ft = this.getSupportFragmentManager()
.beginTransaction();
if (mLastTab != null) {
if (mLastTab.fragment != null) {
ft.detach(mLastTab.fragment);
}
}
if (newTab != null) {
if (newTab.fragment == null) {
newTab.fragment = Fragment.instantiate(this,
newTab.clss.getName(), newTab.args);
ft.add(R.id.realtabcontent, newTab.fragment, newTab.tag);
} else {
ft.attach(newTab.fragment);
}
}
mLastTab = newTab;
ft.commit();
this.getSupportFragmentManager().executePendingTransactions();
}
}
}
主屏幕:-
public class HomeScreen extends Fragment implements OnItemClickListener {
ListView list;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.home, container, false);
list = (ListView) view.findViewById(R.id.list);
String[] values = new String[] { "Barcode scanner", "Photos ", "Map",
"Signature" };
ArrayAdapter<String> files = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, values);
list.setAdapter(files);
list.setOnItemClickListener(this);
return view;
}
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
if (position == 0) {
Utilites.showToast(getActivity(), "Barcode");
Intent i_barcode = new Intent(getActivity(), BarcodeScreen.class);
startActivity(i_barcode);
}
if (position == 1) {
Utilites.showToast(getActivity(), "Photos");
Intent i_photo = new Intent(getActivity(), PhotoScreen.class);
startActivity(i_photo);
}
if (position == 2) {
Utilites.showToast(getActivity(), "Map");
Intent i_map = new Intent(getActivity(), MapScreen.class);
startActivity(i_map);
}
if (position == 3) {
Utilites.showToast(getActivity(), "Signature");
Intent i_signature = new Intent(getActivity(), BarcodeScreen.class);
startActivity(i_signature);
}
}
}
推荐答案
无法实例化活动 ComponentInfo {com.android.demoresponsevision/com.android.demoresponsevision.fragment.BarcodeScreen}: java.lang.ClassCastException: com.android.demoresponsevision.fragment.BarcodeScreen无法投射 到android.app.Activity
Unable to instantiate activity ComponentInfo{com.android.demoresponsevision/com.android.demoresponsevision.fragment.BarcodeScreen}: java.lang.ClassCastException: com.android.demoresponsevision.fragment.BarcodeScreen cannot be cast to android.app.Activity
此stakctrace明确表明BarcodeScreen
不是Activity
.
This stakctrace clearly indicates that BarcodeScreen
is not a Activity
.
您正在使用startActivity在代码中的某些位置进行分段.
You are using startActivity for fragments some where in your code.
问题由以下代码确认
Intent i_barcode = new Intent(getActivity(), BarcodeScreen.class);
startActivity(i_barcode);
您还说过,您在清单中输入了BarcodeScreen
.片段不是活动.所以你在做什么是错误的.
And you also said you made an entry of BarcodeScreen
in manifest. Fragment is not a Activity. So what you are doing is wrong.
您需要有一个ViewGroup
容器,并向其中添加或替换片段.
You need to have a container which is a ViewGroup
and add or replace fragments to it.
阅读
http://developer.android.com/guide/components/fragments.html
这篇关于片段无法转换为android.app.activity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!