0

要获得 MapView,我必须使用 MapActivity 扩展我的类。当我尝试执行此操作时,我收到错误“创建 MapActivity 类”。

如果我不使用 MapActivity 扩展它并改用 Activity,我会得到以下异常。

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.strive.gostrive/com.strive.gostrive.EventDetailActivity}: android.view.InflateException: Binary XML file line #168: Error inflating class com.google.android.maps.MapView

我已经在 xml 文件、android 清单文件中进行了必要的更改。我还在我的项目中包含了 google play services 库。谷歌播放服务和我的应用程序的目标是相同的。

需要帮忙!!

这是我的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:background="@drawable/background"
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=".EventDetailActivity" >

<ImageView
    android:id="@+id/eventDImg"
    android:layout_width="fill_parent"
    android:layout_height="200dp"
    android:layout_alignBaseline="@+id/eventDEndDate"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/innershadow" />

<TextView
    android:id="@+id/textView2D"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/eventDDateTV"
    android:layout_alignBottom="@+id/eventDDateTV"
    android:layout_toRightOf="@+id/eventDDateTV"
    android:text="-" />

<TextView
    android:id="@+id/eventDEndDate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView2D"
    android:layout_toRightOf="@+id/textView2D"
    android:maxLength="10"
    android:text="End Date" />

<TextView
    android:id="@+id/eventDStartAge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/eventDEndDate"
    android:layout_alignLeft="@+id/textView3D"
    android:layout_marginLeft="39dp"
    android:maxLength="2"
    android:text="frm" />

<TextView
    android:id="@+id/textView4D"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/eventDEndDate"
    android:layout_toRightOf="@+id/eventDStartAge"
    android:text="-" />

<TextView
    android:id="@+id/eventDAgeTV"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/eventDEndDate"
    android:layout_toRightOf="@+id/textView4D"
    android:maxLength="2"
    android:text="to" />

<TextView
    android:id="@+id/eventDTitleTV"
    android:layout_width="180dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="48dp"
    android:maxLines="2"
    android:text="Event Title"
    android:textSize="20dp" />

<TextView
    android:id="@+id/eventDDateTV"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/eventDTitleTV"
    android:layout_centerVertical="true"
    android:maxLength="10"
    android:text="Strt Date" />

<TextView
    android:id="@+id/textView1D"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/eventDTitleTV"
    android:layout_marginLeft="180dp"
    android:layout_toLeftOf="@+id/eventDPriceTV"
    android:text="$"
    android:textSize="20dp" />

<TextView
    android:id="@+id/textView3D"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/eventDDayTV"
    android:layout_alignBaseline="@+id/eventDEndDate"
    android:layout_marginLeft="23dp"
    android:layout_toRightOf="@+id/eventDTitleTV"
    android:text="Ages:" />

<TextView
    android:id="@+id/eventDPriceTV"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView1D"
    android:layout_alignBottom="@+id/textView1D"
    android:layout_alignLeft="@+id/textView3D"
    android:text="Event Fee"
    android:textSize="20dp" />

<TextView
    android:id="@+id/eventDStrtTime"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/eventDDayTV"
    android:layout_marginTop="16dp"
    android:text="Strt Tym" />

<TextView
    android:id="@+id/eventDEndTime"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/eventDStrtTime"
    android:layout_alignLeft="@+id/eventDEndDate"
    android:layout_marginLeft="26dp"
    android:text="End tym" />

<TextView
    android:id="@+id/eventDVenueTV"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/eventDEndTime"
    android:layout_alignLeft="@+id/eventDGender"
    android:text="Venue" />

<TextView
    android:id="@+id/eventDDayTV"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/eventDImg"
    android:layout_below="@+id/eventDDateTV"
    android:layout_marginTop="31dp"
    android:text="M Tu W" />

<TextView
    android:id="@+id/eventDGender"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/eventDDayTV"
    android:layout_alignBottom="@+id/eventDDayTV"
    android:layout_toRightOf="@+id/textView1D"
    android:text="gender" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="150dp"
    android:layout_alignLeft="@+id/eventDImg"
    android:layout_alignRight="@+id/eventDImg"
    android:layout_below="@+id/eventDImg"
    android:layout_marginTop="16dp"
    android:orientation="vertical" >


    <com.google.android.maps.MapView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/mapview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:apiKey="***************************"
        android:clickable="true" />
</LinearLayout>

这里是活动

import com.google.android.gms.maps.MapView;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;


public class EventDetailActivity extends Activity{

MapView map;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_event_detail);

    Bundle bundle = getIntent().getExtras();
    int position =  bundle.getInt("position");

    map = (MapView) findViewById(R.id.mapview);

    TextView txtEventName = (TextView)findViewById(R.id.eventDTitleTV);
    TextView txtEventFee = (TextView)findViewById(R.id.eventDPriceTV); 

    TextView txtStartDate = (TextView)findViewById(R.id.eventDDateTV);
    TextView txtEndDate = (TextView)findViewById(R.id.eventDEndDate); 

    TextView txtStartAge = (TextView)findViewById(R.id.eventDStartAge);
    TextView txtEndAge = (TextView)findViewById(R.id.eventDAgeTV); 

    TextView txtDaysWeek = (TextView)findViewById(R.id.eventDDayTV); 
    TextView txtEventGender = (TextView)findViewById(R.id.eventDGender); 

    txtEventName.setText(EventModel.PREF_EVENTNAME); 
    txtEventFee.setText(EventModel.PREF_EVENTFEE);
    txtStartAge.setText(EventModel.PREF_EVENTSATRTAGE);
    txtEndAge.setText(EventModel.PREF_EVENTENDAGE);
    txtStartDate.setText(EventModel.PREF_EVENTSTARTDATE);
    txtEndDate.setText(EventModel.PREF_EVENTENDDATE);
    txtDaysWeek.setText(EventModel.PREF_EVENTDAYSWEEK);
    txtEventGender.setText(EventModel.PREF_EVENTGENDER);


}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.event_detail, menu);
    return true;
}

}
4

2 回答 2

0

您可以改用 MapFragment。这样你就不需要扩展 MapActivity。 https://developers.google.com/maps/documentation/android/start?#add_a_map

要与较低版本一起使用,您可以使用SupportMapFragment。要使用此类,您必须在构建路径中包含 Android 支持库。

于 2013-04-18T17:46:48.133 回答
0

这是一个在 RelativeLayout 内带有 MapView 的工作 Activity。

要显示地图,请在 Google APIs 上创建一个项目,启用地图 API,创建凭据,将密钥复制到您的应用程序中

src/debug/res/values/google_maps.xml
src/release/res/values/google_maps.xml

在应用程序中更新 AndroidManifest.xml

    <!--
         The API key for Google Maps-based APIs is defined as a string resource.
         (See the file "res/values/google_maps_api.xml").
         Note that the API key is linked to the encryption key used to sign the APK.
         You need a different API key for each encryption key, including the release key that is used to
         sign the APK for publishing.
         You can define the keys for the debug and release targets in src/debug/ and src/release/.
    -->
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />

<activity android:name=".MapActivity"/>

或者使用 RelativeLayout 和 TextViews 等创建活动布局。使用 findViewById 等。

package au.com.derpapps.derpapp;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.RelativeLayout;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.OnMapReadyCallback;

public class MapActivity extends Activity implements OnMapReadyCallback  {

    // Private variables

    private MapView mapView;
    private GoogleMap googleMap;

    // Activity methods

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        RelativeLayout relativeLayout = new RelativeLayout(this);
        relativeLayout.setBackgroundColor(Color.WHITE);

        mapView = new MapView(this);
        mapView.onCreate(savedInstanceState);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        layoutParams.setMargins(200, 200, 200, 200);
        mapView.setLayoutParams(layoutParams);
        relativeLayout.addView(mapView);
        setContentView(relativeLayout);

        mapView.getMapAsync(this);

    }

    public void onMapReady(GoogleMap googleMap) {
        this.googleMap = googleMap;

        mapView.onResume();
    }

}
于 2017-02-27T05:57:28.327 回答