这里的问题非常简单,但我遇到了运行时错误,如下所示:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.maptest/com.example.maptest.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
我以前遇到过这个问题,我的直觉是它与图书馆有关,但我不确定。我知道这里有很多这样的问题,但它们非常具体。我只是想知道是否有什么东西突然出现我的代码错了。我很感激帮助!
我的主要活动文件(MainActivity.java)是:
package com.example.maptest;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
private static final int ACTIVITY_CREATE = 0;
private void configureSettings() {
Intent intent = new Intent(this, ConfigureSyncActivity.class);
startActivityForResult(intent, ACTIVITY_CREATE);
}
private void setLimits() {
Intent intent = new Intent(this, SetLimitsActivity.class);
startActivityForResult(intent, ACTIVITY_CREATE);
}
private void violations() {
Intent intent = new Intent(this, ViolationsActivity.class);
startActivityForResult(intent, ACTIVITY_CREATE);
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()) {
case R.id.limits:
setLimits();
return true;
case R.id.sync:
configureSettings();
return true;
case R.id.violations:
violations();
return true;
}
return super.onOptionsItemSelected(item);
}
}
最后但并非最不重要的是,我的xml布局文件:
<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"/>
答案 0 :(得分:1)
在xml布局文件中尝试以下更改。它对我有用:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
如果这不起作用,请尝试检查类名称层次结构。