gpt4 book ai didi

java - 当我在手机上运行此代码时强制关闭

转载 作者:行者123 更新时间:2023-12-01 12:00:28 25 4
gpt4 key购买 nike

尝试学习如何使用 Google map API,并且我正在学习 YouTube 上的教程。当我运行给定代码时,应用程序将关闭并显示按摩“...已停止响应”。请帮忙。

下面是代码:

import com.google.android.gms.maps.*;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import android.app.Activity;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends FragmentActivity {

private GoogleMap mainMap;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpMap(); //line 27

}

private void setUpMap() {

if(mainMap == null) {
mainMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); //line 34
if(mainMap != null) {
startMap();
}
}

}

private void startMap() {

mainMap.setMyLocationEnabled(true);
mainMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria(); //retrieve provider
String provider = locationManager.getBestProvider(criteria, true); //name of best provider
Location myLocation = locationManager.getLastKnownLocation(provider);

double myLat = myLocation.getLatitude();
double myLong = myLocation.getLongitude();

LatLng latLng = new LatLng(myLat, myLong);

mainMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mainMap.animateCamera(CameraUpdateFactory.zoomTo(20));
mainMap.addMarker(new MarkerOptions().position(new LatLng(myLat, myLong)).title("You are here"));

}




@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);

}

}

这是日志猫:

01-18 12:16:20.915: E/AndroidRuntime(5163): FATAL EXCEPTION: main
01-18 12:16:20.915: E/AndroidRuntime(5163): Process: com.example.map, PID: 5163
01-18 12:16:20.915: E/AndroidRuntime(5163): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.map/com.example.map.MainActivity}: java.lang.NullPointerException
01-18 12:16:20.915: E/AndroidRuntime(5163): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205)
01-18 12:16:20.915: E/AndroidRuntime(5163): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2255)
01-18 12:16:20.915: E/AndroidRuntime(5163): at android.app.ActivityThread.access$800(ActivityThread.java:142)
01-18 12:16:20.915: E/AndroidRuntime(5163): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1203)
01-18 12:16:20.915: E/AndroidRuntime(5163): at android.os.Handler.dispatchMessage(Handler.java:102)
01-18 12:16:20.915: E/AndroidRuntime(5163): at android.os.Looper.loop(Looper.java:136)
01-18 12:16:20.915: E/AndroidRuntime(5163): at android.app.ActivityThread.main(ActivityThread.java:5118)
01-18 12:16:20.915: E/AndroidRuntime(5163): at java.lang.reflect.Method.invokeNative(Native Method)
01-18 12:16:20.915: E/AndroidRuntime(5163): at java.lang.reflect.Method.invoke(Method.java:515)
01-18 12:16:20.915: E/AndroidRuntime(5163): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
01-18 12:16:20.915: E/AndroidRuntime(5163): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)
01-18 12:16:20.915: E/AndroidRuntime(5163): at dalvik.system.NativeStart.main(Native Method)
01-18 12:16:20.915: E/AndroidRuntime(5163): Caused by: java.lang.NullPointerException
01-18 12:16:20.915: E/AndroidRuntime(5163): at com.example.map.MainActivity.setUpMap(MainActivity.java:34)
01-18 12:16:20.915: E/AndroidRuntime(5163): at com.example.map.MainActivity.onCreate(MainActivity.java:27)
01-18 12:16:20.915: E/AndroidRuntime(5163): at android.app.Activity.performCreate(Activity.java:5275)
01-18 12:16:20.915: E/AndroidRuntime(5163): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-18 12:16:20.915: E/AndroidRuntime(5163): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
01-18 12:16:20.915: E/AndroidRuntime(5163): ... 11 more

这是layout.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<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_marginBottom="5dp" />

</LinearLayout>

最佳答案

或者:

  • getSupportFragmentManager() 返回 null,这不太可能,或者

  • findFragmentById() 返回 null

如果 res/layout/activity_main.xml 文件中没有名为 R.id.map 的 fragment ,则可能会出现后者。

更新

您的布局正在使用com.google.android.gms.maps.MapFragment。这不适用于 FragmentActivity。如果您要使用 FragmentActivity 和 fragment 反向移植,请使用 com.google.android.gms.maps.SupportMapFragment

关于java - 当我在手机上运行此代码时强制关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28012543/

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