简体   繁体   English

膨胀 class 片段时出错。 我可以在这里做什么?

[英]Error inflating class fragment. What can I do here?

I was coding this program the other day, hoping that the fragment was doing okay, but it isn't.前几天我正在编写这个程序,希望片段运行良好,但事实并非如此。 My project is a Relative Layout app where the GPS map will show up on the screen first thing the activity pops up, and then the choices to view some of the Javanese culture of Indonesia (in the form of Grid - Card View combination), will be available for selection.我的项目是一个相对布局应用程序,其中 GPS map 将首先出现在屏幕上活动弹出,然后选择查看印度尼西亚的一些爪哇文化(以网格 - 卡片视图组合的形式),将可供选择。

Here, in this line, I have a class fragment inflation error, according to Logcat:在这里,根据 Logcat,在这一行中,我有一个 class 片段膨胀错误:

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_java_island); // Erroring line

    KerisCard = (CardView) findViewById(R.id.KerisCard);
    TradClothesCard = (CardView) findViewById(R.id.TradClothesCard);
    TradHouseCard = (CardView) findViewById(R.id.TradHouseCard);
    TariMerakCard = (CardView) findViewById(R.id.TariMerakCard);



    SupportMapFragment javaMapFragment = (SupportMapFragment) getSupportFragmentManager().
            findFragmentById(R.id.java_island_map);
    javaMapFragment.getMapAsync(this); . . . }

In retrospect, I'm not very familiar to the concept of fragments as the classes I've been attending aren't going too detailed on the usage of fragments.回想起来,我对片段的概念并不是很熟悉,因为我一直在参加的课程对片段的使用没有太详细的说明。 Also, I have to note without the declaring the object references ((CardView) declarations on the onCreate() function), this error would not show up, and the app will be executable as normal.另外,我必须注意,如果没有声明 object 引用(onCreate() 函数上的 (CardView) 声明),则不会出现此错误,并且该应用程序将正常执行。

Instead of making support fragment reference create fragment reference directly and call method from fragment.而不是支持片段引用直接创建片段引用并从片段调用方法。 SampleFragment fragment=new SampleFragment() Fragment.someMethod Here is a confusion Do you want to open fragment in activity? SampleFragment fragment=new SampleFragment() Fragment.someMethod 这里有一个困惑你想在activity中打开fragment吗? Because you are trying to find fragment from id,so have you created in xml if not then there is problem.因为您正在尝试从 id 中查找片段,所以您是否在 xml 中创建了如果没有,那么就有问题了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM