gpt4 book ai didi

java - map 应用程序在模拟器上强制退出

转载 作者:行者123 更新时间:2023-12-02 05:33:47 25 4
gpt4 key购买 nike

我无法理解此代码有什么问题,以及为什么会引发此异常,我已经安装了最新的 google play services rev 19

list 文件:

<?xml version="1.0" encoding="utf-8"?>
manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testing"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />

<permission
android:name="com.example.testing.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.example.testing.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />

<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.testing.MainActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="my app key" />
</application>

主要 Activity 代码:

public class MainActivity extends FragmentActivity implements OnClickListener, GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener, LocationListener {

private static final long UPDATE_INTERVAL = 5000;
private static final long FASTEST_INTERVAL = 1000;

private Button showBtn;
private GoogleMap map = null;
private LocationClient locationClient;
private Location myLocation;
private LocationRequest locationRequest;
private Display display;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showBtn = (Button) findViewById(R.id.showBtn);
showBtn.setOnClickListener(this);
showBtn.setClickable(false);

map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);

display = new Display(map);

locationClient = new LocationClient(this, this, this);
locationRequest = LocationRequest.create().setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY).setInterval(UPDATE_INTERVAL)
.setFastestInterval(FASTEST_INTERVAL).setSmallestDisplacement(10);

if (locationClient != null)
locationClient.connect();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public void onLocationChanged(Location location) {
Toast.makeText(this, "Location listener called", Toast.LENGTH_SHORT).show();
LatLng newPosition = new LatLng(location.getLatitude(), location.getLongitude());

myLocation = location;
if (myLocation != null)
showBtn.setClickable(true);
else
showBtn.setClickable(false);

display.clearMap();
map.animateCamera(CameraUpdateFactory.newLatLngZoom(newPosition, 16));
display.addMarker(newPosition, "My Title", "..and my comments go here");
display.setCircleOnMap(newPosition);
}

@Override
public void onConnectionFailed(ConnectionResult arg0) {
}

@Override
public void onConnected(Bundle arg0) {
Toast.makeText(this, "Locationclient Connected", Toast.LENGTH_SHORT).show();
locationClient.requestLocationUpdates(locationRequest, this);
}

@Override
public void onDisconnected() {
Toast.makeText(this, "Disconnected. Please re-connect.", Toast.LENGTH_SHORT).show();
}

@Override
public void onClick(View v) {
if (v.getId() == R.id.showBtn) {
if (InternetStatus.getInstance(this).isOnline(this)) {
new ShowCloseUps(display, myLocation).execute();
} else
Toast.makeText(this, "No internet connection find one and retry!!", Toast.LENGTH_LONG).show();
}
}

@Override
protected void onDestroy() {
if (locationClient.isConnected() && locationClient != null) {
locationClient.removeLocationUpdates(this);
locationClient.disconnect();
}
super.onDestroy();
}
}

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: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=".MainActivity" >

<Button
android:id="@+id/showBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="@string/show" />

<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/showBtn" />

<fragment>
</fragment>

</RelativeLayout>

我的 logcat 在抛出异常时:

       08-11 09:10:31.170: E/AndroidRuntime(1688): FATAL EXCEPTION: main
08-11 09:10:31.170: E/AndroidRuntime(1688): Process: com.example.testing, PID: 1688
08-11 09:10:31.170: E/AndroidRuntime(1688): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testing/com.example.testing.MainActivity}: android.view.InflateException: Binary XML file line #25: Error inflating class fragment
08-11 09:10:31.170: E/AndroidRuntime(1688): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
08-11 09:10:31.170: E/AndroidRuntime(1688): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
08-11 09:10:31.170: E/AndroidRuntime(1688): at android.app.ActivityThread.access$800(ActivityThread.java:135)
08-11 09:10:31.170: E/AndroidRuntime(1688): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-11 09:10:31.170: E/AndroidRuntime(1688): at android.os.Handler.dispatchMessage(Handler.java:102)
08-11 09:10:31.170: E/AndroidRuntime(1688): at android.os.Looper.loop(Looper.java:136)
08-11 09:10:31.170: E/AndroidRuntime(1688): at android.app.ActivityThread.main(ActivityThread.java:5017)
08-11 09:10:31.170: E/AndroidRuntime(1688): at java.lang.reflect.Method.invokeNative(Native Method)
08-11 09:10:31.170: E/AndroidRuntime(1688): at java.lang.reflect.Method.invoke(Method.java:515)
08-11 09:10:31.170: E/AndroidRuntime(1688): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-11 09:10:31.170: E/AndroidRuntime(1688): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-11 09:10:31.170: E/AndroidRuntime(1688): at dalvik.system.NativeStart.main(Native Method)
08-11 09:10:31.170: E/AndroidRuntime(1688): Caused by: android.view.InflateException: Binary XML file line #25: Error inflating class fragment
08-11 09:10:31.170: E/AndroidRuntime(1688): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
08-11 09:10:31.170: E/AndroidRuntime(1688): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
08-11 09:10:31.170: E/AndroidRuntime(1688): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
08-11 09:10:31.170: E/AndroidRuntime(1688): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
08-11 09:10:31.170: E/AndroidRuntime(1688): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
08-11 09:10:31.170: E/AndroidRuntime(1688): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
08-11 09:10:31.170: E/AndroidRuntime(1688): at android.app.Activity.setContentView(Activity.java:1929)
08-11 09:10:31.170: E/AndroidRuntime(1688): at com.example.testing.MainActivity.onCreate(MainActivity.java:39)
08-11 09:10:31.170: E/AndroidRuntime(1688): at android.app.Activity.performCreate(Activity.java:5231)
08-11 09:10:31.170: E/AndroidRuntime(1688): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-11 09:10:31.170: E/AndroidRuntime(1688): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
08-11 09:10:31.170: E/AndroidRuntime(1688): ... 11 more
08-11 09:10:31.170: E/AndroidRuntime(1688): Caused by: java.lang.IllegalArgumentException: Binary XML file line #25: Must specify unique android:id, android:tag, or have a parent with an id for null
08-11 09:10:31.170: E/AndroidRuntime(1688): at android.app.Activity.onCreateView(Activity.java:4759)
08-11 09:10:31.170: E/AndroidRuntime(1688): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
08-11 09:10:31.170: E/AndroidRuntime(1688): ... 21 more

最佳答案

  <fragment>
</fragment>

为什么你的 xml 中有这个空 fragment ?

<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/showBtn" />

您已在 XML 中添加了 MapFragment,并且正在膨胀 SupportMapFragment。

使用这个。

 <fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />

java代码

map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

关于java - map 应用程序在模拟器上强制退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25244379/

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