【问题标题】:InflateException: Binary XML fileInflateException:二进制 XML 文件
【发布时间】:2014-12-05 04:44:09
【问题描述】:

这是我遇到的错误

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 上更改和设置文本。

【问题讨论】:

  • 能否请您发布 dday_fragment 的完整 xml 代码?
  • 如果您使用LisstFragment 扩展片段,则无需在xml 文件中使用ListView
  • 就是完整的xml代码哈哈

标签: android fragment android-inflate


【解决方案1】:

LinearLayout 需要使用"&gt;" 标签。

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

【讨论】:

  • 在这个onCreateView的最后,我不需要返回视图吗?如果我这样做,它会说“无法将视图解析为变量”。返回视图;
【解决方案2】:

您的布局 XML 文件有问题。确保关闭打开的括号。这是您错过的内容。

<LinearLayout> </LinearLayout>

另一种方法是:

<LinearLayout />

【讨论】:

  • 我想是不小心删除了ㅠㅠ
【解决方案3】:

您忘记声明属性 end enclose。请按以下方式操作:

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

【讨论】:

  • 我认为它在我发帖时被意外删除了。
猜你喜欢
  • 2016-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-12
  • 1970-01-01
  • 2014-07-05
  • 1970-01-01
相关资源
最近更新 更多