问题
I have read document in https://developers.google.com/maps/documentation/android/start I create project android in eclipse, folow document step by step but when I can't run project.
sample error java.lang.NullPointerException
error log
03-20 15:13:39.332: D/AndroidRuntime(28930): Shutting down VM
03-20 15:13:39.332: W/dalvikvm(28930): threadid=1: thread exiting with uncaught exception (group=0x2b542210)
03-20 15:13:39.352: E/AndroidRuntime(28930): FATAL EXCEPTION: main
03-20 15:13:39.352: E/AndroidRuntime(28930): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.my_ap/com.mrbuoi.my_ap.MainActivity}: java.lang.NullPointerException
03-20 15:13:39.352: E/AndroidRuntime(28930): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
03-20 15:13:39.352: E/AndroidRuntime(28930): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
03-20 15:13:39.352: E/AndroidRuntime(28930): at android.app.ActivityThread.access$600(ActivityThread.java:127)
03-20 15:13:39.352: E/AndroidRuntime(28930): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
03-20 15:13:39.352: E/AndroidRuntime(28930): at android.os.Handler.dispatchMessage(Handler.java:99)
03-20 15:13:39.352: E/AndroidRuntime(28930): at android.os.Looper.loop(Looper.java:137)
03-20 15:13:39.352: E/AndroidRuntime(28930): at android.app.ActivityThread.main(ActivityThread.java:4441)
03-20 15:13:39.352: E/AndroidRuntime(28930): at java.lang.reflect.Method.invokeNative(Native Method)
03-20 15:13:39.352: E/AndroidRuntime(28930): at java.lang.reflect.Method.invoke(Method.java:511)
03-20 15:13:39.352: E/AndroidRuntime(28930): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-20 15:13:39.352: E/AndroidRuntime(28930): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565)
03-20 15:13:39.352: E/AndroidRuntime(28930): at dalvik.system.NativeStart.main(Native Method)
03-20 15:13:39.352: E/AndroidRuntime(28930): Caused by: java.lang.NullPointerException
03-20 15:13:39.352: E/AndroidRuntime(28930): at com.mrbuoi.my_ap.MainActivity.onCreate(MainActivity.java:31)
03-20 15:13:39.352: E/AndroidRuntime(28930): at android.app.Activity.performCreate(Activity.java:4465)
03-20 15:13:39.352: E/AndroidRuntime(28930): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
03-20 15:13:39.352: E/AndroidRuntime(28930): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
03-20 15:13:39.352: E/AndroidRuntime(28930): ... 11 more
----- File AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.my_ap"
android:versionCode="1"
android:versionName="1.0" >
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="14" />
<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"/>
<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.gms.version"
android:value="15" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="MY_APIKEY"/>
<activity
android:name="com.mrbuoi.may_ap.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>
</application>
---- File MainActivity.java
package com.mrbuoi.my_ap;
import com.example.my_ap.R;
import com.google.android.gms.maps.*;
import com.google.android.gms.maps.model.*;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Bundle;
import android.os.Build;
public class MainActivity extends Activity {
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GoogleMap map = ((MapFragment) getFragmentManager()
.findFragmentById(R.id.map)).getMap();
LatLng sydney = new LatLng(-33.867, 151.206);
map.setMyLocationEnabled(true);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));
map.addMarker(new MarkerOptions()
.title("Sydney")
.snippet("The most populous city in Australia.")
.position(sydney));
}
}
----- File 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="com.mrbuoi.my_ap.MainActivity$PlaceholderFragment" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
</RelativeLayout>
Update activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mrbuoi.bactai_app.MainActivity"
tools:ignore="MergeRootFrame" />
Anyone got a tip? Tks
UPDATE:
@PiYusH GuPtA, I have try this but this not work.
my Error log
03-20 15:24:42.922: E/AndroidRuntime(29491): FATAL EXCEPTION: main
03-20 15:24:42.922: E/AndroidRuntime(29491): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.my_ap/com.mrbuoi.my_ap.MainActivity}: java.lang.NullPointerException
03-20 15:24:42.922: E/AndroidRuntime(29491): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
03-20 15:24:42.922: E/AndroidRuntime(29491): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
03-20 15:24:42.922: E/AndroidRuntime(29491): at android.app.ActivityThread.access$600(ActivityThread.java:127)
03-20 15:24:42.922: E/AndroidRuntime(29491): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
03-20 15:24:42.922: E/AndroidRuntime(29491): at android.os.Handler.dispatchMessage(Handler.java:99)
03-20 15:24:42.922: E/AndroidRuntime(29491): at android.os.Looper.loop(Looper.java:137)
03-20 15:24:42.922: E/AndroidRuntime(29491): at android.app.ActivityThread.main(ActivityThread.java:4441)
03-20 15:24:42.922: E/AndroidRuntime(29491): at java.lang.reflect.Method.invokeNative(Native Method)
03-20 15:24:42.922: E/AndroidRuntime(29491): at java.lang.reflect.Method.invoke(Method.java:511)
03-20 15:24:42.922: E/AndroidRuntime(29491): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-20 15:24:42.922: E/AndroidRuntime(29491): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565)
03-20 15:24:42.922: E/AndroidRuntime(29491): at dalvik.system.NativeStart.main(Native Method)
03-20 15:24:42.922: E/AndroidRuntime(29491): Caused by: java.lang.NullPointerException
03-20 15:24:42.922: E/AndroidRuntime(29491): at com.mrbuoi.my_ap.MainActivity.onCreate(MainActivity.java:22)
03-20 15:24:42.922: E/AndroidRuntime(29491): at android.app.Activity.performCreate(Activity.java:4465)
03-20 15:24:42.922: E/AndroidRuntime(29491): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
03-20 15:24:42.922: E/AndroidRuntime(29491): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
03-20 15:24:42.922: E/AndroidRuntime(29491): ... 11 more
回答1:
First of all you need to change your Activity
to FragmentActivity
while extending in your class as below:
public class MainActivity extends FragmentActivity{
And also your layout inflating which contains the GoogleMap
view is having name Fragment_main
and in your activity you have inflated the layout activity_main
which is wrong.
So change your setContentView
layout
setContentView(R.layout.activity_main);
to
setContentView(R.layout.Fragment_main);
Also as you are having the minimum api level below 11 so you have to use SupportMapFragment
to load the map as below:
GoogleMap map = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
EDITED:
In your manifest change your below line
<meta-data
android:name="com.google.android.gms.version"
android:value="15" />
to
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
Also change your Fragment
class name as below in your fragment_main
layout file.
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
回答2:
you need to extends your class a FragmentActivity instead of Activity.
Other thing that you have to add
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
in your manifest file.
Also change this
if(map!=null){
LatLng sydney = new LatLng(-33.867, 151.206);
map.setMyLocationEnabled(true);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));
map.addMarker(new MarkerOptions()
.title("Sydney")
.snippet("The most populous city in Australia.")
.position(sydney));
}
UPDATE:
change here from
setContentView(R.layout.activity_main);
to
setContentView(R.layout.Fragment_main);
Use this
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mrbuoi.bactai_app.MainActivity"
tools:ignore="MergeRootFrame" >
<include layout="@layout/Fragment_main"></include>
</FrameLayout>
来源:https://stackoverflow.com/questions/22526885/cant-create-project-with-google-map-android-api-v2