【问题标题】:Unable to inflate fragment无法膨胀片段
【发布时间】:2015-11-12 05:18:03
【问题描述】:

我是 Android Studio 的新手,我正在尝试构建一个包含一个活动和一个片段的简单界面。但是当我运行应用程序时,它说“错误膨胀类片段”这是我的代码:

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
           xmlns:android="http://schemas.android.com/apk/res/android"
           xmlns:app="http://schemas.android.com/apk/res-auto"
           xmlns:tools="http://schemas.android.com/tools"             
           android:layout_width="match_parent"
           android:layout_height="match_parent"              
           android:fitsSystemWindows="true"
           tools:context=".MainActivity">

     <include layout="@layout/content_main" />
</android.support.design.widget.CoordinatorLayout>

MainActivity.java:

 package com.example.zhuol.sunshine;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

content_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main" tools:context=".MainActivity"
android:orientation="vertical">



<fragment
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:name="com.example.zhuol.sunshine.Fragment_Item"
    android:id="@+id/fragment"
    android:layout_gravity="center_horizontal|bottom" />
</FrameLayout>

fragment_fragment_item.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" tools:context="com.example.zhuol.sunshine.Fragment_Item">

    <!-- TODO: Update blank fragment layout -->
    <TextView android:layout_width="match_parent"                        
    android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>

主要错误:

 11-12 00:47:42.179 10787-10787/com.example.zhuol.sunshine E/AndroidRuntime:         
 FATAL EXCEPTION: main
 11-12 00:47:42.179 10787-10787/com.example.zhuol.sunshine E/AndroidRuntime:           
 Process: com.example.zhuol.sunshine, PID: 10787
 11-12 00:47:42.179 10787-10787/com.example.zhuol.sunshine E/AndroidRuntime:      
 java.lang.RuntimeException: Unable to start activity   
     ComponentInfo{com.example.zhuol.sunshine/com.example.zhuol.sunshine.MainActivity}: android.view.InflateException: Binary XML file line #17: Error inflating class fragment

 E/AndroidRuntime:  Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class fragment
  E/AndroidRuntime:  Caused by: java.lang.ClassCastException: com.example.zhuol.sunshine.MainActivity@101ed69b must implement OnFragmentInteractionListener

【问题讨论】:

    标签: java android xml android-fragments android-studio


    【解决方案1】:

    这是问题的根源:

    com.example.zhuol.sunshine.MainActivity@101ed69b must implement OnFragmentInteractionListener
    

    你应该在你的MainActivity 中实现OnFragmentInteractionListener

    因此,如果您的 com.example.zhuol.sunshine.Fragment_Item 使用 onFragmentInteractionHomeopenHome 方法定义 OnFragmentInteractionListener 接口

    public class Fragment_Item extends Fragment {
    
        private OnFragmentInteractionListener mListener;
    
        .
        .
    
    
        public interface OnFragmentInteractionListener {
            public void onFragmentInteractionHome(Uri uri);
            public void openHome(View view);
        }
    
    }
    

    那你应该在MainActivity中实现OnFragmentInteractionListener,实现onFragmentInteractionHomeopenHome方法

    public class MainActivity extends Activity implements HomeFragment.OnFragmentInteractionListener {
    
        .
        .
        .
    
        @Override
        public void openHome(View view) {
            System.out.println("Success");
        }
    
        @Override
        public void onFragmentInteractionHome(Uri uri) {
            Toast.makeText(this, "Success", Toast.LENGTH_SHORT).show();
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-26
      • 2023-03-05
      • 2014-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多