2015-11-12 2 views
0

Я новичок в 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: [email protected] must implement OnFragmentInteractionListener 

ответ

1

Вот источник вашей проблемы:

[email protected] 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); 
    } 

} 

Тогда вы должны реализовать OnFragmentInteractionListener в MainActivity и реализации onFragmentInteractionHome, openHome Методы

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

}