错误:android.view.InflateException:二进制XML文件行:错误膨胀类片段
问题描述:
我根据网络状态加载不同的视图。当网络可用时,我正在加载一个片段,当网络不可用时,我正在加载一个图像。的代码如下所示错误:android.view.InflateException:二进制XML文件行:错误膨胀类片段
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!isConnectedToNetwork()) {
setContentView(R.layout.no_internet_connection);
Toast.makeText(getApplicationContext(), "Not connected to internet. Cannot display the map.", Toast.LENGTH_LONG).show();
}
}
@Override
protected void onResume() {
super.onResume();
if (isConnectedToNetwork()) {
setContentView(R.layout.activity_display_map);
setUpViews();
} else {
stopService(new Intent(getApplicationContext(), VoiceLaunchService.class));
setContentView(R.layout.no_internet_connection);
Toast.makeText(getApplicationContext(), "Not connected to internet. Cannot display the map.", Toast.LENGTH_LONG).show();
}
}
private void setUpViews() {
map = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
}
步骤来重现错误:
第一步:与连接到互联网设备启动应用程序。地图可见 第2步:从互联网断开设备(应用程序发送到后台)。现在,该应用程序显示“无互联网连接”页面(应用程序被带到前面)。 第3步:现在再次连接到互联网。下面的错误显示如下
java.lang.RuntimeException:无法恢复活动。 android.view.InflateException:二进制XML文件行#2:错误充气类片段如下图所示
<?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"
android:name="com.google.android.gms.maps.MapFragment"/>
我的布局文件,我不知道我要去的地方错了。我已经浏览了本网站上的所有解决方案,但徒劳无功。任何帮助将不胜感激
答
扩展您的活动与FragmentActivity
- YourActivity延伸FragmentActivity
2.Change setUpViews()方法来
private void setUpViews() {
map = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
}
检查网络连接和做的工作HTTP ://*.com/questions/4238921/detect-whether-there-is-an-internet-connection-available-on-android – 2014-09-01 10:33:29
请发布** ent ire **堆栈跟踪。 – CommonsWare 2014-09-01 10:36:42
你的Fragment xml布局有些问题。 plz发布您的Fragment xml布局或片段类 – 2014-09-01 10:36:52