我是Android新手。我正在创建一个应用程序,我必须在我的两个活动中使用地图。我的应用程序运行良好,但是当我点击我的TagYourself'活动(地图活动之一)我的应用程序崩溃。 Log cat在第46行上显示空指针异常,即this.setContentView(R.layout.activity_tag_yourself);
。 Log cat也在显示。
二进制XML文件行#17上的android.view.InflateException
,它是map的片段。
我无法理解为什么会给出这样的例外。请帮我。 这是我的活动日志猫。
04-06 23:20:02.851: I/InputMethodManager(31122): handleMessage: MSG_SET_ACTIVE false, was true
04-06 23:20:02.867: I/InputMethodManager(31122): handleMessage: MSG_UNBIND 985
04-06 23:20:02.911: D/OpenGLRenderer(31122): Flushing caches (mode 0)
04-06 23:20:03.276: D/OpenGLRenderer(31122): Flushing caches (mode 0)
04-06 23:20:08.341: V/PhoneWindow(31122): DecorView setVisiblity: visibility = 0
04-06 23:20:08.386: V/InputMethodManager(31122): onWindowFocus: null softInputMode=32 first=true flags=#1810100
04-06 23:20:08.386: V/InputMethodManager(31122): START INPUT: com.android.internal.policy.impl.PhoneWindow$DecorView{41394140 V.E..... R.....ID 0,0-480,854} ic=null tba=android.view.inputmethod.EditorInfo@412aebc0 controlFlags=#104
04-06 23:20:08.390: V/InputMethodManager(31122): Starting input: Bind result=InputBindResult{com.android.internal.view.IInputMethodSession$Stub$Proxy@412635d8 com.android.inputmethod.latin/.LatinIME #989}
04-06 23:20:08.420: I/InputMethodManager(31122): handleMessage: MSG_SET_ACTIVE true, was false
04-06 23:20:08.427: I/SurfaceTextureClient(31122): [STC::queueBuffer] (this:0x52cb22e8) fps:0.15, dur:6849.47, max:6849.47, min:6849.47
04-06 23:20:08.427: I/SurfaceTextureClient(31122): [STC::queueBuffer] this:0x52cb22e8, api:1, last queue time elapsed:6849.47
04-06 23:20:09.363: V/Provider/Settings(31122): invalidate [system]: current 182 != cached 0
04-06 23:20:09.367: D/ActivityThread(31122): installProvider: context=android.app.ContextImpl@41393588holder=android.app.IActivityManager$ContentProviderHolder@41271e08noisy=truenoReleaseNeeded=truestable=true
04-06 23:20:09.368: V/Provider/Settings(31122): from db cache, name = sound_effects_enabled , value = 0
04-06 23:20:09.393: D/mylog(31122): Wifi State true
04-06 23:20:09.514: I/SurfaceTextureClient(31122): [STC::queueBuffer] (this:0x52cb22e8) fps:1.84, dur:1087.15, max:843.11, min:244.04
04-06 23:20:09.562: D/AndroidRuntime(31122): Shutting down VM
04-06 23:20:09.562: W/dalvikvm(31122): threadid=1: thread exiting with uncaught exception (group=0x40cab9a8)
04-06 23:20:09.584: E/AndroidRuntime(31122): FATAL EXCEPTION: main
04-06 23:20:09.584: E/AndroidRuntime(31122): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jamaat_times/com.example.jamaattiming.TagYourself}: android.view.InflateException: Binary XML file line #17: Error inflating class fragment
04-06 23:20:09.584: E/AndroidRuntime(31122): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
04-06 23:20:09.584: E/AndroidRuntime(31122): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
04-06 23:20:09.584: E/AndroidRuntime(31122): at android.app.ActivityThread.access$600(ActivityThread.java:162)
04-06 23:20:09.584: E/AndroidRuntime(31122): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
04-06 23:20:09.584: E/AndroidRuntime(31122): at android.os.Handler.dispatchMessage(Handler.java:107)
04-06 23:20:09.584: E/AndroidRuntime(31122): at android.os.Looper.loop(Looper.java:194)
04-06 23:20:09.584: E/AndroidRuntime(31122): at android.app.ActivityThread.main(ActivityThread.java:5371)
04-06 23:20:09.584: E/AndroidRuntime(31122): at java.lang.reflect.Method.invokeNative(Native Method)
04-06 23:20:09.584: E/AndroidRuntime(31122): at java.lang.reflect.Method.invoke(Method.java:525)
04-06 23:20:09.584: E/AndroidRuntime(31122): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
04-06 23:20:09.584: E/AndroidRuntime(31122): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
04-06 23:20:09.584: E/AndroidRuntime(31122): at dalvik.system.NativeStart.main(Native Method)
04-06 23:20:09.584: E/AndroidRuntime(31122): Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class fragment
04-06 23:20:09.584: E/AndroidRuntime(31122): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
04-06 23:20:09.584: E/AndroidRuntime(31122): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
04-06 23:20:09.584: E/AndroidRuntime(31122): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
04-06 23:20:09.584: E/AndroidRuntime(31122): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
04-06 23:20:09.584: E/AndroidRuntime(31122): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
04-06 23:20:09.584: E/AndroidRuntime(31122): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:281)
04-06 23:20:09.584: E/AndroidRuntime(31122): at android.app.Activity.setContentView(Activity.java:1881)
04-06 23:20:09.584: E/AndroidRuntime(31122): at com.example.jamaattiming.TagYourself.onCreate(TagYourself.java:46)
04-06 23:20:09.584: E/AndroidRuntime(31122): at android.app.Activity.performCreate(Activity.java:5122)
04-06 23:20:09.584: E/AndroidRuntime(31122): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
04-06 23:20:09.584: E/AndroidRuntime(31122): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
04-06 23:20:09.584: E/AndroidRuntime(31122): ... 11 more
04-06 23:20:09.584: E/AndroidRuntime(31122): Caused by: java.lang.NullPointerException: name == null
04-06 23:20:09.584: E/AndroidRuntime(31122): at java.lang.VMClassLoader.findLoadedClass(Native Method)
04-06 23:20:09.584: E/AndroidRuntime(31122): at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:354)
04-06 23:20:09.584: E/AndroidRuntime(31122): at java.lang.ClassLoader.loadClass(ClassLoader.java:491)
04-06 23:20:09.584: E/AndroidRuntime(31122): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
04-06 23:20:09.584: E/AndroidRuntime(31122): at android.support.v4.app.Fragment.isSupportFragmentClass(Fragment.java:436)
04-06 23:20:09.584: E/AndroidRuntime(31122): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:255)
04-06 23:20:09.584: E/AndroidRuntime(31122): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
04-06 23:20:09.584: E/AndroidRuntime(31122): ... 21 more
这里是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:orientation="vertical"
android:background="#808080"
tools:context=".TagYourself"
android:id="@+id/activity_tag_yourself">
<TextView
android:id="@+id/tv_distance_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="@string/hello_world" />
<fragment
android:id="@+id/map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_distance_time" />
</RelativeLayout>
这是TagYourself类的Java代码:
public class TagYourself extends FragmentActivity
{
GoogleMap map;
ArrayList<LatLng> markerPoints;
TextView tvDistanceDuration;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_tag_yourself);
this.tvDistanceDuration = (TextView) this.findViewById(R.id.tv_distance_time);
// Initializing
this.markerPoints = new ArrayList<LatLng>();
SupportMapFragment fm = (SupportMapFragment) this.getSupportFragmentManager().findFragmentById(R.id.map);
// Getting Map for the SupportMapFragment
this.map = fm.getMap();
// Enable MyLocation Button in the Map
this.map.setMyLocationEnabled(true);
// Setting onclick event listener for the map
this.map.setOnMapClickListener(new OnMapClickListener()
{
@Override
public void onMapClick(LatLng point)
{
// Already two locations
if (TagYourself.this.markerPoints.size() > 1)
{
TagYourself.this.markerPoints.clear();
TagYourself.this.map.clear();
}
// Adding new item to the ArrayList
TagYourself.this.markerPoints.add(point);
// Creating MarkerOptions
MarkerOptions options = new MarkerOptions();
// Setting the position of the marker
options.position(point);
/**
* For the start location, the color of marker is GREEN and
* for the end location, the color of marker is RED.
*/
if (TagYourself.this.markerPoints.size() == 1)
{
options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
} else if (TagYourself.this.markerPoints.size() == 2)
{
options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
}
// Add new marker to the Google Map Android API V2
TagYourself.this.map.addMarker(options);
// Checks, whether start and end locations are captured
if (TagYourself.this.markerPoints.size() >= 2)
{
LatLng origin = TagYourself.this.markerPoints.get(0);
LatLng dest = TagYourself.this.markerPoints.get(1);
// Getting URL to the Google Directions API
String url = TagYourself.this.getDirectionsUrl(origin, dest);
DownloadTask downloadTask = new DownloadTask();
// Start downloading json data from Google Directions API
downloadTask.execute(url);
}
}
});
}
private String getDirectionsUrl(LatLng origin, LatLng dest)
{
// Origin of route
String str_origin = "origin=" + origin.latitude + "," + origin.longitude;
// Destination of route
String str_dest = "destination=" + dest.latitude + "," + dest.longitude;
// Sensor enabled
String sensor = "sensor=false";
// Building the parameters to the web service
String parameters = str_origin + "&" + str_dest + "&" + sensor;
// Output format
String output = "json";
// Building the url to the web service
String url = "https://maps.googleapis.com/maps/api/directions/" + output + "?" + parameters;
return url;
}
/** A method to download json data from url */
private String downloadUrl(String strUrl) throws IOException
{
String data = "";
InputStream iStream = null;
HttpURLConnection urlConnection = null;
try
{
URL url = new URL(strUrl);
// Creating an http connection to communicate with url
urlConnection = (HttpURLConnection) url.openConnection();
// Connecting to url
urlConnection.connect();
// Reading data from url
iStream = urlConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
StringBuffer sb = new StringBuffer();
String line = "";
while ((line = br.readLine()) != null)
{
sb.append(line);
}
data = sb.toString();
br.close();
} catch (Exception e)
{
Log.d("Exception while downloading url", e.toString());
} finally
{
iStream.close();
urlConnection.disconnect();
}
return data;
}
我使用以下权限:
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="16" />
<uses-permission android:name="com.example.jamaat_times.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<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-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
P.S:我的钥匙没问题。
答案 0 :(得分:1)
引起:java.lang.NullPointerException:name == null
您缺少片段的android:name
属性
<fragment
android:id="@+id/map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_distance_time"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
答案 1 :(得分:0)
在xml中使用此代码
<fragment
android:id="@+id/map"
android:layout_width="wrap_content"
android:layout_height="wrap_contet"
android:layout_below="@id/tv_distance_time"
android:name="com.google.android.gms.maps.SupportMapFragment" />