InflateException:二进制XML文件

时间:2014-12-05 04:44:10

标签: android fragment android-inflate

这是我有的错误

12-05 13:26:18.102: E/AndroidRuntime(10101): FATAL EXCEPTION: main
12-05 13:26:18.102: E/AndroidRuntime(10101): Process: com.example.project_1_4, PID: 10101
12-05 13:26:18.102: E/AndroidRuntime(10101): java.lang.RuntimeException: Unable to start activity         
ComponentInfo{com.example.project_1_4/          
com.example.project_1_4.MainActivity}: android.view.InflateException: Binary XML file line #14:     
Error inflating class fragment

我的xml文件 - 我想我在发帖时错过了括号。现在我纠正了。

<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="match_parent"
            android:layout_height="410dp" >
        </ListView>
</LinearLayout>

源代码

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.zip.Inflater;

import android.app.ListFragment;
import android.content.Context;
import android.os.Bundle;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class Dday_fragment extends ListFragment{

    ArrayList<Detail> detailList = new ArrayList<Detail>();
    ArrayList<String> detailName = new ArrayList<String>();
    DetailOperation DetailDBOperation;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = new View(getActivity());
        try{
        view = inflater.inflate(R.layout.dday_fragment, container, false);
        }catch (InflateException e){
        }

        DetailDBOperation = new DetailOperation(null);
        try {
            DetailDBOperation.open();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        if(DetailDBOperation.getAllDetail()!=null){
        detailList = DetailDBOperation.getAllDetail();
        }else{
            detailList = null;
        }

        int size = detailList.size();

        for(int i = 0; i < size; i++){
            detailName.add(detailList.get(i).getSubjectName());
        }
        ArrayAdapter<String> adapter = 
                new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, detailName);
        setListAdapter(adapter);
        return view;
    }
    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
        Detail_fragment dtf = (Detail_fragment) getFragmentManager().findFragmentById(R.layout.detail_fragment);
        dtf.change(position);
        getListView().setSelector(android.R.color.holo_red_dark);
    }

}

这是Dday_fragment.java的完整代码 如果我不使用ListFragment,我如何使用onListItemClick? 我想要的是显示dday_fragment中的项目列表, 还有另一个xml随用户选择的项目ID而变化。 另一个xml将显示项目的详细信息。 所以我使用onListItemClick来改变和设置另一个xml上的文本。

3 个答案:

答案 0 :(得分:0)

您需要为">"使用LinearLayout标记。

<LinearLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical">

另一件事是您在onCreateView()方法中夸大了视图,并且您还尝试创建View的实例两次以便存在问题。不需要这样做。所以只需改变

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {

  try{

     View  view = inflater.inflate(R.layout.dday_fragment, container, false);

  } catch (InflateException e){
  }

  DetailDBOperation = new DetailOperation(getActivity());
  try {
    DetailDBOperation.open();
  } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }

答案 1 :(得分:0)

您的布局XML文件存在问题。确保关闭打开的支架。这是你错过的。

<LinearLayout> </LinearLayout>

替代方法是:

<LinearLayout />

答案 2 :(得分:0)

您忘记声明属性结束。请在下面填写:

<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="match_parent"
            android:layout_height="410dp" >
        </ListView>
</LinearLayout>