我試圖建立與谷歌地圖應用程序,當我嘗試運行我得到錯誤谷歌地圖無法啓動活動ComponentInfo android.view.InflateException:二進制XML文件6號線:錯誤充氣類片段
但我得到以下錯誤
05-20 17:38:42.448: W/dalvikvm(722): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
05-20 17:38:42.458: E/AndroidRuntime(722): FATAL EXCEPTION: main
05-20 17:38:42.458: E/AndroidRuntime(722): java.lang.RuntimeException: Unable to resume activity {info.androidhive.googlemapsv2/info.androidhive.googlemapsv2.MainActivity}: java.lang.NullPointerException
05-20 17:38:42.458: E/AndroidRuntime(722): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2575)
05-20 17:38:42.458: E/AndroidRuntime(722): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2603)
05-20 17:38:42.458: E/AndroidRuntime(722): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2089)
05-20 17:38:42.458: E/AndroidRuntime(722): at android.app.ActivityThread.access$600(ActivityThread.java:130)
05-20 17:38:42.458: E/AndroidRuntime(722): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
05-20 17:38:42.458: E/AndroidRuntime(722): at android.os.Handler.dispatchMessage(Handler.java:99)
05-20 17:38:42.458: E/AndroidRuntime(722): at android.os.Looper.loop(Looper.java:137)
05-20 17:38:42.458: E/AndroidRuntime(722): at android.app.ActivityThread.main(ActivityThread.java:4745)
05-20 17:38:42.458: E/AndroidRuntime(722): at java.lang.reflect.Method.invokeNative(Native Method)
05-20 17:38:42.458: E/AndroidRuntime(722): at java.lang.reflect.Method.invoke(Method.java:511)
05-20 17:38:42.458: E/AndroidRuntime(722): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-20 17:38:42.458: E/AndroidRuntime(722): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-20 17:38:42.458: E/AndroidRuntime(722): at dalvik.system.NativeStart.main(Native Method)
05-20 17:38:42.458: E/AndroidRuntime(722): Caused by: java.lang.NullPointerException
05-20 17:38:42.458: E/AndroidRuntime(722): at info.androidhive.googlemapsv2.MainActivity.initilizeMap(MainActivity.java:135)
05-20 17:38:42.458: E/AndroidRuntime(722): at info.androidhive.googlemapsv2.MainActivity.onResume(MainActivity.java:126)
05-20 17:38:42.458: E/AndroidRuntime(722): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1184)
05-20 17:38:42.458: E/AndroidRuntime(722): at android.app.Activity.performResume(Activity.java:5082)
05-20 17:38:42.458: E/AndroidRuntime(722): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2565)
05-20 17:38:42.458: E/AndroidRuntime(722): ... 12 more
活動主要XML
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
MainActivity.java
package info.androidhive.googlemapsv2;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.widget.Toast;
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.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends FragmentActivity {
// Google Map
private GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try{
_SetMap();
}catch(Exception e){
e.printStackTrace();
}
}
@Override
protected void onResume() {
super.onResume();
initilizeMap();
}
/**
* function to load map If map is not created it will create it for you
* */
private void _SetMap() {
if (googleMap == null)
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
/*
* creating random postion around a location for testing purpose only
*/
private double[] createRandLocation(double latitude, double longitude) {
return new double[] { latitude + ((Math.random() - 0.5)/500),
longitude + ((Math.random() - 0.5)/500),
150 + ((Math.random() - 0.5) * 10) };
}
}
任何機構請幫我解決這個
請您花費佈局文件和清單..也發佈logcat .. – Lal
發佈您的MainActivity.java..also什麼是行號:135在? – Lal
張貼您的代碼... – Dev