二进制XML文件第11行:膨胀类片段时出错 [英] Binary XML file line #11: Error inflating class fragment

查看:70
本文介绍了二进制XML文件第11行:膨胀类片段时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码中出现两个错误.

原因:java.lang.IllegalStateException:片段com.example.dfoley.write_to_file.topFragment没有创建视图.

由以下原因引起:android.view.InflateException:二进制XML文件第11行:错误夸大类片段,它们都指向Line MainActivity.java:21,它是以下setContentView(R.layout.activity_main);

bottomFragment

package com.example.dfoley.write_to_file;
import android.app.ListFragment;
import android.os.Bundle;
import android.widget.ArrayAdapter;

import java.util.ArrayList;

public class bottomFragment extends ListFragment {
    private ArrayAdapter<StateUser> adapter;

    @Override
    public void onActivityCreated(Bundle saveInstanceState){
        ArrayList<StateUser> flight = MainContoller.getInstance().getFlights();
        this.adapter = new ArrayAdapter<StateUser>(getActivity(), android.R.layout.simple_list_item_1, flight);
        setListAdapter(this.adapter);
        super.onActivityCreated(saveInstanceState);
    }
    public void refreshList(){
        this.adapter.notifyDataSetChanged();
    }
}

顶部片段

package com.example.dfoley.write_to_file;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import.android.util.Log;
import android.view.View;
import.android.widget.Button;
import android.widget.EditText;

import java.io.IOException; 
import java.io.OutputStreamWriter;



public class topFragment extends Fragment{
    private FlightSearcher searcher;
    EditText text1;

    public interface FlightSearcher {
        public void refreshFlightList();
    }

    @Override
    public void onAttach(Activity activity) {
        searcher = (FlightSearcher) activity;
        super.onAttach(activity);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState){
        setupListeners();
        super.onActivityCreated(savedInstanceState);
    }

    public void setupListeners() {
        Button addUser = (Button)getActivity().findViewById(R.id.button);
        addUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                writeToFile();
                searcher.refreshFlightList();
            }
        });
    }

    private void writeToFile() {
        text1=(EditText)getActivity().findViewById(R.id.editText);
        String AddUsers = text1.getText().toString();
        try {
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(getActivity().openFileOutput("UserList", Context.MODE_PRIVATE));
            outputStreamWriter.write(AddUsers);
            outputStreamWriter.close();
        }
        catch (IOException e) {
            Log.e("Exception", "File write failed: " + e.toString());
        }
    }
}

主要活动

package com.example.dfoley.write_to_file;

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

public class MainActivity extends FragmentActivity implements topFragment.FlightSearcher{

    public void refreshFlightList() {
        FragmentManager mgr = getFragmentManager();
        bottomFragment bottomFragmentRef =(bottomFragment) mgr.findFragmentById(R.id.bottom_fragment);
        bottomFragmentRef.refreshList();
    }

    @Override
    public 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);
    }
}

activiy_main.xml

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

<fragment
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:name="com.example.dfoley.write_to_file.topFragment"
    android:id="@+id/top_fragment"
    android:layout_weight="1"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    tools:layout="@layout/topfragment" />

<fragment
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:name="com.example.dfoley.write_to_file.bottomFragment"
    android:id="@+id/bottom_fragment"
    android:layout_weight="1"
    android:layout_below="@+id/top_fragment"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    tools:layout="@layout/bottomfragment" />

解决方案

对于您的两个片段,您都没有告诉它如何创建视图.我看到您正在使用tools:layout标签,但是根据Tools doc,这只是对设计人员的提示;它实际上并不会膨胀该布局:

此属性通常在标签中设置,用于记录您希望在设计时看到的布局(在运行时,这将由标签列出的片段类的操作确定)."

因此,您需要覆盖onCreateView,扩大视图层次结构,然后返回该值:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.topfragment, container, false);
}

I am getting two Errors in my Code.

Caused by: java.lang.IllegalStateException: Fragment com.example.dfoley.write_to_file.topFragment did not create a view.

Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment both pointing to Line MainActivity.java:21 which is the following setContentView(R.layout.activity_main);

bottomFragment

package com.example.dfoley.write_to_file;
import android.app.ListFragment;
import android.os.Bundle;
import android.widget.ArrayAdapter;

import java.util.ArrayList;

public class bottomFragment extends ListFragment {
    private ArrayAdapter<StateUser> adapter;

    @Override
    public void onActivityCreated(Bundle saveInstanceState){
        ArrayList<StateUser> flight = MainContoller.getInstance().getFlights();
        this.adapter = new ArrayAdapter<StateUser>(getActivity(), android.R.layout.simple_list_item_1, flight);
        setListAdapter(this.adapter);
        super.onActivityCreated(saveInstanceState);
    }
    public void refreshList(){
        this.adapter.notifyDataSetChanged();
    }
}

Top Fragment

package com.example.dfoley.write_to_file;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import.android.util.Log;
import android.view.View;
import.android.widget.Button;
import android.widget.EditText;

import java.io.IOException; 
import java.io.OutputStreamWriter;



public class topFragment extends Fragment{
    private FlightSearcher searcher;
    EditText text1;

    public interface FlightSearcher {
        public void refreshFlightList();
    }

    @Override
    public void onAttach(Activity activity) {
        searcher = (FlightSearcher) activity;
        super.onAttach(activity);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState){
        setupListeners();
        super.onActivityCreated(savedInstanceState);
    }

    public void setupListeners() {
        Button addUser = (Button)getActivity().findViewById(R.id.button);
        addUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                writeToFile();
                searcher.refreshFlightList();
            }
        });
    }

    private void writeToFile() {
        text1=(EditText)getActivity().findViewById(R.id.editText);
        String AddUsers = text1.getText().toString();
        try {
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(getActivity().openFileOutput("UserList", Context.MODE_PRIVATE));
            outputStreamWriter.write(AddUsers);
            outputStreamWriter.close();
        }
        catch (IOException e) {
            Log.e("Exception", "File write failed: " + e.toString());
        }
    }
}

Main Activity

package com.example.dfoley.write_to_file;

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

public class MainActivity extends FragmentActivity implements topFragment.FlightSearcher{

    public void refreshFlightList() {
        FragmentManager mgr = getFragmentManager();
        bottomFragment bottomFragmentRef =(bottomFragment) mgr.findFragmentById(R.id.bottom_fragment);
        bottomFragmentRef.refreshList();
    }

    @Override
    public 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);
    }
}

activiy_main.xml

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

<fragment
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:name="com.example.dfoley.write_to_file.topFragment"
    android:id="@+id/top_fragment"
    android:layout_weight="1"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    tools:layout="@layout/topfragment" />

<fragment
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:name="com.example.dfoley.write_to_file.bottomFragment"
    android:id="@+id/bottom_fragment"
    android:layout_weight="1"
    android:layout_below="@+id/top_fragment"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    tools:layout="@layout/bottomfragment" />

解决方案

For both of your fragments, you are not telling it how to create a view. I see that you are using the tools:layout tag, but according to the Tools doc, that is only a hint to the designer; it does not actually inflate that layout:

"This attribute is typically set in a tag and is used to record which layout you want to see rendered at designtime (at runtime, this will be determined by the actions of the fragment class listed by the tag)."

Thus you need to override onCreateView, inflate your view hierarchy, and then return that:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.topfragment, container, false);
}

这篇关于二进制XML文件第11行:膨胀类片段时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆