当前位置: 首页 > 面试题库 >

Google Maps Fragment无法加载

蒯宏达
2023-03-14
问题内容

我想加载一个Google Maps对象并创建一些监听器:

我创建了此类:

import java.lang.ref.WeakReference;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMarkerDragListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewParent;
import android.widget.ImageView;
import android.widget.Toast;

public class itemSaleActivity extends FragmentActivity  {
    private static Context app;
    private static GoogleMap map;
    static final LatLng HAMBURG = new LatLng(53.558, 9.927);
    private static final int LOAD_COORD = 0;
    private ImageView pic;
    private LocationHandler mHandler;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        // Set View to register.xml
        setContentView(R.layout.itemsale);

        app = getApplicationContext();
        Bundle extras = getIntent().getExtras();
        byte[] byteArray = extras.getByteArray("picture");
        Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

        pic = (ImageView) findViewById(R.id.itemImage);
        pic.setImageBitmap(bmp);

        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        //Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG).title("Hamburg"));
        mHandler = new LocationHandler(this);

        OnMarkerDragListener markerDragListener = new OnMarkerDragListener() {

            @Override
            public void onMarkerDragStart(Marker marker) {
                // Called when the marker drag is started
            }

            @Override
            public void onMarkerDragEnd(Marker marker) {
                // Called when the marker is dropped down.
                double[] coords = null;
                coords[0] = marker.getPosition().latitude;
                coords[1] = marker.getPosition().longitude;
                RestoreUIwithSavedLocation(coords);
                Toast.makeText(getApplicationContext(),"Pin Dropped at: " + coords[0] + ", " + coords[1]+marker.getTitle() , Toast.LENGTH_LONG).show();
            }

            @Override
            public void onMarkerDrag(Marker marker) {

            }
        };

        map.setOnMarkerDragListener(markerDragListener);

        View titleView = getWindow().findViewById(android.R.id.title);
        if (titleView != null) {
            ViewParent parent = titleView.getParent();
            if (parent != null && (parent instanceof View)) {
             View parentView = (View)parent;
             parentView.setVisibility(View.GONE);
            }
        }

        // You can also assign the title programmatically by passing a
        // CharSequence or resource id.
    }

    private void RestoreUIwithSavedLocation(double[] coordsArray) {
        Message.obtain(mHandler, LOAD_COORD, coordsArray).sendToTarget();
    }

    static class LocationHandler extends Handler {
        WeakReference<Activity> mActivity;

        public LocationHandler(Activity activity) {
            mActivity = new WeakReference<Activity>(activity);
        }

        public void handleMessage(Message msg) {
            Activity contextActivity = mActivity.get();
            switch ((int)msg.what) {

            case LOAD_COORD:
                map.clear();
                double[] coordArray = (double[])msg.obj;
                Marker marker = map.addMarker(new MarkerOptions().position(new LatLng(coordArray[0], coordArray[1])));
                map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(coordArray[0], coordArray[1]), 18));
                map.animateCamera(CameraUpdateFactory.zoomTo(18), 2000, null);
                marker.setDraggable(true);
                String s = Double.toString(coordArray[0]) + ", " + Double.toString(coordArray[1]);
                Toast.makeText(app,"in the case"+s , Toast.LENGTH_LONG).show();
                break;
            }
        }
    }
}

这是XML:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/itemImage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:src="@drawable/logo" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="400dp"
        android:layout_marginTop="10dp"
        android:layout_weight="66496.79"
        android:orientation="horizontal" >

        <fragment
            android:id="@+id/map"
            class="com.google.android.gms.maps.MapFragment"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginRight="150dp" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="170dp"
            android:orientation="vertical" >

            <Button
            android:id="@+id/messageBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:text="fffff" />

            <Button
                android:id="@+id/call"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentRight="true"
                android:text="ffff" />

        <Button
            android:id="@+id/sms"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@+id/button1"
            android:layout_centerVertical="true"
            android:text="ffff" />
        <Button
            android:id="@+id/email"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@+id/button1"
            android:layout_centerVertical="true"
            android:text="fffff" />

        <Button
            android:id="@+id/navigate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/button1"
            android:layout_alignParentEnd="false"
            android:layout_alignParentStart="false"
            android:layout_below="@+id/button1"
            android:layout_marginTop="14dp"
            android:text="ffffffffff" />
        </LinearLayout>
    </RelativeLayout>
</LinearLayout>

当我在手机(银河s2)上调试它时,它显示了一个异常:

03-29 15:24:29.750: E/AndroidRuntime(2579): FATAL EXCEPTION: main
03-29 15:24:29.750: E/AndroidRuntime(2579): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sal/com.example.sal.itemSaleActivity}: android.view.InflateException: Binary XML file line #26: Error inflating class fragment
03-29 15:24:29.750: E/AndroidRuntime(2579):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2079)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at android.app.ActivityThread.access$600(ActivityThread.java:132)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1157)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at android.os.Looper.loop(Looper.java:137)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at android.app.ActivityThread.main(ActivityThread.java:4575)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at java.lang.reflect.Method.invokeNative(Native Method)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at java.lang.reflect.Method.invoke(Method.java:511)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at dalvik.system.NativeStart.main(Native Method)
03-29 15:24:29.750: E/AndroidRuntime(2579): Caused by: android.view.InflateException: Binary XML file line #26: Error inflating class fragment
03-29 15:24:29.750: E/AndroidRuntime(2579):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at android.app.Activity.setContentView(Activity.java:1835)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at com.example.sal.itemSaleActivity.onCreate(itemSaleActivity.java:45)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at android.app.Activity.performCreate(Activity.java:4465)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2033)
03-29 15:24:29.750: E/AndroidRuntime(2579):     ... 11 more
03-29 15:24:29.750: E/AndroidRuntime(2579): Caused by: java.lang.ClassCastException: com.google.android.gms.maps.MapFragment cannot be cast to android.support.v4.app.Fragment
03-29 15:24:29.750: E/AndroidRuntime(2579):     at android.support.v4.app.Fragment.instantiate(Fragment.java:388)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at android.support.v4.app.Fragment.instantiate(Fragment.java:363)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:264)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:669)
03-29 15:24:29.750: E/AndroidRuntime(2579):     ... 22 more

xml文件中的第26行是:

  <fragment
                android:id="@+id/map"
                class="com.google.android.gms.maps.MapFragment"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginRight="150dp" />

问题答案:

在XML文件中,使用class="com.google.android.gms.maps.SupportMapFragment"

在您的主类中,使用:

map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

代替:

map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();


 类似资料:
  • 这是密码。没有警告,文件正常。

  • 问题内容: 当我尝试运行Hibernate时,我在控制台中收到以下消息。 但是hibernate状态很好。我的项目有什么问题吗?我使用Eclipse(Helios),Hibernate 3.6.8。我使用这些jar文件:antlr-2.7.6,commons- collections-3.1,dom4j-1.6.1,hibernate3,hibernate- jpa-2.0-api-1.0.1.F

  • 我正在运行RSTudio版本1.1.442和r3.5。当我尝试加载rJava时,我得到以下错误; 库(rJava)错误:“r Java”的包或命名空间加载失败:“r Java”的.OnLoad在loadNamespace()中失败,详细信息:调用:dyn.load(文件,DLLpath=DLLpath,...)错误:无法加载共享对象“/library/framework/r.framework/v

  • 应用程序正在启动。获取数据。请稍候...线程“main”java.lang.UnsatisfiedLinkError中出现(约10s)异常:无法加载SWT库。原因:java.library.path中没有swt-cocoa-4236。path中没有swt-cocoa不能加载库:C:\users\andrew.swt\lib\win32\x86_64\swt-cocoa.dll不能加载库:C:\us

  • 我将dll路径存储在设置文件中。当我输出 如果我在project中添加(dll)作为外部类文件夹,并编写:,它可以在这台机器中工作,但是当我使用可运行的jar时,它找不到,所以我想给出dll的绝对路径。但对我没用。 我也试过: C:\users\argenit\desktop\aaa\dll\ergopointer_64_bit dll\ergopointer_64_bit.dll ergopoi

  • 在Eclipse中运行应用程序时,出现以下异常: java.lang.无法加载库'kernel32':本地库(linux-x86-64/libkernel32.so)在资源路径中找不到(/opt/eclipse/plugins/org.eclipse.equinox.launcher_1.3.0.v20140415-2008.jar) 我该如何解决这个问题?

  • 我试图从groovy脚本调用Oracle存储过程,结果出现了标题中的错误。 以下是我的剧本: 当我删除(它们是OUT parameters)参数时,脚本将被编译,所以我认为它们有问题······ 我找到了一个让我的程序工作的解决方案: 但我想有一个更好的方法来传递这些SQL参数... PS:我正在使用Groovy-2.4.13,并尝试了Oracle JDK9和1.8.0_151

  • 我正试图用PDFBox和Groovy来搞乱一下。我试图使用PDType1Font类的getStringWidth()方法计算字符串的宽度。 我的问题是,每次编译和运行代码时,都会出现这样一个错误: 代码如下: 为了清晰起见,我删除了代码中所有不相关的部分。 问题出在哪里? 更新:我在IntelliJ中搜索了一下如何将外部jar添加到我的类路径,发现我需要转到项目结构- 我试着这样做: 但我仍然收到