2014-09-01 135 views
0

我根據網絡狀態加載不同的視圖。當網絡可用時,我正在加載一個片段,當網絡不可用時,我正在加載一個圖像。的代碼如下所示錯誤: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"/> 

我的佈局文件,我不知道我要去的地方錯了。我已經瀏覽了本網站上的所有解決方案,但徒勞無功。任何幫助將不勝感激

+0

檢查網絡連接和做的工作HTTP ://stackoverflow.com/questions/4238921/detect-whether-there-is-an-internet-connection-available-on-android – 2014-09-01 10:33:29

+0

請發佈** ent ire **堆棧跟蹤。 – CommonsWare 2014-09-01 10:36:42

+0

你的Fragment xml佈局有些問題。 plz發佈您的Fragment xml佈局或片段類 – 2014-09-01 10:36:52

回答

-1

擴展您的活動與FragmentActivity

  1. YourActivity延伸FragmentActivity

2.Change setUpViews()方法來

private void setUpViews() { 
map = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); 
} 
相關問題