二进制xml文件行#9错误膨胀类

时间:2014-09-27 23:02:36

标签: android android-fragments

我知道这里有很多关于这个话题的内容,但我似乎并没有找到问题的根源。问题是我在MainActivity中有一个Fragment,当我按下操作栏溢出中的设置按钮时,我想用其他片段替换它。这很好用,但是如果我再次按下设置按钮,或者再次尝试再次尝试,我的应用程序将崩溃(不幸的是MyApp已停止)所以我想知道是什么问题。非常感谢,如果你可以帮助我,或者你只是花时间阅读它,我是Android编程的新手,我想了解很多。

 09-27 17:53:16.364: W/System.err(15060): android.view.InflateException: Binary XML file line #9: Error inflating class fragment
09-27 17:53:16.364: W/System.err(15060):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)
09-27 17:53:16.364: W/System.err(15060):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
09-27 17:53:16.364: W/System.err(15060):    at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
09-27 17:53:16.364: W/System.err(15060):    at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
09-27 17:53:16.364: W/System.err(15060):    at com.example.myfirstapp.FragmentOpAdv.onCreateView(FragmentOpAdv.java:14)
09-27 17:53:16.364: W/System.err(15060):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1504)
09-27 17:53:16.364: W/System.err(15060):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
09-27 17:53:16.364: W/System.err(15060):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
09-27 17:53:16.364: W/System.err(15060):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
09-27 17:53:16.364: W/System.err(15060):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
09-27 17:53:16.364: W/System.err(15060):    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:450)
09-27 17:53:16.364: W/System.err(15060):    at android.os.Handler.handleCallback(Handler.java:733)
09-27 17:53:16.364: W/System.err(15060):    at android.os.Handler.dispatchMessage(Handler.java:95)
09-27 17:53:16.364: W/System.err(15060):    at android.os.Looper.loop(Looper.java:136)
09-27 17:53:16.364: W/System.err(15060):    at android.app.ActivityThread.main(ActivityThread.java:5579)
09-27 17:53:16.364: W/System.err(15060):    at java.lang.reflect.Method.invokeNative(Native Method)
09-27 17:53:16.364: W/System.err(15060):    at java.lang.reflect.Method.invoke(Method.java:515)
09-27 17:53:16.364: W/System.err(15060):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
09-27 17:53:16.364: W/System.err(15060):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
09-27 17:53:16.364: W/System.err(15060):    at dalvik.system.NativeStart.main(Native Method)
09-27 17:53:16.364: W/System.err(15060): Caused by: java.lang.IllegalArgumentException: Binary XML file line #9: Duplicate id 0x7f050041, tag null, or parent id 0x0 with another fragment for com.example.myfirstapp.FragmentA
09-27 17:53:16.364: W/System.err(15060):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:297)
09-27 17:53:16.364: W/System.err(15060):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
09-27 17:53:16.364: W/System.err(15060):    ... 19 more
09-27 17:53:16.364: I/System.out(15060): Binary XML file line #9: Error inflating class fragment
09-27 17:53:16.364: E/ViewRootImpl(15060): sendUserActionEvent() mView == null

这是堆栈跟踪,也可以查看我的MainActivity.class

package com.example.myfirstapp;

import android.app.AlertDialog;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;


public class MainActivity extends ActionBarActivity {

    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

    private FragmentOpAdv fragmentOpAdv;
    private FragmentA fragmentA;

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

        if(findViewById(R.id.mainact)!=null)
        {
            if (savedInstanceState!=null)
            {
                return;
            }
            fragmentA= new FragmentA();
            getSupportFragmentManager().beginTransaction().add(R.id.mainact, fragmentA).commit();
        }
    }


    @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();
        if (R.id.action_search==id)
        {
            openSearch();
            return true;
        }
        if (R.id.action_settings==id)
        {
            openSettings();
            return true;
        }
        else
        {
            return super.onOptionsItemSelected(item);
        }
    }

    /**
     * Called when the user clicks the Settings item
     */
    public void openSettings() {
        fragmentOpAdv = new FragmentOpAdv();
        getSupportFragmentManager().beginTransaction().replace(R.id.mainact, fragmentOpAdv).addToBackStack(null).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE).commit();
    }

    /**
     * Called when the user clicks the Search item
     */
    public void openSearch() {
        AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setTitle("Inserte algo");
        alert.setMessage("Oiga en serio");
        alert.show();
    }


    /** Called when the user clicks the Send button */
    public void sendMessage(View view) 
    {
        Intent intent = new Intent(this, DisplayMessageActivity.class);
        EditText editText = (EditText) findViewById(R.id.edit_message);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);
    }
}

和我的片段类

package com.example.myfirstapp;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragmentOpAdv extends Fragment{

    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
    {
        try {
            View view = inflater.inflate(R.layout.fragment_op_adv, container, false);

            return view;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
            return null;
        }

    }
}

这是主要活动的xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainact"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
android:paddingTop="?attr/actionBarSize" >

2 个答案:

答案 0 :(得分:1)

我刚刚发现的问题是,当布局包含a时,我无法将布局扩展为片段。只有动态添加到片段时才支持嵌套片段。

link

答案 1 :(得分:0)

  

如果所有内容与谷歌代码相同,请在我的情况下检查清单文件,我添加了地理位置密钥和地图密钥,这就是发生异常的原因,

注意 - 请勿在清单文件中添加两个键删除地图密钥

meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="@string/google_maps_key"/>

上面的代码并添加此代码。

 <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/auto_location"/>

 <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version"/>