gpt4 book ai didi

java - 二进制 XML 文件行 #13 :Error inflating class fragment

转载 作者:太空宇宙 更新时间:2023-11-04 13:56:57 24 4
gpt4 key购买 nike

任何人都可以帮我解决这个错误吗?我想做的是使用 Google Play 服务获取当前位置,并在 fragment 内的编辑文本中显示城市名称。

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_width="match_parent"
android:layout_height="match_parent"
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_width="wrap_content"
android:layout_height="wrap_content"
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_width="match_parent"
android:layout_height="match_parent"
android:background="#FFBB00" >

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

<requestFocus />
</EditText>

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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)甚至以下部分也按照建议包含在我的 list 中 Here

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

我还是没能解决这个问题。请问还有更多见解吗?谢谢!

最佳答案

我猜原因是你的 Fragment 类中没有默认构造函数。您重载了构造函数,但 Fragment 需要一个空的构造函数。
Empty Constructor for Extended Fragment
如果您需要在 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;
}

从 Activity 中:

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

R.id.container必须是id您的RelativeLayout (从中删除 <fragment/>)。

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

关于java - 二进制 XML 文件行 #13 :Error inflating class fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29726914/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com