我对Android应用开发相当新,我现在正在为Android Studio中的教师项目(使用API 23)做这件事。我直截了当地说:我运行的应用程序在我运行时在模拟器上工作(没有显示任何致命错误),但是当我在实际设备上运行它时,它会崩溃并出现错误"二进制XML文件行#12:错误膨胀类片段"。
完整堆栈跟踪:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dule.trackerapp/com.example.dule.trackerapp.LokacijaActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
at android.app.ActivityThread.access$900(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1319)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5478)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:691)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:839)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:745)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
at android.app.Activity.setContentView(Activity.java:1915)
at com.example.dule.trackerapp.LokacijaActivity.onCreate(LokacijaActivity.java:39)
at android.app.Activity.performCreate(Activity.java:5234)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
at android.app.ActivityThread.access$900(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1319)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5478)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:691)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException: name == null
at java.lang.VMClassLoader.findLoadedClass(Native Method)
at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:491)
at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
at android.support.v4.app.Fragment.isSupportFragmentClass(Fragment.java:457)
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2248)
at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:111)
at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:314)
at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:31)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:79)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:839)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:745)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
at android.app.Activity.setContentView(Activity.java:1915)
at com.example.dule.trackerapp.LokacijaActivity.onCreate(LokacijaActivity.java:39)
at android.app.Activity.performCreate(Activity.java:5234)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
at android.app.ActivityThread.access$900(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1319)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5478)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:691)
at dalvik.system.NativeStart.main(Native Method)
该活动的完整代码:
public class LokacijaActivity extends FragmentActivity implements OnMapReadyCallback {
//inicijalizacija promenljivih
public GoogleMap mMapa;
public double sirina, duzina;
public static final String SIRINA = "SIRINA";
public static final String DUZINA = "DUZINA";
public File sacuvano = new File("PODACI.txt");
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lokacija);
MapFragment mapFragment = (MapFragment) this.getFragmentManager().findFragmentById(R.id.mapa);
mapFragment.getMapAsync(this);
EditText editTextSirina = (EditText) findViewById(R.id.etSirina);
EditText editTextDuzina = (EditText) findViewById(R.id.etDuzina);
editTextSirina.setText(String.valueOf(sirina));
editTextDuzina.setText(String.valueOf(duzina));
}
@Override
public void onMapReady(GoogleMap map)
{
mMapa = map;
setUpMap();
}
private void setUpMap() {
try {
mMapa.setMyLocationEnabled(true);
}
catch (SecurityException e)
{
e.printStackTrace();
}
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
try
{
final Location myLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (myLocation != null) {
mMapa.setMapType(GoogleMap.MAP_TYPE_NORMAL);
sirina = myLocation.getLatitude();
duzina = myLocation.getLongitude();
}
else {
LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
sirina = myLocation.getLatitude();
duzina = myLocation.getLongitude();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 2000, 10, locationListener);
}
LatLng latLng = new LatLng(sirina, duzina);
mMapa.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMapa.animateCamera(CameraUpdateFactory.zoomTo(14));
mMapa.addMarker(new MarkerOptions().position(new LatLng(sirina, duzina)).title("Ovde ste!").snippet("Lokacija!"));
}
catch (SecurityException e)
{
e.printStackTrace();
}
}
该活动的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.dule.trackerapp.LokacijaActivity"
tools:showIn="@layout/activity_lokacija">
<fragment
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_gravity="top"
android:layout_weight="1"
android:id="@+id/GornjiFragment"
class="com.example.dule.trackerapp.MapaGornjiFragment"
tools:layout="@layout/gornji_fragment_layout" />
<fragment
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/mapa"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
class="com.google.android.gms.maps.MapFragment"/>
</LinearLayout>
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dule.trackerapp">
<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission
android:name="android.permission.INTERNET" />
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission
android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="23" />
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="23" />
<permission
android:name="package.name.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyCpAPnQXwsrHT9RCKfH4ICvZOFdAwBJEa0"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<uses-library
android:name="com.google.android.maps" />
<activity
android:name=".GlavniMeniActivity"
android:exported="true"
android:label="@string/title_activity_glavni_meni"
android:theme="@style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="com.scytec.datamobile.vd.gui.android.AppPreferenceActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".LokacijaActivity"
android:exported="true"
android:label="@string/title_activity_lokacija"
android:parentActivityName=".GlavniMeniActivity"
android:theme="@style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.dule.trackerapp.GlavniMeniActivity" />
</activity>
<activity
android:name=".PosaljiMail"
android:label="@string/title_activity_posalji_mail"
android:parentActivityName=".LokacijaActivity"
android:theme="@style/AppTheme.NoActionBar"
android:exported="true">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.dule.trackerapp.LokacijaActivity" />
</activity>
</application>
</manifest>
请帮我解决这个错误,我真的很感激。