扩充类片段时出错:android.view.InflateException

时间:2018-05-18 19:45:43

标签: java android

我已经在论坛上搜索了几个小时,但我无法找到解决问题的方法。 我有一个Activity,它有一些TextView和一个MapFragment。为了设置片段我有一个单独的类。

调用MapFragment类的Activity: package homework.android.aut.bme.hu.shoppy;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.database.FirebaseDatabase;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import homework.android.aut.bme.hu.shoppy.data.Entry;
import homework.android.aut.bme.hu.shoppy.map.MapFragment;

import static homework.android.aut.bme.hu.shoppy.LoginActivity.userName;

public class EntryDetailActivity extends MainActivity {
    @BindView(R.id.detailTitle)
    TextView DetailTitle;
    @BindView(R.id.detailBody)
    TextView DetailBody;

    private Entry tmpEntry;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_entry_detail);
        ButterKnife.bind(this);

        Intent intent = getIntent();
        tmpEntry = (Entry) intent.getSerializableExtra("tmpEntry");

        DetailTitle.setText(tmpEntry.getTitle());
        DetailBody.setText(tmpEntry.getBody());
        MapFragment mapFragment = new MapFragment();
        Bundle args = new Bundle();
        args.putDouble("latitude", tmpEntry.getLatitude());
        args.putDouble("longitude", tmpEntry.getLongitude());

        ViewGroup view = (ViewGroup) findViewById(R.id.activity_write_post);
        mapFragment.onCreateView(LayoutInflater.from(getApplicationContext()),view,args);

    }

    @OnClick(R.id.btnDelete)
    void deleteList() {
        FirebaseDatabase.getInstance().getReference().child(userName).child(tmpEntry.getId()).setValue(null).addOnCompleteListener(new OnCompleteListener<Void>() {

            @Override
            public void onComplete(@NonNull Task<Void> task) {
                Toast.makeText(EntryDetailActivity.this, "Entry deleted", Toast.LENGTH_SHORT).show();

                finish();
            }
        });
    }

}

以下是此活动的布局

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_write_post"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin">

        <TextView
            android:id="@+id/detailTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dp"
            android:textSize="20sp"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/detailBody"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dp"/>

        <fragment
            android:id="@+id/map"
            class="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="400dp"
            android:layout_height="200dp"
            android:tag="map"/>

        <Button
            android:id="@+id/btnDelete"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/delete_this_list" />

    </LinearLayout>
</ScrollView>

MapFragment类:

package homework.android.aut.bme.hu.shoppy.map;


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

import com.google.android.gms.maps.CameraUpdate;
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.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import homework.android.aut.bme.hu.shoppy.R;


public class MapFragment extends android.support.v4.app.Fragment implements OnMapReadyCallback
{
    GoogleMap googleMap;
    private double latitude;
    private double longitude;
    private Marker marker;

    public MapFragment() {
        this.latitude = 0;
        this.longitude = 0;
    }


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

        View v = inflater.inflate(R.layout.activity_entry_detail, container, false);

        this.latitude = savedInstanceState.getDouble("latitude");
        this.longitude = savedInstanceState.getDouble("longitude");

        SupportMapFragment mapFrag = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
        mapFrag.getMapAsync(this);

        return v;
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        this.googleMap = googleMap;
        setUpMap();
    }

    private void setUpMap() {
        LatLng position = new LatLng(latitude, longitude);
        MarkerOptions markerOptions = new MarkerOptions().
                position(position).
                title("Location of the shop");

        marker = googleMap.addMarker(markerOptions);

        CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(position, 15);
        googleMap.animateCamera(cameraUpdate);
    }
}

我收到此错误:

    05-18 21:43:49.781 10290-10290/homework.android.aut.bme.hu.shoppy E/AndroidRuntime: FATAL EXCEPTION: main
    Process: homework.android.aut.bme.hu.shoppy, PID: 10290
    java.lang.RuntimeException: Unable to start activity ComponentInfo{homework.android.aut.bme.hu.shoppy/homework.android.aut.bme.hu.shoppy.EntryDetailActivity}: android.view.InflateException: Binary XML file line #33: Binary XML file line #33: Error inflating class fragment
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2681)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2742)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1489)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6171)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781)
     Caused by: android.view.InflateException: Binary XML file line #33: Binary XML file line #33: Error inflating class fragment
     Caused by: android.view.InflateException: Binary XML file line #33: Error inflating class fragment
     Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.fragment" on path: DexPathList[[zip file "/data/app/homework.android.aut.bme.hu.shoppy-1/base.apk", zip file "/data/app/homework.android.aut.bme.hu.shoppy-1/split_lib_dependencies_apk.apk", zip file "/data/app/homework.android.aut.bme.hu.shoppy-1/split_lib_slice_0_apk.apk", zip file "/data/app/homework.android.aut.bme.hu.shoppy-1/split_lib_slice_1_apk.apk", zip file "/data/app/homework.android.aut.bme.hu.shoppy-1/split_lib_slice_2_apk.apk", zip file "/data/app/homework.android.aut.bme.hu.shoppy-1/split_lib_slice_3_apk.apk", zip file "/data/app/homework.android.aut.bme.hu.shoppy-1/split_lib_slice_4_apk.apk", zip file "/data/app/homework.android.aut.bme.hu.shoppy-1/split_lib_slice_5_apk.apk", zip file "/data/app/homework.android.aut.bme.hu.shoppy-1/split_lib_slice_6_apk.apk", zip file "/data/app/homework.android.aut.bme.hu.shoppy-1/split_lib_slice_7_apk.apk", zip file "/data/app/homework.android.aut.bme.hu.shoppy-1/split_lib_slice_8_apk.apk", zip file "/data/app/homework.android.aut.bme.hu.shoppy-1/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/homework.android.aut.bme.hu.shoppy-1/lib/arm, /system/lib, /vendor/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at android.view.LayoutInflater.createView(LayoutInflater.java:609)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:700)
        at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:68)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:717)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:785)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:861)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
        at homework.android.aut.bme.hu.shoppy.map.MapFragment.onCreateView(MapFragment.java:41)
        at homework.android.aut.bme.hu.shoppy.EntryDetailActivity.onCreate(EntryDetailActivity.java:51)
        at android.app.Activity.performCreate(Activity.java:6682)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2634)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2742)
05-18 21:43:49.782 10290-10290/homework.android.aut.bme.hu.shoppy E/AndroidRuntime:     at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1489)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6171)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781)

也许是因为它会膨胀一个已经开始的活动?我不知道为什么它会停在那里而且我没有发现任何类似的问题......我确信这件事对我来说有点遗漏......

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

您不需要手动调用片段生命周期方法,它应由操作系统管理,以便删除

mapFragment.onCreateView(LayoutInflater.from(getApplicationContext()),view,args);

改为使用

mapFragment = (MapFragment)getSupportFragmentManager().findFragmentById(R.id.map);