二进制XML文件行#19:错误膨胀类片段

时间:2016-07-19 05:20:36

标签: android android-fragments

a)创建新的NAVIGATION DRAWER ACTIVITY项目create project b)我创建了一个片段 enter image description here c)然后我去了layout-> containt main.xml&将其更新如下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.papa.raviyadav.learn2.MainActivity"
    tools:showIn="@layout/app_bar_main">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
    <fragment
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:name="layout.BlankFragment"
        android:id="@+id/newid"/>
</RelativeLayout>

d)以下是项目结构 enter image description here e)现在,当我构建并运行项目时,我在下面的行

处得到以下错误
Caused by: java.lang.RuntimeException: com.papa.raviyadav.learn2.MainActivity@f7dd2fb must implement OnFragmentInteractionListener                                                                                   at layout.BlankFragment.onAttach(BlankFragment.java:84)

f)下面是相应的代码

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
       throw new RuntimeException(context.toString()
               + " must implement OnFragmentInteractionListener");

g)以下是完整错误

07-19 10:15:35.761 14204-14204/com.papa.raviyadav.learn2 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.papa.raviyadav.learn2, PID: 14204
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.papa.raviyadav.learn2/com.papa.raviyadav.learn2.MainActivity}: android.view.InflateException: Binary XML file line #19: Error inflating class fragment                                                                           
    at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)                                                                           
    at     android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)                                                                           
    at     android.app.ActivityThread.access$800(ActivityThread.java:155)                                                                           
    at     android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)                                                                           
    at     android.os.Handler.dispatchMessage(Handler.java:102)                                                                           
    at     android.os.Looper.loop(Looper.java:135)                                                                           
    at     android.app.ActivityThread.main(ActivityThread.java:5343)                                                                           
    at     java.lang.reflect.Method.invoke(Native Method)                                                                           
    at     java.lang.reflect.Method.invoke(Method.java:372)                                                                           
    at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)                                                                           
    at     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)                                                                            
Caused by: android.view.InflateException: Binary XML file line #19: Error inflating class fragment                                                                           
    at     android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)                                                                           
    at     android.view.LayoutInflater.rInflate(LayoutInflater.java:806)                                                                           
    at     android.view.LayoutInflater.parseInclude(LayoutInflater.java:916)                                                                           
    at     android.view.LayoutInflater.rInflate(LayoutInflater.java:802)                                                                           
    at     android.view.LayoutInflater.parseInclude(LayoutInflater.java:916)                                                                           
    at     android.view.LayoutInflater.rInflate(LayoutInflater.java:802)                                                                           
    at     android.view.LayoutInflater.inflate(LayoutInflater.java:504)                                                                           
    at     android.view.LayoutInflater.inflate(LayoutInflater.java:414)                                                                           
    at     android.view.LayoutInflater.inflate(LayoutInflater.java:365)                                                                           
    at     android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)                                                                           
    at     android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)                                                                           
    at     com.papa.raviyadav.learn2.MainActivity.onCreate(MainActivity.java:22)                                                                           
    at     android.app.Activity.performCreate(Activity.java:6010)                                                                           
    at     android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)                                                                           
    at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)                                                                           
    at     android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)                                                                            
    at     android.app.ActivityThread.access$800(ActivityThread.java:155)                                                                            
    at     android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)                                                                            
    at     android.os.Handler.dispatchMessage(Handler.java:102)                                                                            
    at     android.os.Looper.loop(Looper.java:135)                                                                            
    at     android.app.ActivityThread.main(ActivityThread.java:5343)                                                                            
    at     java.lang.reflect.Method.invoke(Native Method)                                                                            
    at     java.lang.reflect.Method.invoke(Method.java:372)                                                                            
    at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)                                                                           
    at     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)                                                                             
Caused by: java.lang.RuntimeException: com.papa.raviyadav.learn2.MainActivity@f7dd2fb must implement OnFragmentInteractionListener                                                                           
    at     layout.BlankFragment.onAttach(BlankFragment.java:84)                                                                           
    at     android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1039)                                                                           
    at     android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1264)                                                                           
    at     android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1366)                                                                           
    at     android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2409)                                                                           
    at     android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)                                                                           
    at     android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:374)                                                                           
    at     android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:33)                                                                           
    at     android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:75)                                                                           
    at     android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)                                                                           
    at     android.view.LayoutInflater.rInflate(LayoutInflater.java:806)                                                                           
    at     android.view.LayoutInflater.parseInclude(LayoutInflater.java:916)                                                                           
    at     android.view.LayoutInflater.rInflate(LayoutInflater.java:802)                                                                            
    at     android.view.LayoutInflater.parseInclude(LayoutInflater.java:916)                                                                            
    at     android.view.LayoutInflater.rInflate(LayoutInflater.java:802)                                                                            
    at     android.view.LayoutInflater.inflate(LayoutInflater.java:504)                                                                           
    at     android.view.LayoutInflater.inflate(LayoutInflater.java:414)                                                                           
    at     android.view.LayoutInflater.inflate(LayoutInflater.java:365)                                                                            
    at     android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)                                                                            
    at     android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)                                                                            
    at     com.papa.raviyadav.learn2.MainActivity.onCreate(MainActivity.java:22)                                                                            
    at     android.app.Activity.performCreate(Activity.java:6010)                                                                            
    at     android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)                                                                            
    at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)                                                                            
    at     android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)                                                                           
    at     android.app.ActivityThread.access$800(ActivityThread.java:155)                                                                            
    at     android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)                                                                            
    at     android.os.Handler.dispatchMessage(Handler.java:102)                                                                            
    at     android.os.Looper.loop(Looper.java:135)                                                                            
    at     android.app.ActivityThread.main(ActivityThread.java:5343)                                                                            
    at     java.lang.reflect.Method.invoke(Native Method)                                                                            
    at     java.lang.reflect.Method.invoke(Method.java:372)                                                                     
    at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)                                                                           
    at     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) 

MainActivity.Java

import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import com.papa.raviyadav.learn3.BlankFragment;

public class MainActivity extends AppCompatActivity
            implements NavigationView.OnNavigationItemSelectedListener, BlankFragment.OnFragmentInteractionListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    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);
}
@Override
public void onFragmentInteraction(Uri uri){

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.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);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_camera) {
        // Handle the camera action
    } else if (id == R.id.nav_gallery) {

    } else if (id == R.id.nav_slideshow) {

    } else if (id == R.id.nav_manage) {

    } else if (id == R.id.nav_share) {

    } else if (id == R.id.nav_send) {

    }

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

}

BlankFragment.Java

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link BlankFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link BlankFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class BlankFragment extends Fragment {

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;

private OnFragmentInteractionListener mListener;

public BlankFragment() {
    // Required empty public constructor
}

/**
 * Use this factory method to create a new instance of
 * this fragment using the provided parameters.
 *
 * @param param1 Parameter 1.
 * @param param2 Parameter 2.
 * @return A new instance of fragment BlankFragment.
 */
// TODO: Rename and change types and number of parameters
public static BlankFragment newInstance(String param1, String param2) {
    BlankFragment fragment = new BlankFragment();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_blank, container, false);
}

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

/**
 * This interface must be implemented by activities that contain this
 * fragment to allow an interaction in this fragment to be communicated
 * to the activity and potentially other fragments contained in that
 * activity.
 * <p/>
 * See the Android Training lesson <a href=
 * "http://developer.android.com/training/basics/fragments/communicating.html"
 * >Communicating with Other Fragments</a> for more information.
 */
public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(Uri uri);
}

}

3 个答案:

答案 0 :(得分:2)

在要附加片段的活动中添加implements BlankFragment.OnFragmentInteractionListener

示例:

 public class MainActivity extends Activity implements BlankFragment.OnFragmentInteractionListener{

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

     @Override
       public void OnFragmentInteraction(Uri uri) {
        Toast.makeText(this, "Success", Toast.LENGTH_SHORT).show();
        }

    }

编辑:用于附加片段

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.papa.raviyadav.learn2.MainActivity"
tools:showIn="@layout/app_bar_main">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!" />
<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:name="layout.BlankFragment"
    android:id="@+id/newid"/>
</RelativeLayout>

在您的主要活动中:

 public class MainActivity extends Activity implements BlankFragment.OnFragmentInteractionListener{

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

    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();

    BlankFragment fragment= new BlankFragment ();
    ft.replace(R.id.newid, fragment);
    ft.commit();
  }

 @Override
   public void OnFragmentInteraction(Uri uri) {
    Toast.makeText(this, "Success", Toast.LENGTH_SHORT).show();
    }

}

答案 1 :(得分:1)

有错误说

 must implement OnFragmentInteractionListener         

做并重试

答案 2 :(得分:0)

在活动中使用frament时必须实施OnFragmentInteractionListener

尝试覆盖:public void onFragmentInteraction(Uri uri)