我正在尝试在我的某个活动上为Google地图片段添加标记。显示地图但没有标记。它可能与活动类型有关。源代码如下。
content_checkin_info.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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.martin.ivebeenthere.CheckinInfo"
tools:showIn="@layout/app_bar_checkin_info">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:id="@+id/imageView3"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:src="@drawable/craig"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text=""
android:id="@+id/textView7"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text=""
android:id="@+id/textView8"
android:layout_below="@+id/textView7"
android:layout_alignLeft="@+id/textView7"
android:layout_alignStart="@+id/textView7" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/textView9"
android:layout_marginTop="30dp"
android:layout_below="@+id/fragment"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<fragment
android:layout_width="wrap_content"
android:layout_height="250dp"
android:name="com.google.android.gms.maps.MapFragment"
android:id="@+id/fragment"
android:layout_below="@+id/imageView3"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="31dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
CheckinInfo.java
package com.example.martin.ivebeenthere;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
public class CheckinInfo extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private TextView txtInfo;
private TextView txtUser;
private TextView txtVisit;
private SupportMapFragment mapFragment;
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkin_info);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
new LongRunningGetIO().execute();
mapFragment = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.fragment));
if (mapFragment != null) {
mapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng mayo = new LatLng(53.9345, -9.3516);
mMap.addMarker(new MarkerOptions().position(mayo).title("Jack White - Mayo"));
}
});
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
private class LongRunningGetIO extends AsyncTask<Void, Void, String> {
protected String getASCIIContentFromEntity(HttpEntity entity) throws IllegalStateException, IOException {
InputStream in = entity.getContent();
StringBuffer out = new StringBuffer();
int n = 1;
while (n > 0) {
byte[] b = new byte[4096];
n = in.read(b);
if (n > 0) out.append(new String(b, 0, n));
}
return out.toString();
}
@Override
protected String doInBackground(Void... params) {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet("https://api.myjson.com/bins/20xew");
String text = null;
try {
HttpResponse response = httpClient.execute(httpGet, localContext);
HttpEntity entity = response.getEntity();
text = getASCIIContentFromEntity(entity);
} catch (Exception e) {
return e.getLocalizedMessage();
}
return text;
}
protected void onPostExecute(String results) {
if (results != null) {
JSONArray myJSONArray;
try {
myJSONArray = new JSONArray(results);
for (int i = 0; i < myJSONArray.length(); i++) {
JSONObject obj = (JSONObject) myJSONArray.get(i);
String info = obj.getString("Info");
String user = obj.getString("User");
String visit = obj.getString("Visit");
txtInfo = (TextView) findViewById(R.id.textView9);
txtUser = (TextView) findViewById(R.id.textView7);
txtVisit = (TextView) findViewById(R.id.textView8);
txtInfo.setText(info);
txtUser.setText(user);
txtVisit.setText(visit);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.checkin_info, menu);
return true;
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_feed) {
startActivity(new Intent(getApplicationContext(), Feed.class));
} else if (id == R.id.nav_map) {
startActivity(new Intent(getApplicationContext(), Map.class));
} else if (id == R.id.nav_account) {
startActivity(new Intent(getApplicationContext(), Account.class));
} else if (id == R.id.nav_logout) {
startActivity(new Intent(getApplicationContext(), Home.class));
Toast.makeText(getApplicationContext(), "Logged Out",
Toast.LENGTH_LONG).show();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
答案 0 :(得分:1)
因为以下代码返回null
mapFragment = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.fragment));
使用也应该SupportMapFragment
使用xml
<fragment
android:layout_width="wrap_content"
android:layout_height="250dp"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/fragment"
android:layout_below="@+id/imageView3"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="31dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />