引用到GoogleMap对象后的空指针

时间:2014-07-13 09:33:26

标签: android google-maps nullpointerexception google-maps-android-api-2 supportmapfragment

我是Android编程新手我被要求制作一个有助于公共交通的地图应用程序,无论如何,我已经添加了API,一切正常,直到我必须制作地图的对象我做了一切都定期,称为

  

的setContentView(R.layout.activitymap);

在Map instialization之前

。 我有一个main_fragment.xml文件是一个线性布局里面有一个按钮,我点击弹出地图,并将Contentview设置为mapactivity.class文件,我想知道为什么当我尝试运行我的地图我得到一个null指针异常,顺便说一句地图工作正常,没有对象instialization ..

  

initMap()

我也没有太多经验,所以我按照lynda教程...

package com.tari2yp2;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.MenuItem;
import android.widget.Toast;

public class MapActivity extends FragmentActivity {
    private static final int GPS_ERRORDIALOG_REQUEST = 9001;
    GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activitymap);
        if (servicesOK()){
            setContentView(R.layout.activitymap);
            //Toast.makeText(this,"Ready to Map! ", Toast.LENGTH_SHORT).show();
        }
        else {
            Toast.makeText(this,"Unable to Display Google Map",       Toast.LENGTH_SHORT).show();
            setContentView(R.layout.fragment_main);
        }
        if (initMap()) {
            Toast.makeText(this,"Play with your map! ", Toast.LENGTH_SHORT).show();
        }
        else {
                Toast.makeText(this,"Map not Available!", Toast.LENGTH_SHORT).show();
        }

    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
           int id = item.getItemId();
           if (id == R.id.action_settings) {
                  return true;
  }
                return super.onOptionsItemSelected(item);
  }
    public boolean servicesOK(){
          int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
            if (isAvailable == ConnectionResult.SUCCESS){
                return true;
            }
            else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)){
               Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable, this,       GPS_ERRORDIALOG_REQUEST);
                dialog.show();
            }
            else {
                Toast.makeText(this,"can't connect to google play services",   Toast.LENGTH_SHORT).show();
            }
            return false;
        }
    public boolean initMap()
    {

        if (mMap == null) {
            SupportMapFragment mapFrag =
                    (SupportMapFragment)   getSupportFragmentManager().findFragmentById(R.id.map);
            mMap = mapFrag.getMap();
        }
        return (mMap != null);
    }
}

日志文件:

            07-13 12:19:40.917: D/dalvikvm(18134): GC_FOR_ALLOC freed 127K, 14% free          9614K/11143K, paused 18ms, total 18ms
    07-13 12:19:40.917: I/dalvikvm-heap(18134): Grow heap (frag case) to 11.709MB for 1013488-byte allocation
  07-13 12:19:41.017: D/dalvikvm(18134): GC_CONCURRENT freed 1K, 13% free 10602K/12167K, paused 2ms+2ms, total 103ms
  07-13 12:19:41.017: D/dalvikvm(18134): WAIT_FOR_CONCURRENT_GC blocked 73ms
  07-13 12:19:41.157: D/libEGL(18134): loaded /system/lib/egl/libEGL_mali.so
  07-13 12:19:41.157: D/libEGL(18134): loaded /system/lib/egl/libGLESv1_CM_mali.so
  07-13 12:19:41.167: D/libEGL(18134): loaded /system/lib/egl/libGLESv2_mali.so
  07-13 12:19:41.187: I/(18134): ION_IOC_IMPORT success d9f09ac0,phy addr = 8efe0000
  07-13 12:19:41.187: D/OpenGLRenderer(18134): Enabling debug mode 0
  07-13 12:19:47.367: I/(18134): ION_IOC_IMPORT success c883dc40,phy addr = 8ee69000
  07-13 12:19:47.657: I/(18134): ION_IOC_IMPORT success c51ad600,phy addr = 8f157000
  07-13 12:19:47.657: I/(18134): ION_IOC_FREE success d9f09ac0,phy addr = 8efe0000
  07-13 12:19:47.657: I/(18134): ION_IOC_FREE success c51ad600,phy addr = 8f157000
  07-13 12:19:47.677: E/SpannableStringBuilder(18134): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
  07-13 12:19:47.677: E/SpannableStringBuilder(18134): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
  07-13 12:19:52.747: W/IInputConnectionWrapper(18134): getSelectedText on inactive InputConnection
  07-13 12:19:52.747: W/IInputConnectionWrapper(18134): setComposingText on inactive InputConnection
  07-13 12:19:54.397: I/(18134): ION_IOC_IMPORT success c552ef40,phy addr = 8efe0000
  07-13 12:19:56.047: I/(18134): ION_IOC_IMPORT success de293980,phy addr = 8f157000
  07-13 12:19:56.417: I/(18134): ION_IOC_IMPORT success c32977c0,phy addr = 8f2ce000
  07-13 12:19:56.717: I/u(18134): Making Creator dynamically
  07-13 12:19:57.067: D/dalvikvm(18134): GC_CONCURRENT freed 192K, 13% free 10971K/12551K, paused 63ms+4ms, total 148ms
  07-13 12:19:57.067: D/dalvikvm(18134): WAIT_FOR_CONCURRENT_GC blocked 111ms
  07-13 12:19:57.307: I/Google Maps Android API(18134): Google Play services client version: 5077000
  07-13 12:19:57.417: I/dalvikvm(18134): Could not find method guj.a, referenced from method gqi.a
  07-13 12:19:57.417: W/dalvikvm(18134): VFY: unable to resolve static method 24936: Lguj;.a (Landroid/content/Context;)Lgri;
  07-13 12:19:57.417: D/dalvikvm(18134): VFY: replacing opcode 0x71 at 0x0003
  07-13 12:19:57.437: E/dalvikvm(18134): Could not find class 'gpr', referenced from method gps.a
  07-13 12:19:57.437: W/dalvikvm(18134): VFY: unable to resolve new-instance 4090 (Lgpr;) in Lgps;
  07-13 12:19:57.437: D/dalvikvm(18134): VFY: replacing opcode 0x22 at 0x0000
  07-13 12:19:57.437: E/dalvikvm(18134): Could not find class 'gpr', referenced from method gps.a
  07-13 12:19:57.437: W/dalvikvm(18134): VFY: unable to resolve new-instance 4090       (Lgpr;) in Lgps;
  07-13 12:19:57.437: D/dalvikvm(18134): VFY: replacing opcode 0x22 at 0x0000
  07-13 12:19:57.437: E/dalvikvm(18134): Could not find class 'gpr', referenced from method gps.a
  07-13 12:19:57.437: W/dalvikvm(18134): VFY: unable to resolve new-instance 4090 (Lgpr;) in Lgps;
  07-13 12:19:57.437: D/dalvikvm(18134): VFY: replacing opcode 0x22 at 0x0000
  07-13 12:19:57.437: D/dalvikvm(18134): DexOpt: unable to opt direct call 0x5ea3 at 0x0a in Lgps;.a
  07-13 12:19:57.437: D/dalvikvm(18134): DexOpt: unable to opt direct call 0x5ea3 at 0x0c in Lgps;.a
  07-13 12:19:57.437: D/dalvikvm(18134): DexOpt: unable to opt direct call 0x5ea3 at             0x0a in Lgps;.a
  07-13 12:19:57.457: I/Google Maps Android API(18134): Google Play services package version: 5084034
  07-13 12:19:57.477: W/dalvikvm(18134): VFY: unable to resolve static field 20873 (t) in Lyp;
  07-13 12:19:57.477: D/dalvikvm(18134): VFY: replacing opcode 0x62 at 0x000e
  07-13 12:19:57.477: W/dalvikvm(18134): VFY: unable to resolve static field 20873 (t) in Lyp;
  07-13 12:19:57.477: D/dalvikvm(18134): VFY: replacing opcode 0x62 at 0x000d
  07-13 12:19:57.677: D/dalvikvm(18134): GC_CONCURRENT freed 1290K, 21% free 10251K/12935K, paused 20ms+20ms, total 188ms
  07-13 12:19:57.677: D/dalvikvm(18134): WAIT_FOR_CONCURRENT_GC blocked 44ms
  07-13 12:19:58.297: D/dalvikvm(18134): GC_CONCURRENT freed 267K, 20% free 10418K/12935K, paused 44ms+42ms, total 145ms
  07-13 12:19:58.567: I/dalvikvm(18134): Failed resolving Lcom/google/android/gms/location/internal/ParcelableGeofence; interface 4023 'Lgln;'
  07-13 12:19:58.567: W/dalvikvm(18134): Link of class 'Lcom/google/android/gms/location/internal/ParcelableGeofence;' failed
  07-13 12:19:58.567: E/dalvikvm(18134): Could not find class 'com.google.android.gms.location.internal.ParcelableGeofence', referenced from method glt.a
  07-13 12:19:58.567: W/dalvikvm(18134): VFY: unable to resolve check-cast 2086 (Lcom/google/android/gms/location/internal/ParcelableGeofence;) in Lglt;
  07-13 12:19:58.577: D/dalvikvm(18134): VFY: replacing opcode 0x1f at 0x0019
  07-13 12:19:58.757: D/AbsListView(18134): Get MotionRecognitionManager
  07-13 12:19:58.877: D/dalvikvm(18134): GC_CONCURRENT freed 254K, 18% free 10676K/12935K, paused 21ms+57ms, total 215ms
  07-13 12:19:58.877: D/dalvikvm(18134): WAIT_FOR_CONCURRENT_GC blocked 11ms
  07-13 12:19:59.037: D/AndroidRuntime(18134): Shutting down VM
  07-13 12:19:59.037: W/dalvikvm(18134): threadid=1: thread exiting with uncaught exception (group=0x41d162b8)
  07-13 12:19:59.167: W/ActivityThread(18134): ClassLoader.loadClass: The class loader returned by Thread.getContextClassLoader() may fail for processes that host multiple applications. You should explicitly specify a context class loader. For example: Thread.setContextClassLoader(getClass().getClassLoader());
  07-13 12:19:59.177: E/AndroidRuntime(18134): FATAL EXCEPTION: main
  07-13 12:19:59.177: E/AndroidRuntime(18134): java.lang.RuntimeException: Unable to             start activity ComponentInfo{com.tari2yp2/com.tari2yp2.MapActivity}: java.lang.NullPointerException
  07-13 12:19:59.177: E/AndroidRuntime(18134):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2118)
  07-13 12:19:59.177: E/AndroidRuntime(18134):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2143)
  07-13 12:19:59.177: E/AndroidRuntime(18134):  at android.app.ActivityThread.access$700(ActivityThread.java:140)
  07-13 12:19:59.177: E/AndroidRuntime(18134):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
  07-13 12:19:59.177: E/AndroidRuntime(18134):  at android.os.Handler.dispatchMessage(Handler.java:99)
  07-13 12:19:59.177: E/AndroidRuntime(18134):  at android.os.Looper.loop(Looper.java:174)
  07-13 12:19:59.177: E/AndroidRuntime(18134):  at android.app.ActivityThread.main(ActivityThread.java:4952)
  07-13 12:19:59.177: E/AndroidRuntime(18134):  at java.lang.reflect.Method.invokeNative(Native Method)
  07-13 12:19:59.177: E/AndroidRuntime(18134):  at java.lang.reflect.Method.invoke(Method.java:511)
  07-13 12:19:59.177: E/AndroidRuntime(18134):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
  07-13 12:19:59.177: E/AndroidRuntime(18134):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
  07-13 12:19:59.177: E/AndroidRuntime(18134):  at dalvik.system.NativeStart.main(Native Method)
  07-13 12:19:59.177: E/AndroidRuntime(18134): Caused by: java.lang.NullPointerException
  07-13 12:19:59.177: E/AndroidRuntime(18134):  at com.tari2yp2.MapActivity.initMap(MapActivity.java:69)
  07-13 12:19:59.177: E/AndroidRuntime(18134):  at com.tari2yp2.MapActivity.onCreate(MapActivity.java:29)
  07-13 12:19:59.177: E/AndroidRuntime(18134):  at android.app.Activity.performCreate(Activity.java:5188)
  07-13 12:19:59.177: E/AndroidRuntime(18134):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
  07-13 12:19:59.177: E/AndroidRuntime(18134):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2082)
  07-13 12:19:59.177: E/AndroidRuntime(18134):  ... 11 more
  07-13 12:19:59.967: D/dalvikvm(18134): GC_CONCURRENT freed 228K, 16% free 10894K/12935K, paused 21ms+21ms, total 119ms

0 个答案:

没有答案