android - 你是否必须在清单中添加片段

时间:2012-02-22 07:33:25

标签: android manifest fragment

我正在使用一个应该显示webview的片段。当我尝试从使用它的类中实例化它时,我在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();
    }


    }
    });
 }
}

2 个答案:

答案 0 :(得分:32)

不要将其添加到您的清单中。您永远不需要在清单中添加片段。

您是否在某处创建了一个Intent来启动WebActivity?如何将它带到屏幕上,这可能就是你的问题所在。

修改

这是你的问题:

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

您无法将片段作为活动启动,您必须将片段包装在扩展FragmentActivity

的活动中

答案 1 :(得分:0)

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

proToolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

//注意:您必须为活动的主布局指定一个ID,例如:searchVehicles

            ((ConstraintLayout) findViewById(R.id.searchVehicles)).removeAllViews();
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragment_nearby_vehicles fnv = new fragment_nearby_vehicles();
            fragmentTransaction.add(R.id.searchVehicles, fnv).commit();
        }
    });