android - 你是否需要将片段添加到清单中
我使用一个应该显示网络视图的片段。当我尝试从使用它的类实例化它时,我在 logcat 中收到以下警告。
02-21 23:26:46.843: W/System.err(32468): android.content.ActivityNotFoundException: Unable to find explicit activity class {get.scanner/get.scanner.WebFrag}; have you declared this activity in your AndroidManifest.xml?
我刚刚学习如何使用片段,我从未尝试在我的清单中声明它们,也没有看到任何地方告诉你这样做。
这是 WebFrag 类。
public class WebFrag extends Fragment{
private WebView viewer = null;
// if we weren't just using the compat library, we could use WebViewFragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
viewer = (WebView) inflater
.inflate(R.layout.webview, container, false);
WebSettings settings = viewer.getSettings();
settings.setJavaScriptEnabled(true);
settings.setDefaultZoom(ZoomDensity.FAR);
return viewer;
}
@Override
public void onPause() {
if (viewer != null) {
viewer.onPause();
}
super.onPause();
}
@Override
public void onResume() {
super.onResume();
if (viewer != null) {
viewer.onResume();
}
}
public void updateUrl(String newUrl) {
if (viewer != null) {
viewer.loadUrl(newUrl);
}
}
}
编辑:将 WebFrag 作为活动添加到清单中会导致以下错误
02-22 00:17:55.711: E/AndroidRuntime(2524): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{get.scanner/get.scanner.WebFrag}: java.lang.ClassCastException: get.scanner.WebFrag
编辑:这是我尝试使用我的类的主要fragmentactivity
public class GetScannerActivity extends FragmentActivity {
private String mUrl = "http://www.yahoo.com/";
Button scanButton;
Button paint;
Button compTrans;
String yurl = "http://www.yahoo.com/";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
compTrans = (Button) findViewById(R.id.checkCurrentDeals);
compTrans.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
WebFrag viewer = (WebFrag) getSupportFragmentManager()
.findFragmentById(R.id.web_frag);
try{
if (viewer == null || !viewer.isInLayout()) {
Intent showContent = new Intent(getApplicationContext(),
WebFrag.class);
showContent.setData(Uri.parse(yurl));
try{
startActivity(showContent);
}catch(ActivityNotFoundException e){
e.printStackTrace();
}
} else {
viewer.updateUrl(yurl);
}
}catch(Exception e){
e.printStackTrace();
}
}
});
}
}
Im using a fragment that is supposed to display a webview. When I try to instantiate it from the class that uses it I get the following warning in my logcat.
02-21 23:26:46.843: W/System.err(32468): android.content.ActivityNotFoundException: Unable to find explicit activity class {get.scanner/get.scanner.WebFrag}; have you declared this activity in your AndroidManifest.xml?
Im just learning how to use fragments and Ive never tried declaring them in my manifest and I havent seen anywhere telling you to do so.
Heres the WebFrag class.
public class WebFrag extends Fragment{
private WebView viewer = null;
// if we weren't just using the compat library, we could use WebViewFragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
viewer = (WebView) inflater
.inflate(R.layout.webview, container, false);
WebSettings settings = viewer.getSettings();
settings.setJavaScriptEnabled(true);
settings.setDefaultZoom(ZoomDensity.FAR);
return viewer;
}
@Override
public void onPause() {
if (viewer != null) {
viewer.onPause();
}
super.onPause();
}
@Override
public void onResume() {
super.onResume();
if (viewer != null) {
viewer.onResume();
}
}
public void updateUrl(String newUrl) {
if (viewer != null) {
viewer.loadUrl(newUrl);
}
}
}
EDIT: adding WebFrag as an activity to the manifest causes the following error
02-22 00:17:55.711: E/AndroidRuntime(2524): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{get.scanner/get.scanner.WebFrag}: java.lang.ClassCastException: get.scanner.WebFrag
EDIT: Heres the main fragmentactivity where Im trying to use my class
public class GetScannerActivity extends FragmentActivity {
private String mUrl = "http://www.yahoo.com/";
Button scanButton;
Button paint;
Button compTrans;
String yurl = "http://www.yahoo.com/";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
compTrans = (Button) findViewById(R.id.checkCurrentDeals);
compTrans.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
WebFrag viewer = (WebFrag) getSupportFragmentManager()
.findFragmentById(R.id.web_frag);
try{
if (viewer == null || !viewer.isInLayout()) {
Intent showContent = new Intent(getApplicationContext(),
WebFrag.class);
showContent.setData(Uri.parse(yurl));
try{
startActivity(showContent);
}catch(ActivityNotFoundException e){
e.printStackTrace();
}
} else {
viewer.updateUrl(yurl);
}
}catch(Exception e){
e.printStackTrace();
}
}
});
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,不要将其添加到您的清单中。您永远不需要将片段添加到清单中。
您是否在某处创建 Intent 来启动 WebActivity?它是如何带到屏幕上的,这可能就是您的问题所在。
编辑
这是你的问题:
你不能将片段作为活动启动,你必须将片段包装在扩展 FragmentActivity
No don't add it to your manifest. You never need to add fragments to your manifest.
Do you create an Intent somewhere to start the WebActivity? How is it brought to the screen, that is probably where your problem lies.
EDIT
This is your problem:
You can't start a Fragment as an Activity, you'll have to wrap the fragment in an Activity that extends FragmentActivity
如果您想从 Activity 返回到 Fragment,请尝试此
//注意:您必须为 Activity 的主布局提供 id,例如:searchVehicles
if you want to go back from activity to Fragment try this
//Note: you must give an id to the Main layout of your activity e.g : searchVehicles