我进行了搜索和搜索,但没有任何东西能让我朝着正确的方向前进。
这里是MainActivity:
package com.example.google.playservices.placecomplete;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Intent Showframe = new Intent(this, FrameLayout.class);
public void ShowIt(View view) {
startActivity(Showframe);
}
}
这里是FrameLocationActivity:
package com.example.google.playservices.placecomplete;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.location.places.AutocompletePrediction;
import com.google.android.gms.location.places.PlaceBuffer;
import com.google.android.gms.location.places.Places;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
public class FrameLayout extends FragmentActivity implements GoogleApiClient.OnConnectionFailedListener{
/**
* GoogleApiClient wraps our service connection to Google Play Services and provides access
* to the user's sign in state as well as the Google's APIs.
*/
protected GoogleApiClient mGoogleApiClient;
private PlaceAutocompleteAdapter mAdapter;
private DelayAutoCompleteTextView mAutocompleteView;
private static final LatLngBounds BOUNDS_GREATER_SYDNEY = new LatLngBounds(
new LatLng(-34.041458, 150.790100), new LatLng(-33.682247, 151.383362));
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.frame_location);
// Construct a GoogleApiClient for the {@link Places#GEO_DATA_API} using AutoManage
// functionality, which automatically sets up the API client to handle Activity lifecycle
// events. If your activity does not extend FragmentActivity, make sure to call connect()
// and disconnect() explicitly.
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, 0 , this)
.addApi(Places.GEO_DATA_API)
.build();
// Retrieve the AutoCompleteTextView that will display Place suggestions.
mAutocompleteView = (DelayAutoCompleteTextView)findViewById(R.id.autoComplete);
mAutocompleteView.setThreshold(3);
// Register a listener that receives callbacks when a suggestion has been selected
mAutocompleteView.setOnItemClickListener(mAutocompleteClickListener);
// Set up the adapter that will retrieve suggestions from the Places Geo Data API that cover
// the entire world.
mAdapter = new PlaceAutocompleteAdapter(this, mGoogleApiClient, BOUNDS_GREATER_SYDNEY,
null);
//Set Adapter
mAutocompleteView.setAdapter(mAdapter);
//Set LoadingIndicator
mAutocompleteView.setLoadingIndicator((android.widget.ProgressBar)findViewById(R.id.pb_loading_indicator));
// Set up the 'clear text' button that clears the text in the autocomplete view
Button clearButton = (Button)findViewById(R.id.button_clear);
clearButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mAutocompleteView.setText("");
}
});
}
/**
* Listener that handles selections from suggestions from the AutoCompleteTextView that
* displays Place suggestions.
* Gets the place id of the selected item and issues a request to the Places Geo Data API
* to retrieve more details about the place.
*
* @see com.google.android.gms.location.places.GeoDataApi#getPlaceById(com.google.android.gms.common.api.GoogleApiClient,
* String...)
*/
private AdapterView.OnItemClickListener mAutocompleteClickListener
= new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Retrieve the place ID of the selected item from the Adapter.
//The adapter stores each Place suggestion in a AutocompletePrediction from which we
//read the place ID and title.
final AutocompletePrediction item = mAdapter.getItem(position);
final String placeId = item.getPlaceId();
final CharSequence primaryText = item.getPrimaryText(null);
//Issue a request to the Places Geo Data API to retrieve a Place object with additional
//details about the place.
PendingResult<PlaceBuffer> placeResult = Places.GeoDataApi
.getPlaceById(mGoogleApiClient, placeId);
Toast.makeText(getBaseContext(), "Clicked: " + primaryText,Toast.LENGTH_SHORT).show();
}
};
/**
* Called when the Activity could not connect to Google Play services and the auto manager
* could resolve the error automatically.
* In this case the API is not available and notify the user.
*
* @param connectionResult can be inspected to determine the cause of the failure
*/
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
// TODO(Developer): Check error code and notify the user of error state and resolution.
Toast.makeText(getBaseContext(),"Could not connect to Google API Client: Error " + connectionResult.getErrorCode(),Toast.LENGTH_SHORT).show();
}
}
清单文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.google.playservices.placecomplete"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19"/>
<application
android:allowBackup="true"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="api_key"/>
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".FrameLayout" android:label="@string/app_name"/>
</application>
日志文件:
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: FATAL EXCEPTION: main
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: Process: com.example.google.playservices.placecomplete, PID: 9065
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: java.lang.IllegalStateException: Could not execute method of the activity
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at android.view.View$1.onClick(View.java:3969)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at android.view.View.performClick(View.java:4633)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:19330)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:733)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at android.os.Looper.loop(Looper.java:157)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5356)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: Caused by: java.lang.reflect.InvocationTargetException
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at android.view.View$1.onClick(View.java:3964)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at android.view.View.performClick(View.java:4633)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:19330)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:733)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at android.os.Looper.loop(Looper.java:157)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5356)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {/com.example.google.playservices.placecomplete.FrameLayout}; have you declared this activity in your AndroidManifest.xml?
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1648)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at android.app.Instrumentation.execStartActivity(Instrumentation.java:1442)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at android.app.Activity.startActivityForResult(Activity.java:3511)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at android.app.Activity.startActivityForResult(Activity.java:3472)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at android.app.Activity.startActivity(Activity.java:3714)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at android.app.Activity.startActivity(Activity.java:3682)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at com.example.google.playservices.placecomplete.MainActivity.ShowIt(MainActivity.java:37)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at android.view.View$1.onClick(View.java:3964)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at android.view.View.performClick(View.java:4633)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:19330)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:733)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at android.os.Looper.loop(Looper.java:157)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5356)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
11-17 22:16:54.250 9065-9065/com.example.google.playservices.placecomplete E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
我没有错误所在的clou ......: - (
先感谢您的耐心等待。
答案 0 :(得分:0)
我找到了答案,因为Fragment需要活动存在,所以无法启动Fragment或FragmentActivity。只需使用fragmentmanager并替换Containerview中的Fragment即可。