无法对片段

时间:2015-11-12 05:18:04

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

我是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

1 个答案:

答案 0 :(得分:1)

以下是问题的根源:

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

您应该在OnFragmentInteractionListener

中实施MainActivity

因此,如果您的com.example.zhuol.sunshine.Fragment_Item使用OnFragmentInteractionListener定义了onFragmentInteractionHome界面,openHome方法

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

}