我正在尝试在地图上使用数据lat和web服务创建一个标记,但它返回错误空指针。我确实创建了日志数据,并且在日志中成功检索了lat和long数据。请帮忙。提前谢谢。
这是我的KDetail.java
private PData data;
private static final String TAG = "KDActivity";
private Call<APIBaseResponse> call;
private RestClient.GitApiInterface service;
private Toolbar toolbar;
private SessionManager sessions;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_k_d);
toolbar = (Toolbar) findViewById(R.id.toolbar);
this.setSupportActionBar(toolbar);
sessions = new SessionManager(this);
Intent i = getIntent();
data = (PData) i.getSerializableExtra("PDItem");
txtIdP = (TextView) findViewById(R.id.txtIdP);
txtCdt = (EditText) findViewById(R.id.txtCreatedDtm);
txtIdP.setText(data.getId_P());
txtCdt.setText(data.getCreatedDtm());
final MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
Log.d(TAG, "onMapReady: ");
double lata = Double.parseDouble(data.getLatP());
double lana = Double.parseDouble(data.getLangP());
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(lata, lana))
.title("Start") );
}
});
}
这是错误:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.MapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference
at com.kd.kddupe.Activity.KDetail.onCreate(KDetail.java:115)
第115行是:
mapFragment.getMapAsync(new OnMapReadyCallback(){
这是我的acitivity_k_d.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="@color/feed_bg">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/view">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay" >
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/view" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/bg_parent_rounded_corner"
android:elevation="6pt"
android:layout_marginBottom="20dp"
android:layout_marginLeft="@dimen/feed_item_margin"
android:layout_marginRight="@dimen/feed_item_margin"
android:layout_marginTop="@dimen/feed_item_margin"
android:paddingBottom="@dimen/feed_item_padding_top_bottom"
android:paddingTop="@dimen/feed_item_padding_top_bottom" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/id_p"
android:id="@+id/txtIdP"
android:textColor="@color/colorPrimaryDark"
android:textStyle="bold"
android:textSize="18sp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="20dp"
android:textAlignment="center"/>
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="@color/colorPrimaryDark"
android:id="@+id/line1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="250dp">
<fragment android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="@color/colorPrimaryDark"
android:id="@+id/line2" />
<LinearLayout
android:orientation="horizontal"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_marginTop="20dp" >
<LinearLayout
android:layout_weight="2"
android:layout_height="fill_parent"
android:layout_width="0dp"
android:orientation="vertical"
android:paddingLeft="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/created"
android:textSize="15sp"
android:gravity="center_vertical" />
</LinearLayout>
<LinearLayout
android:layout_weight="4"
android:layout_height="fill_parent"
android:layout_width="0dp"
android:orientation="vertical"
android:paddingRight="10dp"
android:paddingLeft="5dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtCreatedDtm"
android:background="@drawable/bg_parent_rounded_corner"
android:hint="@string/yyyy_mm_dd"
android:inputType="textMultiLine"
android:minHeight="35dp"
android:textSize="15sp"
android:singleLine="false"
android:focusable="false"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
答案 0 :(得分:0)
检查可能是getSupportFragmentManager()
或检查id是map
肯定吗?试一试,而不是getFragmentManager
答案 1 :(得分:0)
在这里,您可以在XML文件中使用地图片段的支持版本和搜索正常代码的代码。所以要修复它你有两个解决方案
从两个中选择一个
1-将xml处的片段名称更改为
android:name="com.google.android.gms.maps.MapFragment"
2-将您的java代码{/ 1}更改为MapFragment