i have a problem to access SupportMapFragment, in this part of my code, its always returning NPE,
googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
Here is my fragment_main.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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="yai.properti.tujuh.tujuh.tujuh.MainActivity$PlaceholderFragment" >
<fragment
android:id="@+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>
and my MainActivity.java
//...
import android.app.Activity;
import android.app.Dialog;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.widget.DrawerLayout;
import android.widget.TextView;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
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.SupportMapFragment;
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;
public class MainActivity extends ActionBarActivity implements
NavigationDrawerFragment.NavigationDrawerCallbacks {
static final LatLng HAMBURG = new LatLng(53.558, 9.927);
static final LatLng KIEL = new LatLng(53.551, 9.993);
private GoogleMap googleMap;
private NavigationDrawerFragment mNavigationDrawerFragment;
private CharSequence mTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int status = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(getBaseContext());
if (status != ConnectionResult.SUCCESS) {
int requestCode = 10;
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this,
requestCode);
dialog.show();
} else {
setUpMapIfNeeded();
}
mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager()
.findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
mNavigationDrawerFragment.setUp(R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
private void setUpMapIfNeeded() {
if (googleMap == null) {
// Part of my code that i get NPE
// my code running well if i commenting this section
//googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
if (googleMap != null) {
}
}
}
//...
and here my PlaceholderFragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
i think my manifest works fine, because everything is fine before i used
googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)
but, for clearing my question, here my manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="yai.properti.tujuh.tujuh.tujuh"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<permission
android:name="yai.properti.tujuh.tujuh.tujuh.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="yai.properti.tujuh.tujuh.tujuh.permission.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" />
<!-- Not Required in Map API v2 but highly recomended -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-library
android:name="com.google.android.maps"
android:required="true"
/>
<!-- Initiating Activity -->
<activity
android:name="yai.properti.tujuh.tujuh.tujuh.InitiatingActivity"
android:label="@string/app_name"
android:screenOrientation="landscape"
android:theme="@android:style/Theme.Black.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Welcome Activity -->
<activity
android:name="yai.properti.tujuh.tujuh.tujuh.WelcomeActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Black.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</activity>
<!-- Main Activity -->
<activity
android:name="yai.properti.tujuh.tujuh.tujuh.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<!-- Login Activity -->
<activity
android:name="yai.properti.tujuh.tujuh.tujuh.LoginActivity"
android:label="@string/title_activity_login"
android:windowSoftInputMode="adjustResize|stateHidden"
android:screenOrientation="landscape"
>
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="XXX" />
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>
and here my log cat that explain to me NPE problem :
06-02 18:32:04.292: W/dalvikvm(32016): threadid=1: thread exiting with uncaught exception (group=0x41d0b2a0)
06-02 18:32:04.297: E/AndroidRuntime(32016): FATAL EXCEPTION: main
06-02 18:32:04.297: E/AndroidRuntime(32016): java.lang.RuntimeException: Unable to start activity ComponentInfo{yai.properti.tujuh.tujuh.tujuh/yai.properti.tujuh.tujuh.tujuh.MainActivity}: java.lang.NullPointerException
06-02 18:32:04.297: E/AndroidRuntime(32016): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
06-02 18:32:04.297: E/AndroidRuntime(32016): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
06-02 18:32:04.297: E/AndroidRuntime(32016): at android.app.ActivityThread.access$700(ActivityThread.java:140)
06-02 18:32:04.297: E/AndroidRuntime(32016): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
06-02 18:32:04.297: E/AndroidRuntime(32016): at android.os.Handler.dispatchMessage(Handler.java:99)
06-02 18:32:04.297: E/AndroidRuntime(32016): at android.os.Looper.loop(Looper.java:137)
06-02 18:32:04.297: E/AndroidRuntime(32016): at android.app.ActivityThread.main(ActivityThread.java:4921)
06-02 18:32:04.297: E/AndroidRuntime(32016): at java.lang.reflect.Method.invokeNative(Native Method)
06-02 18:32:04.297: E/AndroidRuntime(32016): at java.lang.reflect.Method.invoke(Method.java:511)
06-02 18:32:04.297: E/AndroidRuntime(32016): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
06-02 18:32:04.297: E/AndroidRuntime(32016): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
06-02 18:32:04.297: E/AndroidRuntime(32016): at dalvik.system.NativeStart.main(Native Method)
06-02 18:32:04.297: E/AndroidRuntime(32016): Caused by: java.lang.NullPointerException
06-02 18:32:04.297: E/AndroidRuntime(32016): at yai.properti.tujuh.tujuh.tujuh.MainActivity.setUpMapIfNeeded(MainActivity.java:84)
06-02 18:32:04.297: E/AndroidRuntime(32016): at yai.properti.tujuh.tujuh.tujuh.MainActivity.onCreate(MainActivity.java:68)
06-02 18:32:04.297: E/AndroidRuntime(32016): at android.app.Activity.performCreate(Activity.java:5188)
06-02 18:32:04.297: E/AndroidRuntime(32016): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
06-02 18:32:04.297: E/AndroidRuntime(32016): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
06-02 18:32:04.297: E/AndroidRuntime(32016): ... 11 more
I have tried to find solution in SO, but no one can solve my problem, i have tried to :
Used MapFragment by following this link but not solved my question.
Following this solution and still give me some error.
Following this not helped too.
Following this question and not fixing my problem, so i make a new question here in SO.
Following this link, link, and another link but no one can solve my problem.
My Google play services work properly, so its not from Google Play Services.
Any advice? what's solution? many thanks, i really need this for my collage final project, any help would be appreciated it.
UPDATE
line 84 on Log Cat is :
googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();