Googleplayservices v2修订版12给出了nullpointerexception

时间:2013-10-26 06:51:54

标签: android google-maps google-maps-android-api-2 google-play-services

我最近在eclipse中更新了我的Google Play服务,直至修订版12, 我已经在我的日食中重新导入了lib,但之后我在

处有空指针异常
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);

但我已将地图初始化为

所有旧项目都会发生这种情况,无论我将Google Play服务用作lib还是我正在显示Google地图,我正在检查模拟器

编辑:经过一些刷新和几次重新导入lib之后我现在遇到了这个错误

10-26 03:35:44.203: E/AndroidRuntime(1537): FATAL EXCEPTION: main
10-26 03:35:44.203: E/AndroidRuntime(1537): java.lang.NoClassDefFoundError:com.google.android.gms.R$styleable
10-26 03:35:44.203: E/AndroidRuntime(1537):     at com.google.android.gms.maps.GoogleMapOptions.createFromAttributes(Unknown Source)
10-26 03:35:44.203: E/AndroidRuntime(1537):     at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source)
10-26 03:35:44.203: E/AndroidRuntime(1537):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:284)
10-26 03:35:44.203: E/AndroidRuntime(1537):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
10-26 03:35:44.203: E/AndroidRuntime(1537):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
10-26 03:35:44.203: E/AndroidRuntime(1537):     at  android.view.LayoutInflater.inflate(LayoutInflater.java:492)
10-26 03:35:44.203: E/AndroidRuntime(1537):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
10-26 03:35:44.203: E/AndroidRuntime(1537):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
10-26 03:35:44.203: E/AndroidRuntime(1537):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
10-26 03:35:44.203: E/AndroidRuntime(1537):     at android.app.Activity.setContentView(Activity.java:1895)
10-26 03:35:44.203: E/AndroidRuntime(1537):     at com.example.googlemapscreenshot.MainActivity.onCreate(MainActivity.java:61)
10-26 03:35:44.203: E/AndroidRuntime(1537):     at android.app.Activity.performCreate(Activity.java:5133)
10-26 03:35:44.203: E/AndroidRuntime(1537):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-26 03:35:44.203: E/AndroidRuntime(1537):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
10-26 03:35:44.203: E/AndroidRuntime(1537):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-26 03:35:44.203: E/AndroidRuntime(1537):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-26 03:35:44.203: E/AndroidRuntime(1537):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-26 03:35:44.203: E/AndroidRuntime(1537):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-26 03:35:44.203: E/AndroidRuntime(1537):     at android.os.Looper.loop(Looper.java:137)
10-26 03:35:44.203: E/AndroidRuntime(1537):     at android.app.ActivityThread.main(ActivityThread.java:5103)
10-26 03:35:44.203: E/AndroidRuntime(1537):     at java.lang.reflect.Method.invokeNative(Native Method)
10-26 03:35:44.203: E/AndroidRuntime(1537):     at java.lang.reflect.Method.invoke(Method.java:525)
10-26 03:35:44.203: E/AndroidRuntime(1537):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-26 03:35:44.203: E/AndroidRuntime(1537):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-26 03:35:44.203: E/AndroidRuntime(1537):     at dalvik.system.NativeStart.main(Native Method)

这可能是模拟器问题吗?

根据GrlsHu编辑:

布局:

<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"
tools:context=".MainActivity" >

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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/snap"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Take Snap Shot" />

    <ImageView
        android:id="@+id/imgv"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_gravity="right" />
</LinearLayout>

java代码:

public class MainActivity extends FragmentActivity {

GoogleMap map;
Button snap;
double latti, longi;
ImageView imgv;
ArrayList<String> strlist;
String mPath;

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

    snap = (Button) findViewById(R.id.snap);
    imgv = (ImageView) findViewById(R.id.imgv);
    map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
    map.setMapType(GoogleMap.MAP_TYPE_NORMAL);


    strlist = new ArrayList<String>();

    latti = 17.4721005;
    longi = 78.3443972;

    map.addMarker(new MarkerOptions().position(new LatLng(latti, longi)).title("Office").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))).showInfoWindow();
    map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latti,longi), 15.0f));

    snap.setOnClickListener(new OnClickListener() {

        @SuppressLint("NewApi")
        @Override
        public void onClick(View v) {
            SnapshotReadyCallback callback = new SnapshotReadyCallback() {
                Bitmap bitmap;

                @Override
                public void onSnapshotReady(Bitmap snapshot) {
                    bitmap = snapshot;
                    try {
                        mPath = "/mnt/sdcard/"+ "MyMapScreen";
                        File imageFile = new File(mPath);
                        FileOutputStream out = new FileOutputStream(imageFile);

                        // above "/mnt ..... png" => is a storage path (where image will be stored) + name of image you can customize as per your Requirement

                        bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            };

            map.snapshot(callback);
            Uri uri = Uri.fromFile(new File(mPath));
            imgv.setImageURI(uri);
    }
    });
}
}
编辑:好的,我的问题解决了,我的系统可能遇到了一些问题,我重新启动系统后工作正常,但现在我有一个不同的问题

你可以看到我的上面的代码,每当我点击按钮时,它应该拍摄一张快照并将其保存到我的SD卡中,但它没有找到类别def

                SnapshotReadyCallback callback = new SnapshotReadyCallback() {

有没有人知道snapshotreadycallback函数以及为什么我会收到此错误?

1 个答案:

答案 0 :(得分:-1)

希望下面的代码会有所帮助

而不是直接初始化map,请执行以下操作以初始化map

if (map == null) {
// Try to obtain the map from the SupportMapFragment.
    map = ((SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map)).getMap();
}

在此if condition内,写下您需要的代码。