map =((mapfragment)getfragmentmanager()。findfragmentbyid(r.id.map))。getmap()致命异常

时间:2014-08-06 22:38:23

标签: nullpointerexception

使用Android Studio,我已经在这个应用程序中盯着,戳戳和刺激了几个小时。我在第28行遇到了致命的例外:

map =((mapfragment)getfragmentmanager()。findfragmentbyid(r.id.map))。getmap()

我不知道此时它出了什么问题。我读了一些关于空值的内容,但是我复制并粘贴了以前一个活动地图应用程序中几乎所有代码都可以完美运行。我的图书馆也在运作。我需要通过这一部分,以便继续使用Google地方信息的搜索功能。

应用程序以启动活动开始 - >主菜单 - >地图活动 包括map.java,map.xml和AndroidManifest.xml

map.java:

package com.LittleBlackApp.MaJoRProduct.littleblackapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;


public class map extends Activity {
private final LatLng LOCATION_NAPLES = new LatLng(40.852758, 14.269437);
private GoogleMap map;

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

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


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public void onClick_btnNap(View v){
    map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
    CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_NAPLES, 9);
    map.animateCamera(update);

}


@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);
}
}

map.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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".main">



<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Naples"
    android:id="@+id/btnNap"
    android:onClick="onClick_btnNap"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />


<fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_below="@+id/btnNap"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</RelativeLayout>

的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.LittleBlackApp.MaJoRProduct.littleblackapp" >

<users-sdk
    android:minSdkVersion="18"
    android:targetSdkVersion="20" />

<permission
    android:name="com.LittleBlackApp.MaJoRProduct.littleblackapp.permission.MAPS_RECEIVE"
    android:protectionLevel="signature"/>
<uses-permission android:name="com.LittleBlackApp.MaJoRProduct.littleblackapp.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">


    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="NEW API KEY HERE"/>
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

    <activity
        android:name=".splash"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:configChanges="keyboardHidden|orientation|screenSize" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".main"
        android:screenOrientation="portrait"
        android:configChanges="keyboardHidden|orientation|screenSize" ></activity>
    <activity android:name=".contact"
        android:screenOrientation="portrait"
        android:configChanges="keyboardHidden|orientation|screenSize" ></activity>
    <activity android:name=".map"
        android:screenOrientation="portrait"
        android:configChanges="keyboardHidden|orientation|screenSize" ></activity>
    <activity android:name=".chat"
        android:screenOrientation="portrait"
        android:configChanges="keyboardHidden|orientation|screenSize" ></activity>
    <activity android:name=".note"
        android:screenOrientation="portrait"
        android:configChanges="keyboardHidden|orientation|screenSize" ></activity>
</application>

</manifest>

感谢任何帮助。

0 个答案:

没有答案