android.view.inflate exception.Binary XML文件行#11.Error膨胀类片段

时间:2015-07-29 10:48:44

标签: android

我是Android的初学者。我试图将列表片段膨胀到我的活动中,并且我得到了“android.view.inflate exception.Binary XML文件行#11.Error膨胀类片段。我附上下面的代码..

片段类

package com.example.fragmentrough; 

import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MenuFragment extends ListFragment {
String[] AndroidOS = new String[] {       "Cupcake","Donut","Eclair","Froyo","Gingerbread","Honeycomb","Ice Cream   SandWich","Jelly Bean","KitKat" };

@Override

public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
    View view =inflater.inflate(R.layout.list_fragment, container, true);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_list_item_1, AndroidOS);
    setListAdapter(adapter);

    return view;

}

   @Override
      public void onListItemClick(ListView l, View v, int position, long id)      {
Toast.makeText(getActivity(), ""+position, 4000).show();
getListView().setSelector(android.R.color.holo_blue_dark);
}
}

主要活动

package com.example.fragmentrough;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends FragmentActivity {

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

    FragmentManager manager=getFragmentManager();
    FragmentTransaction transaction=manager.beginTransaction();
    MenuFragment fragment=new  MenuFragment();
    transaction.add(R.id.fragment1,fragment);
    transaction.commit();

}

}

activity_main.xml中

  <LinearLayout 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"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context="com.example.fragmentrough.MainActivity" >

  <fragment
    android:id="@+id/fragment1"
    class=com.example.fragmentrough.MenuFragment
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="158dp" />

    </LinearLayout>

**

list_fragment.xml

**

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

  <ListView
    android:id="@android:id/list"
    android:layout_width="233dp"
    android:layout_height="wrap_content" >
   </ListView>

   </LinearLayout>

1 个答案:

答案 0 :(得分:0)

试试这个

<fragment
android:id="@+id/fragment1"
android:name="com.example.fragmentrough.MenuFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="158dp" />