【问题标题】:android - do you ever have to add fragments to the manifestandroid - 您是否必须向清单中添加片段
【发布时间】:2012-03-12 13:00:43
【问题描述】:

我正在使用应该显示 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


    【解决方案1】:

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

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

    编辑

    这是你的问题:

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

    您不能将 Fragment 作为 Activity 启动,您必须将 Fragment 包装在扩展 FragmentActivity 的 Activity 中

    【讨论】:

    • 是的,我很确定你不会那样做。我会在我认为我正在使用它的地方发布我的其他课程。
    • 包装片段是否意味着创建一个扩展我的 WebFrag 类的片段活动?
    • 不,您将创建一个扩展 FragmentActivity 的“WebActivity”。然后在 WebActivity 的 xml 布局中,您将添加您的片段,即
    • 这就是我现在正在做的事情,但由于某种原因它永远不会加载 webfragment。这就是我尝试实现按钮的原因。片段是否应该在布局中声明后才加载,然后在 setContentView() 中设置布局?
    • 是的,在片段中添加一些Log.d("",""); 行,看看它是否正在加载但未显示。
    【解决方案2】:

    如果你想从活动返回到 Fragment,试试这个

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-04
      • 1970-01-01
      • 1970-01-01
      • 2010-10-14
      • 1970-01-01
      • 2012-05-12
      • 1970-01-01
      相关资源
      最近更新 更多