谷歌地图v2.0不会工作和应用程序崩溃

时间:2013-12-10 06:51:47

标签: android google-maps crash

我正在制作地图应用。它在SDK 16上为我工作,但问题是我需要在sdk 8上扩展它并且我的应用程序崩溃。这是我对sdk 16 here

的完整代码

我将support-library-v4添加为项目的externar JAR。我还添加了google-play-service-lib作为库。但没有任何作用

我将Activity更改为FragmentActivity 并且还使用了

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

而不是

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

我遇到了很多问题,但最终在sdk 16上解决了这个问题。我需要在sdk 8中使用它,但应用程序崩溃并且不起作用。

请帮助我。

这是我的清单:

         <permission
        android:name="com.ariagostar.maptest.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />


    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="8" />



    <uses-permission android:name="com.ariagostar.maptest.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <!-- Required to show current location -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <!-- Required OpenGL ES 2.0. for Maps V2 -->
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/mahdi_theme"
        android:name=".G"
        >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" 
            >

        </activity>

        <activity android:name=".SplashActivity">



        </activity>
        <activity android:name=".LocationsActivity"
            android:screenOrientation="portrait"
            >


        </activity>
        <activity android:name=".AIDSActivity"
            android:screenOrientation="portrait"
           ></activity>
        <activity android:name=".AboutActivity"></activity>
        <activity android:name=".CureActivity"
            android:screenOrientation="portrait"
           ></activity>
        <activity android:name=".EllatActivity"
            android:screenOrientation="portrait"
            ></activity>
        <activity android:name=".InfectedActivity"
            android:screenOrientation="portrait"
            ></activity>
        <activity android:name=".QuestionActivity"
            android:screenOrientation="portrait"
            >




        </activity>

        <activity android:name=".SeirActivity"
            android:screenOrientation="portrait"></activity>
        <activity android:name=".PreventionActivity"
            android:screenOrientation="portrait"></activity>
        <activity android:name=".TashdidActivity"
            android:screenOrientation="portrait"></activity>
        <activity android:name=".AboutUsActivity"
            android:screenOrientation="portrait"></activity>
        <activity android:name=".OtherProductActivity"
            android:screenOrientation="portrait"></activity>
        <activity android:name=".MapActivity"
           android:label="@string/title_activity_main">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>



      <uses-library android:name="com.google.android.maps" />

         <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="my actual key" />

         <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

    </application>

这是我的地图活动:

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

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.CameraPosition;
import com.google.android.gms.maps.model.LatLng;


public class MapActivity extends FragmentActivity {



         private GoogleMap googleMap;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.map_form);
            try {
                Log.i("LOG", "map test 1");
                // Loading map
                initilizeMap();
                Log.i("LOG", "map test 2");

            } catch (Exception e) {
                 Log.e("LOG", "I got an error", e);
                e.printStackTrace();
            }

            Button b1 = (Button) findViewById(R.id.button1);

            b1.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    double latitude =20.002 ;
                    double longitude = 3.399;

                    // create marker
                    //MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps ");

                    // adding marker
                    //googleMap.addMarker(marker);

                    CameraPosition cameraPosition = new CameraPosition.Builder().target(
                            new LatLng(37.385044, 78.486671)).zoom(12).build();

                    googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

                }
            });
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }

        private void initilizeMap() {
            if (googleMap == null) {

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

                //googleMap =   ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
                //googleMap =   ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
                googleMap =   ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();


                // check if map is created successfully or not
                if (googleMap == null) {
                    Toast.makeText(getApplicationContext(),
                            "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                            .show();
                }
            }
        }

        @Override
        protected void onResume() {
            initilizeMap();
            super.onResume();
        }


}

这是我的map_form

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="Button" />

</RelativeLayout>

logcat的:

12-13 11:09:51.957: E/LOG(4706): I got an error
12-13 11:09:51.957: E/LOG(4706): java.lang.NullPointerException
12-13 11:09:51.957: E/LOG(4706):    at com.ariagostar.hiv.MapActivity.initilizeMap(MapActivity.java:76)
12-13 11:09:51.957: E/LOG(4706):    at com.ariagostar.hiv.MapActivity.onCreate(MapActivity.java:32)
12-13 11:09:51.957: E/LOG(4706):    at android.app.Activity.performCreate(Activity.java:5372)
12-13 11:09:51.957: E/LOG(4706):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
12-13 11:09:51.957: E/LOG(4706):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
12-13 11:09:51.957: E/LOG(4706):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359)
12-13 11:09:51.957: E/LOG(4706):    at android.app.ActivityThread.access$700(ActivityThread.java:165)
12-13 11:09:51.957: E/LOG(4706):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
12-13 11:09:51.957: E/LOG(4706):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-13 11:09:51.957: E/LOG(4706):    at android.os.Looper.loop(Looper.java:137)
12-13 11:09:51.957: E/LOG(4706):    at android.app.ActivityThread.main(ActivityThread.java:5455)
12-13 11:09:51.957: E/LOG(4706):    at java.lang.reflect.Method.invokeNative(Native Method)
12-13 11:09:51.957: E/LOG(4706):    at java.lang.reflect.Method.invoke(Method.java:525)
12-13 11:09:51.957: E/LOG(4706):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
12-13 11:09:51.957: E/LOG(4706):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
12-13 11:09:51.957: E/LOG(4706):    at dalvik.system.NativeStart.main(Native Method)
12-13 11:09:51.957: E/AndroidRuntime(4706): FATAL EXCEPTION: main
12-13 11:09:51.957: E/AndroidRuntime(4706): java.lang.RuntimeException: Unable to resume activity {com.ariagostar.hiv/com.ariagostar.hiv.MapActivity}: java.lang.NullPointerException
12-13 11:09:51.957: E/AndroidRuntime(4706):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2929)
12-13 11:09:51.957: E/AndroidRuntime(4706):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2958)
12-13 11:09:51.957: E/AndroidRuntime(4706):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2364)
12-13 11:09:51.957: E/AndroidRuntime(4706):     at android.app.ActivityThread.access$700(ActivityThread.java:165)
12-13 11:09:51.957: E/AndroidRuntime(4706):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
12-13 11:09:51.957: E/AndroidRuntime(4706):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-13 11:09:51.957: E/AndroidRuntime(4706):     at android.os.Looper.loop(Looper.java:137)
12-13 11:09:51.957: E/AndroidRuntime(4706):     at android.app.ActivityThread.main(ActivityThread.java:5455)
12-13 11:09:51.957: E/AndroidRuntime(4706):     at java.lang.reflect.Method.invokeNative(Native Method)
12-13 11:09:51.957: E/AndroidRuntime(4706):     at java.lang.reflect.Method.invoke(Method.java:525)
12-13 11:09:51.957: E/AndroidRuntime(4706):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
12-13 11:09:51.957: E/AndroidRuntime(4706):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
12-13 11:09:51.957: E/AndroidRuntime(4706):     at dalvik.system.NativeStart.main(Native Method)
12-13 11:09:51.957: E/AndroidRuntime(4706): Caused by: java.lang.NullPointerException
12-13 11:09:51.957: E/AndroidRuntime(4706):     at com.ariagostar.hiv.MapActivity.initilizeMap(MapActivity.java:76)
12-13 11:09:51.957: E/AndroidRuntime(4706):     at com.ariagostar.hiv.MapActivity.onResume(MapActivity.java:90)
12-13 11:09:51.957: E/AndroidRuntime(4706):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1209)
12-13 11:09:51.957: E/AndroidRuntime(4706):     at android.app.Activity.performResume(Activity.java:5450)
12-13 11:09:51.957: E/AndroidRuntime(4706):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2919)
12-13 11:09:51.957: E/AndroidRuntime(4706):     ... 12 more

4 个答案:

答案 0 :(得分:1)

仅在API级别12之后才支持MapFragment或SupportMapFragment。建议使用SupportMapFragment。

Fragment类在API Level 11之前不可用,并且您的目标是不支持Fragment的API Level 8。这就是为什么我认为你得到如下错误:

Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader

答案 1 :(得分:0)

您需要在项目中导入Google Play Services

检查您是否已在Android SDK Manager

下载了Google Play服务

了解更多信息Check this example

答案 2 :(得分:0)

当您的错误指示INSTALL_FAILED_MISSING_SHARED_LIBRARY and cancels the istalation. in my real device "unfortunately ... has stoped"并且正在使用SupportMapFragment及其在supportv4库中可用时,应该在项目中添加support-library-v4以支持下面的地图片段API12。

更改您的onResume(),如下所示:

   @Override
    protected void onResume() {
         super.onResume();
         initilizeMap();
    }

答案 3 :(得分:0)

我终于解决了这个问题。这只是因为使用

android:name="com.google.android.gms.maps.MapFragment"

并且应该使用这个:

android:name="com.google.android.gms.maps.SupportMapFragment"

并且应用正常运行