我试图在谷歌地图活动的底部显示列表视图(仅在点击按钮时),但它不起作用,只显示应用停止消息。如何在Google地图活动中显示listview?
这是我的java和xml代码
package com.example.dong_gyo.project;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import java.util.ArrayList;
public class MapFind extends ActionBarActivity {
GoogleMap mMap; // Might be null if Google Play services APK is not available.
LocationManager lm;
String locationProvider;
Location location;
Button mapListbut;
ListView mapList;
ArrayList list;
ButtonListener listener;
ArrayAdapter mapadapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_find);
setUpMapIfNeeded();
mapListbut = (Button)findViewById(R.id.showMapList);
mapadapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);
mapList = (ListView)findViewById(R.id.mapList);
mapList.setAdapter(mapadapter);
mapList.setVisibility(View.INVISIBLE);
mapListbut.setOnClickListener(listener);
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationProvider = lm.getBestProvider(new Criteria(), true);
location = lm.getLastKnownLocation(locationProvider);
double latitude = location.getLatitude();
double longitude = location.getLongitude();
final LatLng LOC = new LatLng(latitude, longitude);
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(LOC, 16));
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap(LOC);
}
}
}
private void setUpMap(LatLng LOC) {
mMap.addMarker(new MarkerOptions().position(LOC));
}
class ButtonListener implements View.OnClickListener {
@Override
public void onClick(View v) { // 목록 토글 버튼 리스너
// TODO Auto-generated method stub
switch (v.getId()) { //
case R.id.showMapList:
if (!mapList.isShown()) {
mapList.setVisibility(View.VISIBLE);
mapadapter.notifyDataSetChanged();
mapListbut.setSelected(true);
} else {
mapList.setVisibility(View.INVISIBLE);
mapListbut.setSelected(false);
}
break;
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
startActivity(new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<fragment 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:id="@+id/map"
tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
<Button
android:id="@+id/showMapList"
android:text = "목록보기"
android:layout_width="100dp"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp"
android:layout_marginLeft="20dp"/>
<ListView
android:id="@+id/mapList"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#eeeeee" >
</ListView>
</RelativeLayout>
答案 0 :(得分:0)
我运行了你的代码,它说它得到一个空指针。查看代码,location = lm.getLastKnownLocation(locationProvider);
使location
为空。因此,请尝试:
getBestProvider
location = lm.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
我试过了,地图加载了。