扩展类片段时出错(第二次打开对话框时)

时间:2017-03-23 02:06:46

标签: android android-fragments

所以我在打开DialogFragment时遇到了这个错误。我第一次打开它很好但是如果我把它弄坏然后再尝试打开它我会遇到错误。在添加GoogleMap片段以及将片段导入从android.support.v4.app.DialogFragment;更改为import android.app.DialogFragment后,这种情况不会发生;

我被告知最近在这里包含更多代码(在Android S.O.中)所以我从一开始就添加了一些。如果我结束了,请告诉我。

堆栈跟踪:

   FATAL EXCEPTION: main
 Process: com.example.andrew.neighborlabour, PID: 9528
 android.view.InflateException: Binary XML file line #166: Binary XML file line #166: Error inflating class fragment
     at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
     at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
     at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
     at com.example.andrew.neighborlabour.CreateJobDialog.onCreateView(CreateJobDialog.java:77)
     at android.support.v4.app.Fragment.performCreateView(Fragment.java:2189)
     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)
     at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
     at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:757)
     at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2355)
     at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2146)
     at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2098)
     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2008)
     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:710)
     at android.os.Handler.handleCallback(Handler.java:739)
     at android.os.Handler.dispatchMessage(Handler.java:95)
     at android.os.Looper.loop(Looper.java:148)
     at android.app.ActivityThread.main(ActivityThread.java:5417)
     at java.lang.reflect.Method.invoke(Native Method)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  Caused by: android.view.InflateException: Binary XML file line #166: Error inflating class fragment
     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
     at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
     at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
     at android.view.LayoutInflater.rInflate(LayoutInflater.java:838)
     at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
     at android.view.LayoutInflater.rInflate(LayoutInflater.java:838)
     at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
     at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
     at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
     at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
     at com.example.andrew.neighborlabour.CreateJobDialog.onCreateView(CreateJobDialog.java:77) 
     at android.support.v4.app.Fragment.performCreateView(Fragment.java:2189) 
     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299) 
     at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528) 
     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595) 
     at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:757) 
     at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2355) 
     at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2146) 
     at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2098) 
     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2008) 
     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:710) 
     at android.os.Handler.handleCallback(Handler.java:739) 
     at android.os.Handler.dispatchMessage(Handler.java:95) 
     at android.os.Looper.loop(Looper.java:148) 
     at android.app.ActivityThread.main(ActivityThread.java:5417) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
  Caused by: java.lang.IllegalArgumentException: Binary XML file line #166: Duplicate id 0x7f0e00b7, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.SupportMapFragment
     at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3420)
     at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
     at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:378)
     at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:33)
     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:79)
     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:754)
     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
     at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
     at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
     at android.view.LayoutInflater.rInflate(LayoutInflater.java:838) 
     at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
     at android.view.LayoutInflater.rInflate(LayoutInflater.java:838) 
     at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
     at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
     at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
     at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
     at com.example.andrew.neighborlabour.CreateJobDialog.onCreateView(CreateJobDialog.java:77) 
     at android.support.v4.app.Fragment.performCreateView(Fragment.java:2189) 
     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299) 
     at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528) 
     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595) 
     at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:757) 
     at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2355) 
     at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2146) 
     at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2098) 
     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2008) 
     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:710) 
     at android.os.Handler.handleCallback(Handler.java:739) 
     at android.os.Handler.dispatchMessage(Handler.java:95) 
     at android.os.Looper.loop(Looper.java:148) 
     at android.app.ActivityThread.main(ActivityThread.java:5417) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

我如何调用对话框:

new CreateJobDialog().show(getSupportFragmentManager(), null);

CreateJobDialog:

import android.app.DatePickerDialog;
import android.os.Handler;
import android.support.v4.app.DialogFragment;
import android.app.FragmentTransaction;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.InflateException;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.NumberPicker;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.example.andrew.neighborlabour.Services.Utils.Conversions;
import com.example.andrew.neighborlabour.Services.Utils.SuccessCB;
import com.example.andrew.neighborlabour.Services.listings.Listing;
import com.example.andrew.neighborlabour.Services.listings.ListingManager;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import java.util.Calendar;
import java.util.GregorianCalendar;

public class CreateJobDialog extends DialogFragment implements OnMapReadyCallback{

    public final static String EXTRA_MESSAGE = "CreateJob";
    private final String TAG = "Create Job Dialog";

    final int MAX_DURATION = 4 * 8; //in 15 minute intervals
    final int MAX_COMPENSATION = 100;

    View view;

    DatePickerDialog datePicker;
    TimePickerDialog timePicker;

    TextView etTitle;
    TextView etDescription;
    TextView etAddress;
    TextView tvCompensation;
    TextView tvDuration;
    TextView tvStartDate;
    TextView tvStartTime;

    GoogleMap map;

    Button btCreate;

    Calendar calendar = Calendar.getInstance();
    int year = calendar.get(Calendar.YEAR);
    int month = calendar.get(Calendar.MONTH);
    int day = calendar.get(Calendar.DAY_OF_MONTH);
    int hour = 12;
    int minute = 0;

    int duration = 15;
    int compensation = 10;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.dialog_create_job, null);

        SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        setUpGui(view);
        initTextValues();
        setButtonListeners();

        return view;
    }

    void setUpGui(View view){
        etTitle = (TextView)  view.findViewById(R.id.etTitle);
        etDescription = (TextView)  view.findViewById(R.id.etDescription);
        etAddress = (TextView)  view.findViewById(R.id.etAddress);
        tvCompensation = (TextView)  view.findViewById(R.id.tvCompensation);
        tvDuration = (TextView)  view.findViewById(R.id.tvDuration);
        tvStartDate = (TextView)  view.findViewById(R.id.tvStartDate);
        tvStartTime = (TextView)  view.findViewById(R.id.tvStartTime);
        btCreate = (Button) view.findViewById(R.id.btCreate);
    }

    void initTextValues(){
        tvStartDate.setText( (month+1) + "/" + day + "/" + year);
        tvStartTime.setText( "12:00");
        tvCompensation.setText("$10");
        tvDuration.setText("0:15");
    }

    void setButtonListeners(){
        tvStartDate.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                //showStartDatePicker();
            }
        });
        tvDuration.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                //showDurationPicker();
            }
        });
        tvStartTime.setOnClickListener( new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                //showTimePicker();
            }
        });
        tvCompensation.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                //showCompensationPicker();
            }
        });
        btCreate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //createJob();
            }
        });
        etAddress.setOnFocusChangeListener(new View.OnFocusChangeListener(){
            @Override
            public void onFocusChange(View view, boolean b) {
                //updateMap();
            }
        });
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        map = googleMap;
        Log.e("map", "ready");
    }
}

CreateJobXML

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

    <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="wrap_content"
        android:background="@color/colorPrimary"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tvTitle"
            android:text="Create Job"
            android:textAppearance="@color/colorPrimaryDark"
            android:textSize="24dp"
            android:padding="20dp"
            android:lines="2"
            android:layout_gravity="left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </TextView>

    </LinearLayout>

    <ScrollView android:id="@+id/ScrollView01"
        android:layout_width="fill_parent"
        android:layout_height="400dp">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <EditText
                android:paddingLeft="20dp"
                android:paddingRight="20dp"
                android:paddingBottom="20dp"
                android:layout_width="300dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:hint="Job Title"
                android:id="@+id/etTitle"/>

            <EditText
                android:paddingLeft="20dp"
                android:paddingRight="20dp"
                android:paddingBottom="20dp"
                android:layout_width="300dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:hint="Address"
                android:id="@+id/etAddress"/>

            <EditText
                android:paddingLeft="20dp"
                android:paddingRight="20dp"
                android:paddingBottom="20dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Description"
                android:lines="3"
                android:id="@+id/etDescription"/>

            <LinearLayout
                android:paddingLeft="20dp"
                android:paddingRight="20dp"
                android:paddingBottom="20dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    android:layout_width = "300dp"
                    android:text="Compensation: "
                    android:layout_weight="1"
                    android:layout_height="wrap_content" />

                <TextView
                    android:id="@+id/tvCompensation"
                    android:textAlignment="textEnd"
                    android:layout_gravity="end"
                    android:layout_width="200dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1" />

            </LinearLayout>


            <LinearLayout
                android:paddingLeft="20dp"
                android:paddingRight="20dp"
                android:paddingBottom="20dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    android:layout_width = "300dp"
                    android:text="Duration: "
                    android:layout_weight="1"
                    android:layout_height="wrap_content" />

                <TextView
                    android:textAlignment="textEnd"
                    android:layout_gravity="end"
                    android:layout_width="300dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:id="@+id/tvDuration"/>

            </LinearLayout>

            <LinearLayout
                android:paddingLeft="20dp"
                android:paddingRight="20dp"
                android:paddingBottom="20dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    android:layout_width = "300dp"
                    android:text="Date: "
                    android:layout_weight="1"
                    android:layout_height="wrap_content" />

                <TextView
                    android:id="@+id/tvStartDate"
                    android:textAlignment="textEnd"
                    android:layout_gravity="end"
                    android:layout_width="200dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1" />

            </LinearLayout>

            <LinearLayout
                android:paddingLeft="20dp"
                android:paddingRight="20dp"
                android:paddingBottom="20dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    android:layout_width = "300dp"
                    android:text="Time: "
                    android:layout_weight="1"
                    android:layout_height="wrap_content" />

                <TextView
                    android:id="@+id/tvStartTime"
                    android:textAlignment="textEnd"
                    android:layout_gravity="end"
                    android:layout_width="200dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1" />

            </LinearLayout>

            <fragment xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:id="@+id/map"
                tools:context=".MapsActivity"
                android:name="com.google.android.gms.maps.SupportMapFragment" />

        </LinearLayout>

    </ScrollView>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Create Job"
        android:id="@+id/btCreate"
        android:onClick="Create"/>

</LinearLayout>

好的衡量标准:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.andrew.neighborlabour">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>


    <application
        android:name=".ParseProject"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="AIzaSyB151f1l-Rr5H3jWNYTTWlIFklHbDpGchg"/>

        <activity
            android:name=".UI.auth.LoginActivity"
            android:label="login">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".MainActivity">
        </activity>
        <activity
            android:name=".UI.jobListings.ListingDetailActivity"
            android:theme="@android:style/Theme.Dialog">
        </activity>
        <activity
            android:name=".UI.auth.RegisterActivity">
        </activity>
        <activity
            android:name=".UI.auth.ProfileActivity">
        </activity>

    </application>

</manifest>

2 个答案:

答案 0 :(得分:0)

我认为您的问题在跟踪中的此行标识:

  

引起:java.lang.IllegalArgumentException:二进制XML文件行#166:复制id 0x7f0e00b7,标记为null或父ID为0xffffffff,另一个片段为com.google.android.gms.maps.SupportMapFragment

检查您是否正在重复使用ID进行分段,并正确解雇DialogFragment。有关解除`DialogFragment'的信息,请参阅此Stack Overflow near futurequestion

答案 1 :(得分:0)

大多数情况下,您可能会在视图中夸大另一个SupportMapFragment(即在活动的xml本身中)。现在,您正在尝试使用R.id.map中具有相同ID DialogFragment的新帐户进行充气。这种情况不可能发生,因为事实证明在同一活动的视图层次结构中有两个具有相同ID的视图。

来自错误堆栈跟踪:

  

使用com.google.android.gms.maps.SupportMapFragment的另一个片段重复ID 0x7f0e00b7,标记为null或父ID为0xffffffff

更改SupportMapFragment中的CreateJobXML的ID:

<LinearLayout>
...
    <fragment   android:layout_width="match_parent"
                android:layout_height="200dp"
                android:id="@+id/map1" // another id
                tools:context=".MapsActivity"
                android:name="com.google.android.gms.maps.SupportMapFragment" />
...
</LinearLayout>