2015-03-10 2 views
6

Я пытаюсь написать несколько тестов для пользовательского представления, но у меня возникают проблемы с раздуванием пользовательского представления в моем тестовом примере. Ошибки я получаюRobolectric раздувает пользовательские представления для тестов

android.view.InflateException: <merge /> can be used only with a valid ViewGroup root and attachToRoot=true 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:458) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
    at com.androidas.models.ui.order.MyLocationViewTest.setUp(MyLocationViewTest.java:45) 

Я даже попытался сделать фиктивную деятельность (TestActivity) с меткой в ​​нем, чтобы попытаться раздуть его

public class MyLocationView extends RelativeLayout { 

private TextView mTextEta; 
private TextView mTextOnTime; 
private Date mMeetTime; 
private CalendarManager mCalendarManager; 



public MyLocationView(Context context) { 
    this(context, null); 
} 

public MyLocationView(Context context, AttributeSet attrs) { 
    this(context, attrs, 0); 
} 

public MyLocationView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 

    LayoutInflater.from(context) 
      .inflate(R.layout.v_mylocation, this, true); 

    mTextEta = (TextView) findViewById(R.id.order_statusTracking_txt_myEta); 
    mTextOnTime = (TextView) findViewById(R.id.order_statusTracking_txt_myDelay); 

} 
} 

раскладку

<?xml version="1.0" encoding="utf-8"?> 
<merge xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
> 


<ImageView 
    android:id="@+id/order_statusTracking_img_walking" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/ic_walk" 
    /> 

<TextView 
    android:id="@+id/order_statusTracking_txt_myEta" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="@dimen/spacing_small" 
    android:layout_toRightOf="@+id/order_statusTracking_img_walking" 
    android:layout_toEndOf="@+id/order_statusTracking_img_walking" 
    android:gravity="center_vertical" 
    tools:text="Arrive in 7min" 
    style="@style/HeaderText" 
    /> 

<TextView 
    android:id="@+id/order_statusTracking_txt_myDelay" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/order_statusTracking_txt_myEta" 
    android:layout_below="@+id/order_statusTracking_txt_myEta" 
    android:layout_marginTop="@dimen/spacing_normal" 
    tools:text="On Time" 
    style="@style/InfoText.Black" 
    /> 
</merge> 

и тестовый пример :

@RunWith(RobolectricTestRunner.class) 
@Config(emulateSdk = 18) 
public class MyLocationViewTest { 

MyLocationView mLocation; 
TextView mTextDelay; 
Trip mTrip; 
CalendarManager mCalendarManager; 
Date meetupTime; 


@Before 
public void setUp() { 


    Activity activity = Robolectric.buildActivity(TestActivity.class).create().get(); 
    mLocation = (MyLocationView) LayoutInflater.from(activity).inflate(R.layout.v_mylocation, null); 
    mTextDelay = (TextView) mLocation.findViewById(R.id.order_statusTracking_txt_myDelay); 
} 
+0

Вы получили решение для этого? –

ответ

5

ОК, 1 y ухо позже, но вот странная вещь, которая сработала для меня:

MyCustomView mView = (MyCustomView) LayoutInflater 
     .from(RuntimeEnvironment.application) 
     .inflate(R.layout.my_custom_view_layout, 
       new MyCustomView(RuntimeEnvironment.application), 
       true); 
Смежные вопросы