android - 你是否需要将片段添加到清单中

发布于 2025-01-07 12:53:31 字数 2744 浏览 1 评论 0原文

我使用一个应该显示网络视图的片段。当我尝试从使用它的类实例化它时,我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鱼窥荷 2025-01-14 12:53:31

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

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

编辑

这是你的问题:

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

你不能将片段作为活动启动,你必须将片段包装在扩展 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:

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

You can't start a Fragment as an Activity, you'll have to wrap the fragment in an Activity that extends FragmentActivity

新人笑 2025-01-14 12:53:31

如果您想从 Activity 返回到 Fragment,请尝试此

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

//注意:您必须为 Activity 的主布局提供 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();
        }
    });

if you want to go back from activity to Fragment try this

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

//Note: you must give an id to the Main layout of your activity e.g : 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();
        }
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文