二进制 XML 文件第 13 行:膨胀类片段时出错

Posted

技术标签:

【中文标题】二进制 XML 文件第 13 行:膨胀类片段时出错【英文标题】:Binary XML file line #13 :Error inflating class fragment 【发布时间】:2015-06-25 22:34:29 【问题描述】:

谁能帮我解决这个错误? 我要做的是使用谷歌播放服务获取当前位置,并在片段内的编辑文本中显示城市名称。

MainActivity.java

    package com.example.mylocationwiki;


import java.util.List;
import java.util.Locale;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.app.Activity;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends FragmentActivity implements ConnectionCallbacks,
        OnConnectionFailedListener 

    private static final String TAG = MainActivity.class.getSimpleName();

    private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 1000;
    private Location mLastLocation;
    private GoogleApiClient mGoogleApiClient;
    private boolean mRequestingLocationUpdates = false;
    private LocationRequest mLocationRequest;

    String cityname="";



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

        if (checkPlayServices()) 
            buildGoogleApiClient();
        

        try 
            getLocation();
        
        catch (Exception e) 
            e.printStackTrace();
        
        if(cityname!=null)Log.d("pathanor thik agey ", cityname);
        new FragmentA(cityname);

    


    private void getLocation() throws Exception

        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

        if (mLastLocation != null) 
            double latitude = mLastLocation.getLatitude();
            double longitude = mLastLocation.getLongitude();

            Geocoder gc=new Geocoder(this,Locale.getDefault());
            List<Address> addresses=gc.getFromLocation(latitude, longitude, 1);
            cityname=addresses.get(0).getLocality();
            if(cityname!=null)Log.d("calculate holo ", cityname );

            new FragmentA(cityname);

         else
        
        return; 
    


    protected synchronized void buildGoogleApiClient() 
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API).build();
    


    private boolean checkPlayServices() 
        int resultCode = GooglePlayServicesUtil
                .isGooglePlayServicesAvailable(this);
        if (resultCode != ConnectionResult.SUCCESS) 
            if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) 
                GooglePlayServicesUtil.getErrorDialog(resultCode, this,
                        PLAY_SERVICES_RESOLUTION_REQUEST).show();
             else 
                Toast.makeText(getApplicationContext(),
                        "This device is not supported.", Toast.LENGTH_LONG)
                        .show();
                finish();
            
            return false;
        
        return true;
    

    @Override
    protected void onStart() 
        super.onStart();
        if (mGoogleApiClient != null) 
            mGoogleApiClient.connect();
        
    

    @Override
    protected void onResume() 
        super.onResume();
        checkPlayServices();
    


    @Override
    public void onConnectionFailed(ConnectionResult result) 
        Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = "
                + result.getErrorCode());
    

    @Override
    public void onConnected(Bundle arg0) 
        try 
            getLocation();
         catch (Exception e) 
            e.printStackTrace();
        
    

    @Override
    public void onConnectionSuspended(int arg0) 
        mGoogleApiClient.connect();
    

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:background="#00BBFF"
android:gravity="center"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.mylocationwiki.MainActivity" >

<fragment
    android:id="@+id/fragment1"
    android:name="com.example.mylocationwiki.FragmentA"
    android:layout_
    android:layout_
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" /></RelativeLayout>

FragmentA.java

    package com.example.mylocationwiki;

import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;


public class FragmentA extends Fragment implements View.OnClickListener

    String cityname="";
    Button button;
    EditText field;

    FragmentA(String cityname)
        cityname=this.cityname;
        if(cityname!=null)Log.d("received ", cityname);

    


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

    @Override
    public void onActivityCreated(Bundle savedInstanceState) 
        super.onActivityCreated(savedInstanceState);
        button=(Button) getActivity().findViewById(R.id.button1);
        field=(EditText)getActivity().findViewById(R.id.editText1);
        field.setText(cityname);
    


    @Override
    public void onClick(View v) 
        // 

    



fragment_a.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_
    android:background="#FFBB00" >

    <EditText
        android:id="@+id/editText1"
        android:layout_
        android:layout_
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_margin="20dp"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_
        android:layout_
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="37dp"
        android:text="Button" />

</RelativeLayout>

到目前为止我所做的尝试:

阅读此链接: similar question on SO 并在我的项目中进行了以下更改:

1) 包括以下导入:

    import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;

2) 确保 MainActivity 扩展了 FragmentActivity

3) 在 onActivityCreated() 方法中声明了 getActivity() 方法,很好地建议here

4) 按照建议Here

,我的清单中甚至包含以下部分
    <meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

我仍然无法解决问题。请问还有什么见解吗? 谢谢!

【问题讨论】:

@downvoter 请您说明拒绝投票的原因吗? 不知道为什么要投反对票,但如果你能把你遇到的错误告诉我们,我们可能会提供更多帮助;) @romo 嗨,这会有帮助吗? dropbox.com/s/14u7c56o1o9usaq/error.png?dl=0 【参考方案1】:

我猜原因是您的 Fragment 类中没有默认构造函数。您重载了构造函数,但 Fragment 需要一个空的。Empty Constructor for Extended Fragment 如果您需要在片段中接收参数,请使用 Bundle:

public static YourFragment getInstance(String argument)
    
        YourFragment fragment = new YourFragment();
        Bundle bundle = new Bundle();
        bundle.putString("argument", argument);
        fragment.setArguments(bundle);
        return fragment;
    

从活动中:

YourFragment fragment = YourFragment.getInstance(argument);
getFragmentManager().beginTransaction().add(R.id.container, fragment, "fragmentTag").commit();

R.id.container 必须是您的RelativeLayoutid(从中删除&lt;fragment/&gt;)。

如果您仍然想在xml 中使用&lt;fragment/&gt;,那么这里有一个建议,如何在那里传递参数:If I declare a fragment in an XML layout, how do I pass it a Bundle?

【讨论】:

以上是关于二进制 XML 文件第 13 行:膨胀类片段时出错的主要内容,如果未能解决你的问题,请参考以下文章

android.view.InflateException:二进制 XML 文件第 15 行:二进制 XML 文件第 19 行:膨胀类片段时出错

android.view.InflateException 膨胀类片段时出错,二进制 XML 文件第 1 行?

Google Map API 问题:android.view.InflateException:二进制 XML 文件第 2 行:二进制 XML 文件第 2 行:膨胀类片段时出错

二进制 XML 文件第 13 行:在扩展 VideoView 的类上膨胀类时出错

无法启动活动 ComponentInfo 二进制 XML 文件第 13 行:膨胀类 com.google.android.material.appbar.MaterialToolbar 时出错

第 7 行的 InflateException 二进制 XML 文件:膨胀类片段时出错