繁体   English   中英

我希望在相对布局中有一个mapView,其中包含imageView和TextView

[英]I wish to have a mapView in my relative layout which has imageView and TextView

要获得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服务库包含在我的项目中。 Google Play服务和我的应用程序的目标是相同的。

需要帮忙!!

这是我的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;
}

}

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

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

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

要显示地图,请通过Google API创建一个项目,启用地图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();
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM