任何人都可以帮我解决这个错误吗? 我想要做的是使用谷歌播放服务获取当前位置,并在片段内的编辑文本中显示城市名称。
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)即使以下部分也按照建议Here
包含在我的清单中 <meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
我仍未能解决问题。还有更多的见解吗? 谢谢!
答案 0 :(得分:0)
我想原因是你的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
必须是id
的{{1}}(从RelativeLayout
删除)。
如果您仍想在<fragment/>
中使用<fragment/>
,那么这里有一个建议,如何在那里传递参数:If I declare a fragment in an XML layout, how do I pass it a Bundle?