布局膨胀错误片段

Posted

技术标签:

【中文标题】布局膨胀错误片段【英文标题】:Layout Inflate Error Fragment 【发布时间】:2018-06-22 04:08:18 【问题描述】:

主活动:

    public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener 

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    FloatingActionButton fab2 = (FloatingActionButton) findViewById(R.id.developer);
    fab2.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 
            CFAlertDialog.Builder builder = new CFAlertDialog.Builder( MainActivity.this)
                    .setDialogStyle(CFAlertDialog.CFAlertStyle.ALERT)
                    .setTitle("Developer Info")
                    .setMessage ( "This Application is Developed By :\nPriyansh MIshra\nB.Tech CSE \nAUMP Gwalior" )
                    .addButton( "OK", -1, -1, CFAlertDialog.CFAlertActionStyle.POSITIVE,
                            CFAlertDialog.CFAlertActionAlignment.CENTER, ( dialog, which) -> 
                                dialog.dismiss();
                            );

            builder.show();

        
    );



    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.info);
    fab.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 
            CFAlertDialog.Builder builder = new CFAlertDialog.Builder( MainActivity.this)
                    .setDialogStyle(CFAlertDialog.CFAlertStyle.ALERT)
                    .setTitle("Info\n"+"App v1.0\n")
                    .setMessage ( "Amity YouthFest \nPresents Chroma 2K18" )
                    .addButton( "OK", -1, -1, CFAlertDialog.CFAlertActionStyle.POSITIVE,
                            CFAlertDialog.CFAlertActionAlignment.CENTER, ( dialog, which) -> 
                                dialog.dismiss();
                            );

            builder.show();

        
    );

    FloatingActionButton fab1 = (FloatingActionButton) findViewById(R.id.contactus);
    fab1.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 
                sendEmail();
        
    );

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    displaySelectedScreen(R.id.nav_home);


private void sendEmail ( ) 

    String[] TO = "";
    String[] CC = "";
    Intent emailIntent = new Intent(Intent.ACTION_SEND);

    emailIntent.setData( Uri.parse( "mailto:"));
    emailIntent.setType("text/plain");
    emailIntent.putExtra(Intent.EXTRA_EMAIL,new String[]"priyansh.mishra19@gmail.com");
    emailIntent.putExtra(Intent.EXTRA_CC, CC);
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Groomauto:Enquiry");
    emailIntent.putExtra(Intent.EXTRA_TEXT, " ");

    try 
        startActivity(Intent.createChooser(emailIntent,"Send mail..."));
        finish();

     catch (android.content.ActivityNotFoundException ex) 
        Toast.makeText( MainActivity.this, "There is no email client installed.", Toast.LENGTH_SHORT).show();
    

@Override
public void onBackPressed() 
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) 
        drawer.closeDrawer(GravityCompat.START);
     else 
        super.onBackPressed();
    

public boolean onNavigationItemSelected(MenuItem item) 
    displaySelectedScreen(item.getItemId());
    return true;

private void displaySelectedScreen(int itemId) 

    //creating fragment object
    Fragment fragment = null;


    switch (itemId) 
        case R.id.nav_home:
            fragment = new FragmentHome ();
            break;
        case R.id.nav_about:
            fragment = new FragmentAbout ();
            break;

        case R.id.nav_events:
            fragment = new FragmentEvents ();
            break;

        case R.id.nav_gallery:
            fragment = new FragmentGallery ();
            break;

        case R.id.nav_winners:
            fragment = new FragmentWinners();
            break;

        case R.id.nav_login:
            fragment = new FragmentRegister();
            break;





    
    //replacing the fragment
    if (fragment != null) 
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(content_frame, fragment);
        ft.commit();
    

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);

XML 代码:

<ScrollView
android:layout_
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:background="@drawable/fundo"
android:layout_
xmlns:app="http://schemas.android.com/apk/res-auto">

<LinearLayout
    android:layout_

    android:id="@+id/fadingTextView1"
    android:layout_>

<com.tomer.fadingtextview.FadingTextView
    android:layout_
    android:layout_
    app:texts="@array/examples"
    app:timeout="500"
    android:padding="10dp"
    android:textSize="18sp"
    android:fontFamily="casual"
    android:textStyle="bold"
    android:textAlignment="center"
    android:textColor="@color/colorAccent"
    android:id="@+id/fadingTextView"/>

</LinearLayout>

<LinearLayout
    android:layout_
    android:layout_
    android:id="@+id/layout1"
    android:layout_below="@id/fadingTextView1"
    android:layout_margin="0dp">

    <com.daimajia.slider.library.SliderLayout
        android:id="@+id/slider"
        android:layout_
        android:layout_
        />
</LinearLayout>


    <TextView
        android:layout_
        android:layout_
        android:textSize="22sp"
        android:id="@+id/title"
        android:textStyle="bold"
        android:textAlignment="center"
        android:layout_below="@id/layout1"
        android:text="Reviews and Words Of Wisdom"
        android:textColor="@color/colorAccent"/>

  <LinearLayout
      android:layout_
      android:layout_
      android:orientation="horizontal"
      android:layout_marginTop="10dp"
      android:layout_below="@id/title"
      >

      <HorizontalScrollView
          android:layout_
          android:scrollbars="horizontal"
          android:layout_>

          <LinearLayout
              android:layout_
              android:orientation="horizontal"
              android:layout_>



            <LinearLayout
                android:layout_
                android:layout_margin="10dp"
                android:layout_>



                <android.support.v7.widget.CardView
                    android:layout_
                    app:cardCornerRadius="20dp"
                    android:layout_margin="10dp"
                    android:layout_
                    >

                    <LinearLayout
                        android:orientation="vertical"
                        android:layout_
                        android:paddingTop="10dp"
                        android:layout_>
                        <com.mikhaellopez.circularimageview.CircularImageView
                            android:layout_
                            android:layout_
                            android:src="@drawable/vc"
                            app:civ_border_color="#e6e1e1"
                            app:civ_border_
                            android:layout_marginLeft="40dp"
                            android:layout_gravity="center_horizontal"
                            app:civ_shadow="true"
                            app:civ_shadow_radius="20"
                            app:civ_shadow_color="#8BC34A"/>
                        <TextView
                            android:layout_
                            android:layout_
                           android:textAlignment="center"
                            android:textColor="#1d3bd2"
                            android:textSize="20sp"
                            android:textStyle="normal" />
                        <TextView
                            android:layout_
                            android:layout_
                            android:text="Passion and commitment to make our university on top is visible among students, dosed with sense of responsibility within."
                            android:textAlignment="center"
                            android:textColor="#000"
                            android:textSize="15sp"
                            android:textStyle="normal" />

                    </LinearLayout>
                </android.support.v7.widget.CardView>


                <android.support.v7.widget.CardView
                    android:layout_
                    app:cardCornerRadius="20dp"
                    android:layout_margin="10dp"
                    android:layout_
                    >

                    <LinearLayout
                        android:orientation="vertical"
                        android:layout_
                        android:paddingTop="10dp"

                        android:layout_>
                        <com.mikhaellopez.circularimageview.CircularImageView
                            android:layout_
                            android:layout_
                            android:src="@drawable/provc"
                            app:civ_border_color="#e6e1e1"
                            app:civ_border_
                            android:layout_marginLeft="40dp"
                            android:layout_gravity="center_horizontal"
                            app:civ_shadow="true"
                            app:civ_shadow_radius="20"
                            app:civ_shadow_color="#8BC34A"/>
                        <TextView
                            android:layout_
                            android:layout_
                            android:textAlignment="center"
                            android:textColor="#1d3bd2"
                            android:textSize="20sp"
                            android:textStyle="normal" />
                        <TextView
                            android:layout_
                            android:layout_
                            android:text="Kudos to all the wonderful efforts the students are putting into developing young talent for this great university."
                            android:textAlignment="center"
                            android:textColor="#000"
                            android:textSize="15sp"
                            android:textStyle="normal" />

                    </LinearLayout>
                </android.support.v7.widget.CardView>

                <android.support.v7.widget.CardView
                    android:layout_
                    app:cardCornerRadius="20dp"
                    android:layout_margin="10dp"
                    android:layout_
                    >

                    <LinearLayout
                        android:orientation="vertical"
                        android:layout_
                        android:paddingTop="10dp"

                        android:layout_>
                        <com.mikhaellopez.circularimageview.CircularImageView
                            android:layout_
                            android:layout_
                            android:src="@drawable/hoi"
                            app:civ_border_color="#e6e1e1"
                            app:civ_border_
                            android:layout_marginLeft="40dp"
                            android:layout_gravity="center_horizontal"
                            app:civ_shadow="true"
                            app:civ_shadow_radius="20"
                            app:civ_shadow_color="#8BC34A"/>
                        <TextView
                            android:layout_
                            android:layout_
                           android:textAlignment="center"
                            android:textColor="#1d3bd2"
                            android:textSize="20sp"
                            android:textStyle="normal" />
                        <TextView
                            android:layout_
                            android:layout_
                            android:text="Wonderful organization of symposium by our students. I wish them all the best for their future endeavours."
                            android:textAlignment="center"
                            android:textColor="#000"
                            android:textSize="15sp"
                            android:textStyle="normal" />

                    </LinearLayout>
                </android.support.v7.widget.CardView>

                <android.support.v7.widget.CardView
                    android:layout_
                    app:cardCornerRadius="20dp"
                    android:layout_margin="10dp"
                    android:layout_
                    >

                    <LinearLayout
                        android:orientation="vertical"
                        android:layout_
                        android:paddingTop="10dp"

                        android:layout_>
                        <com.mikhaellopez.circularimageview.CircularImageView
                            android:layout_
                            android:layout_
                            android:src="@drawable/nasirkhan"
                            app:civ_border_color="#e6e1e1"
                            app:civ_border_
                            android:layout_marginLeft="40dp"
                            android:layout_gravity="center_horizontal"
                            app:civ_shadow="true"
                            app:civ_shadow_radius="20"
                            app:civ_shadow_color="#8BC34A"/>
                        <TextView
                            android:layout_
                            android:layout_
                           android:textAlignment="center"
                            android:textColor="#1d3bd2"
                            android:textSize="20sp"
                            android:textStyle="normal" />
                        <TextView
                            android:layout_
                            android:layout_

                            android:textAlignment="center"
                            android:textColor="#000"
                            android:textSize="15sp"
                            android:textStyle="normal" />

                    </LinearLayout>
                </android.support.v7.widget.CardView>

            </LinearLayout>

          </LinearLayout>

      </HorizontalScrollView>
  </LinearLayout>
 </RelativeLayout>
 </ScrollView>

片段类

 @SuppressLint("ValidFragment")
 public class FragmentHome extends android.support.v4.app.Fragment           implements BaseSliderView.OnSliderClickListener, ViewPagerEx.OnPageChangeListener

SliderLayout sliderLayout;
HashMap<String,String> Hash_file_maps ;


@Nullable
@Override
public View onCreateView (@NonNull LayoutInflater inflater,                    @Nullable      ViewGroup container, @Nullable Bundle savedInstanceState ) 
    return inflater.inflate( R.layout.fragment_home, container, false);


@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) 
    super.onViewCreated(view, savedInstanceState);

    String[] texts = "YouthFest Of The Year \n We Hold A Brand \n Let's Make A Dream Come True ",
            "Most Awaited YouthFest Chroma \n AUMP" ,
            " Grab The Opputunity \n Light Your Night \n DJ NIght";
    FadingTextView FTV = (FadingTextView)view.findViewById( R.id.fadingTextView);
    FTV.setTexts(texts);

    Hash_file_maps = new HashMap<String, String>();

    sliderLayout = (SliderLayout)view.findViewById(R.id.slider);

    Hash_file_maps.put("Android Workshop", "http://www.i3indyatechnologies.com/img/android-application-development.jpg");
    Hash_file_maps.put("Ludo", "http://amityaump.com/android%20images/ludo.jpg");
    Hash_file_maps.put("Python Workshop", "https://aeccodingclub.files.wordpress.com/2017/01/python-workshop-poster-viveks.jpg?w=1024");
    Hash_file_maps.put("Treasure Hunt", "http://amityaump.com/android%20images/treasure%20hunt.jpg");
    Hash_file_maps.put("Solo Dance", "http://amityaump.com/android%20images/solo%20dance.jpg");

    for(String name : Hash_file_maps.keySet())

        TextSliderView textSliderView = new TextSliderView(FragmentHome.this.getActivity());
        textSliderView
                .description(name)
                .image(Hash_file_maps.get(name))
                .setScaleType(BaseSliderView.ScaleType.Fit)
                .setOnSliderClickListener(this);
        textSliderView.bundle(new Bundle());
        textSliderView.getBundle()
                .putString("extra",name);
        sliderLayout.addSlider(textSliderView);
    
    sliderLayout.setPresetTransformer(SliderLayout.Transformer.Accordion);
    sliderLayout.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
    sliderLayout.setCustomAnimation(new DescriptionAnimation());
    sliderLayout.setDuration(3000);
    sliderLayout.addOnPageChangeListener(this);


@Override
public void onStop() 

    sliderLayout.stopAutoCycle();

    super.onStop();


@Override
public void onSliderClick(BaseSliderView slider) 



@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) 

@Override
public void onPageSelected(int position) 

    Log.d("Slider Demo", "Page Changed: " + position);


@Override
public void onPageScrollStateChanged(int state) 

错误:java.lang.RuntimeException:无法启动活动 组件信息com.darkweb.android.chroma2018/com.darkweb.android.chroma2018.MainActivity: android.view.InflateException:二进制 XML 文件第 0 行:错误 膨胀类 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) 在 android.app.ActivityThread.access$800(ActivityThread.java:151) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 在 android.os.Handler.dispatchMessage(Handler.java:110) 在 android.os.Looper.loop(Looper.java:193) 在 android.app.ActivityThread.main(ActivityThread.java:5292) 在 java.lang.reflect.Method.invokeNative(Native Method) 在 java.lang.reflect.Method.invoke(Method.java:515) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640) 在 dalvik.system.NativeStart.main(本机方法) 引起:android.view.InflateException:二进制 XML 文件第 0 行: 膨胀类错误 在 android.view.LayoutInflater.createView(LayoutInflater.java:620) 在 com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) 在 android.view.LayoutInflater.onCreateView(LayoutInflater.java:669) 在 android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694) 在 android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 在 android.view.LayoutInflater.inflate(LayoutInflater.java:492) 在 android.view.LayoutInflater.inflate(LayoutInflater.java:397) 在 com.darkweb.android.chroma2018.FragmentHome.onCreateView(FragmentHome.java:36)

【问题讨论】:

发布你的xml文件代码 你在哪里关闭ScrollView 它已关闭,可能不在发布的代码中! @AjayMishra 请接受我的编辑请求,我编辑您的代码。它可以帮助您解决您的问题 我没有收到您的编辑请求:/ @Munir 【参考方案1】:

我认为你的 XML 父/子搞砸了。您从 Scrollview 开始,但以线性布局结束。你应该有一个父布局并在里面做所有其他事情。我猜是看到 XLMS 两次在顶部声明你复制并粘贴了一些东西,我猜它搞砸了你的树。

【讨论】:

重新发布新的 XML? 我的意思是你的新 XML,你改变了什么 我是新来的,我可以在哪里发布新的 XML 代码? 只需编辑您的初始问题并用新的 XML 替换现有的 XML。 你真的看过你的 XML 了吗? Android Studio 给了我很多警告,比如未使用的父级和无法嵌套的元素。

以上是关于布局膨胀错误片段的主要内容,如果未能解决你的问题,请参考以下文章

片段对话框中的片段膨胀引发错误“片段未创建视图”

谷歌地图的“错误膨胀类片段”

如何从布局中获取膨胀的片段参考?

Android片段布局完成膨胀

android.view.InflateException Binary xml 文件第 11 行:错误膨胀类片段

二进制 XML 文件第 1 行:Xamarin Android 中 Google Maps 的类片段错误膨胀错误