android-您是否必须向清单添加片段

问题描述 投票:20回答:2

我正在使用应该显示Web视图的片段。当我尝试从使用它的类中实例化它时,我的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

编辑:这是我试图使用我的班级的主要片段活动

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();
    }


    }
    });
 }
}
android manifest fragment
2个回答
32
投票

不,不要将其添加到清单中。您无需在清单中添加片段。

您是否在某个地方创建了一个Intent以启动WebActivity?如何将其显示在屏幕上,这可能是您的问题所在。

编辑

这是您的问题:

 Intent showContent = new Intent(getApplicationContext(),
            WebFrag.class);
 startActivity(showContent);

您无法将片段作为活动开始,您必须将片段包装在扩展了FragmentActivity的活动中>


0
投票

如果您想从活动返回到片段,请尝试此操作

© www.soinside.com 2019 - 2024. All rights reserved.