I have seen many other questions like this one on this website and tried changing my code to match but it never solves the problem.
My app loads with some buttons to push. One of them opens a class file that opens a map on the screen. The code for that class is :
public class FuelMap extends Activity
{
private final LatLng locBurb = new LatLng(49.27645, -122.917587);
private final LatLng locSura = new LatLng(49.187500, -122.849000);
private GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fuel_map);
//map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map )).getMap();
// Show the Up button in the action bar.
setupActionBar();
}
/**
* Set up the {@link android.app.ActionBar}, if the API is available.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar()
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
{
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.fuel_map, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
public void OnClickCity(View C)
{
//CameraUpdate update = CameraUpdateFactory.newLatLng(locBurb);
//map.animateCamera(update);
}
public void OnClickBuraby(View B)
{
}
public void OnClickSurray(View S)
{
}
}
But if I uncomment the line: //map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map )).getMap();
and then the app will run until i try to switch it over to the map.
Like I said at the beginning of this I have searched and often found answers on this site relating to my question but none of them gave me any results. I do not believe this problem is due an error of installing the google map stuff needed for the android app since I can load a map. So anyone know what might be the cause?
-LogCat info
04-16 17:16:10.747: D/AndroidRuntime(11672): Shutting down VM
04-16 17:16:10.747: W/dalvikvm(11672): threadid=1: thread exiting with uncaught exception (group=0x41543300)
04-16 17:16:10.752: E/AndroidRuntime(11672): FATAL EXCEPTION: main
04-16 17:16:10.752: E/AndroidRuntime(11672): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.testing.fuelapptesting/com.testing.fuelapptesting.FuelMap}: java.lang.NullPointerException
04-16 17:16:10.752: E/AndroidRuntime(11672): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
04-16 17:16:10.752: E/AndroidRuntime(11672): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
04-16 17:16:10.752: E/AndroidRuntime(11672): at android.app.ActivityThread.access$600(ActivityThread.java:130)
04-16 17:16:10.752: E/AndroidRuntime(11672): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
04-16 17:16:10.752: E/AndroidRuntime(11672): at android.os.Handler.dispatchMessage(Handler.java:99)
04-16 17:16:10.752: E/AndroidRuntime(11672): at android.os.Looper.loop(Looper.java:137)
04-16 17:16:10.752: E/AndroidRuntime(11672): at android.app.ActivityThread.main(ActivityThread.java:4745)
04-16 17:16:10.752: E/AndroidRuntime(11672): at java.lang.reflect.Method.invokeNative(Native Method)
04-16 17:16:10.752: E/AndroidRuntime(11672): at java.lang.reflect.Method.invoke(Method.java:511)
04-16 17:16:10.752: E/AndroidRuntime(11672): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-16 17:16:10.752: E/AndroidRuntime(11672): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-16 17:16:10.752: E/AndroidRuntime(11672): at dalvik.system.NativeStart.main(Native Method)
04-16 17:16:10.752: E/AndroidRuntime(11672): Caused by: java.lang.NullPointerException
04-16 17:16:10.752: E/AndroidRuntime(11672): at com.testing.fuelapptesting.FuelMap.onCreate(FuelMap.java:31)
04-16 17:16:10.752: E/AndroidRuntime(11672): at android.app.Activity.performCreate(Activity.java:5008)
04-16 17:16:10.752: E/AndroidRuntime(11672): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
04-16 17:16:10.752: E/AndroidRuntime(11672): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
04-16 17:16:10.752: E/AndroidRuntime(11672): ... 11 more
04-16 17:16:13.580: I/Process(11672): Sending signal. PID: 11672 SIG: 9
-- activity_Fuel_Map layout
<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=".MainActivity" >
<fragment
android:id="@+id/maps"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/busCity" />
<TextView
android:id="@+id/map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/maps"
android:layout_alignParentTop="true"
android:layout_marginLeft="33dp"
android:ems="10"
android:text="Map" >
<requestFocus />
</TextView>
<Button
android:id="@+id/buttBuraby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/map"
android:layout_toRightOf="@+id/buttCity"
android:onClick="OnClickBuraby"
android:text="Buraby" />
<Button
android:id="@+id/buttSurray"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/map"
android:layout_toRightOf="@+id/buttBuraby"
android:onClick="OnClickSurray"
android:text="Surray" />
<Button
android:id="@+id/buttCity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/buttBuraby"
android:layout_alignBottom="@+id/buttBuraby"
android:layout_alignLeft="@+id/maps"
android:onClick="OnClickCity"
android:text="City" />
</RelativeLayout>
-- Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.testing.fuelapptesting"
android:versionCode="1"
android:versionName="1.0" >
<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"/>
<!-- The following two permissions are not required to use
Google Maps Android API v2, but are recommended. -->
<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"/>
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.testing.fuelapptesting.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.testing.fuelapptesting.News"
android:label="@string/title_activity_news"
android:parentActivityName="com.testing.fuelapptesting.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.testing.fuelapptesting.MainActivity" />
</activity>
<activity
android:name="com.testing.fuelapptesting.Messages"
android:label="@string/title_activity_messages" >
</activity>
<activity
android:name="com.testing.fuelapptesting.FuelMap"
android:label="@string/title_activity_fuel_map"
android:parentActivityName="com.testing.fuelapptesting.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.testing.fuelapptesting.MainActivity" />
</activity>
<activity
android:name="com.testing.fuelapptesting.Scanner"
android:label="@string/title_activity_scanner"
android:parentActivityName="com.testing.fuelapptesting.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.testing.fuelapptesting.MainActivity" />
</activity>
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyC8hlQR9rdDGYMEXQCqpMD2PxzQRrpprPg"/>
</application>
</manifest>