2016-07-22 146 views
2

我正在開發一個應用程序,其中我有一個用ScrollView包裝的佈局。在這個佈局的片段中,我打了一個Web服務來從服務器獲取數據並將其顯示在我的佈局中。無論何時出現錯誤或Web服務中沒有可用的數據,我都會向用戶顯示一個小吃棒,並在其中顯示相應的消息。SnackBar導致ScrollView與非法狀態異常崩潰

我有,只有出現非常較少的問題問題。當有時數據不可用,小吃吧顯示,我的應用程序崩潰,並非法狀態異常,顯示消息

ScrollView can host only one direct child

這裏是崩潰日誌

java.lang.IllegalStateException: ScrollView can host only one direct child 
              at android.widget.ScrollView.addView(ScrollView.java:253) 
              at android.support.design.widget.Snackbar.showView(Snackbar.java:475) 
              at android.support.design.widget.Snackbar$1.handleMessage(Snackbar.java:162) 
              at android.os.Handler.dispatchMessage(Handler.java:107) 
              at android.os.Looper.loop(Looper.java:214) 
              at android.app.ActivityThread.main(ActivityThread.java:6102) 
              at java.lang.reflect.Method.invoke(Native Method) 
              at java.lang.reflect.Method.invoke(Method.java:372) 
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028) 
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823) 

我使用在許多這樣的組合因此我只發佈佈局代碼之一的xmls

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:scrollbars="none"> 

    <LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/background" 
     android:orientation="vertical" 
     android:paddingBottom="20dp" 
     android:paddingLeft="20dp" 
     android:paddingRight="20dp" 
     android:paddingTop="10dp"> 

     <com.xsinfosol.emergency.progressbar.RopeProgressBar 
      android:id="@+id/progress_bar1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      app:ropeDynamicLayout="true" 
      app:ropePrimaryColor="@color/red" 
      app:ropeSecondaryColor="#20FFFFFF" 
      app:ropeSlack="0dp" 
      app:ropeStrokeWidth="10dp" /> 

     <ImageView 
      android:id="@+id/iv_default_sos" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:layout_marginTop="40dp" 
      android:src="@drawable/on_light" /> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="20dp" 
      android:background="@drawable/edittext_bg"> 

      <com.xsinfosol.emergency.utils.CustomTextView 
       android:layout_width="30dp" 
       android:layout_height="30dp" 
       android:layout_gravity="center_vertical" 
       android:layout_margin="10dp" 
       android:background="@drawable/number_circle" 
       android:gravity="center" 
       android:text="1" 
       android:textColor="@color/red" 
       android:textSize="22sp" /> 

      <com.xsinfosol.emergency.utils.CustomTextView 
       android:id="@+id/tv_view_health" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_gravity="center_vertical" 
       android:drawableRight="@drawable/medical" 
       android:gravity="center_vertical" 
       android:paddingLeft="10dp" 
       android:paddingRight="10dp" 
       android:text="Health" 
       android:textColor="@android:color/white" /> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:background="@drawable/edittext_bg"> 

      <com.xsinfosol.emergency.utils.CustomTextView 
       android:layout_width="30dp" 
       android:layout_height="30dp" 
       android:layout_gravity="center_vertical" 
       android:layout_margin="10dp" 
       android:background="@drawable/number_circle" 
       android:gravity="center" 
       android:text="2" 
       android:textColor="@color/red" 
       android:textSize="22sp" /> 

      <com.xsinfosol.emergency.utils.CustomTextView 
       android:id="@+id/tv_view_police" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_gravity="center_vertical" 
       android:drawableRight="@drawable/police" 
       android:gravity="center_vertical" 
       android:paddingLeft="10dp" 
       android:paddingRight="10dp" 
       android:text="Police" 
       android:textColor="@android:color/white" /> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:background="@drawable/edittext_bg"> 

      <com.xsinfosol.emergency.utils.CustomTextView 
       android:layout_width="30dp" 
       android:layout_height="30dp" 
       android:layout_gravity="center_vertical" 
       android:layout_margin="10dp" 
       android:background="@drawable/number_circle" 
       android:gravity="center" 
       android:text="3" 
       android:textColor="@color/red" 
       android:textSize="22sp" /> 

      <com.xsinfosol.emergency.utils.CustomTextView 
       android:id="@+id/tv_view_fire" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_gravity="center_vertical" 
       android:drawableRight="@drawable/fire" 
       android:gravity="center_vertical" 
       android:paddingLeft="10dp" 
       android:paddingRight="10dp" 
       android:text="Fire" 
       android:textColor="@android:color/white" /> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:background="@drawable/edittext_bg"> 

      <com.xsinfosol.emergency.utils.CustomTextView 
       android:layout_width="30dp" 
       android:layout_height="30dp" 
       android:layout_gravity="center_vertical" 
       android:layout_margin="10dp" 
       android:background="@drawable/number_circle" 
       android:gravity="center" 
       android:text="4" 
       android:textColor="@color/red" 
       android:textSize="22sp" /> 

      <com.xsinfosol.emergency.utils.CustomTextView 
       android:id="@+id/tv_view_no_4" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_gravity="center_vertical" 
       android:drawableRight="@drawable/contact_person" 
       android:gravity="center_vertical" 
       android:paddingLeft="10dp" 
       android:paddingRight="10dp" 
       android:text="Contact Person 1" 
       android:textColor="@android:color/white" /> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:background="@drawable/edittext_bg"> 

      <com.xsinfosol.emergency.utils.CustomTextView 
       android:layout_width="30dp" 
       android:layout_height="30dp" 
       android:layout_gravity="center_vertical" 
       android:layout_margin="10dp" 
       android:background="@drawable/number_circle" 
       android:gravity="center" 
       android:text="5" 
       android:textColor="@color/red" 
       android:textSize="22sp" /> 

      <com.xsinfosol.emergency.utils.CustomTextView 
       android:id="@+id/tv_view_no_5" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_gravity="center_vertical" 
       android:drawableRight="@drawable/contact_person" 
       android:gravity="center_vertical" 
       android:paddingLeft="10dp" 
       android:paddingRight="10dp" 
       android:text="Contact Person 2" 
       android:textColor="@android:color/white" /> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:background="@drawable/edittext_bg"> 

      <com.xsinfosol.emergency.utils.CustomTextView 
       android:layout_width="30dp" 
       android:layout_height="30dp" 
       android:layout_gravity="center_vertical" 
       android:layout_margin="10dp" 
       android:background="@drawable/number_circle" 
       android:gravity="center" 
       android:text="6" 
       android:textColor="@color/red" 
       android:textSize="22sp" /> 

      <com.xsinfosol.emergency.utils.CustomTextView 
       android:id="@+id/tv_view_no_6" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_gravity="center_vertical" 
       android:drawableRight="@drawable/contact_person" 
       android:gravity="center_vertical" 
       android:paddingLeft="10dp" 
       android:paddingRight="10dp" 
       android:text="Contact Person 3" 
       android:textColor="@android:color/white" /> 
     </LinearLayout> 

    </LinearLayout> 
</ScrollView> 

這是我通過api

public void parseSOSResponse(String response) { 
    try { 
     JSONObject jsonObject = new JSONObject(response); 
     String Message=jsonObject.optString("Message"); 
     int error_code = jsonObject.getInt("error_code"); 
     if (error_code == 1) { 
      String num_4 = jsonObject.getString("num_4"); 
      String num_5 = jsonObject.getString("num_5"); 
      String num_6 = jsonObject.getString("num_6"); 
      String health_number=jsonObject.getString("health_number"); 
      String fire_number=jsonObject.getString("fire_number"); 
      String police_number=jsonObject.getString("police_number"); 
      Profile_complete=jsonObject.getInt("Profile_complete"); 
      default_sos = jsonObject.getString("default_sos"); 
      tv_view_no_4.setText(num_4); 
      tv_view_no_5.setText(num_5); 
      tv_view_no_6.setText(num_6); 
      tv_view_fire.setText("Fire \n"+fire_number); 
      tv_view_police.setText("Police \n"+police_number); 
      tv_view_health.setText("Health \n"+health_number); 
      progress_bar1.setMax(100); 
      progress_bar1.animate(); 
      progress_bar1.animateProgress(Profile_complete); 
      prefManager.putString(PrefrenceConstants.KEY_EMERGENCY_TYPE,default_sos); 
     }else{ 
      ((ActivityNavigationDrawer)getActivity()).showSnackBar(iv_default_sos,Message); 
     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
} 

showSnackbar方法

public void showSnackBar(View view, String message) { 
    Snackbar.make(view, message, Snackbar.LENGTH_SHORT).show(); 
} 
+0

顯示您的代碼添加SnackBar的位置。 – Drv

+0

@Drv添加了相關代碼 –

+0

將您的整個代碼添加到活動中您的showSnackBar方法在哪裏? – Drv

回答

0

正如Marcelo Noguti建議將Scrollview放入 a Coordinator Layout並將其作爲小吃棒方法中的視圖傳遞給我解決了這個問題。

0

嘗試用寬度和高度設置爲包裹包裝內容與LinearLayout中孩子解析數據的方法。由於這部分

<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/background" 
    android:orientation="vertical" 
    android:paddingBottom="20dp" 
    android:paddingLeft="20dp" 
    android:paddingRight="20dp" 
    android:paddingTop="10dp"> 
4

的問題是,ScrollViews只能有一個直接的孩子,在你的代碼這個孩子是outter線性佈局。當小吃店被要求顯示時,我想你要求小吃店被畫的視圖是滾動視圖本身,所以會有兩個直接的孩子(你的線性佈局和現在的小吃店的視圖)

+0

我知道,但作爲快餐欄將僅顯示在出現錯誤的情況下,它也是動態添加的。也不是每次都發生 –

+1

好吧,明白。那麼,我建議你改變快餐欄的視圖,因爲這是一個簡單的改變 - 並將其放在協調器佈局中,遵循文檔 –