У меня есть активность Допустим, A
это определено в данном приложении и это соответствующий манифест. Это действие загружает contentView, который он просто загружает через статический индекс R. Допустим, R.layout.foo. В этом макете есть компонент, который смотрит на что-то, что не является базовым атрибутом Android. Я вижу, что когда тестовое приложение запускает это действие, тема и стили внутри темы не заполняются ничем.
Образец манифеста
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.foo.bar"
android:versionCode="1"
android:versionName="Test"
android:installLocation="auto">
<uses-sdk android:minSdkVersion="3" android:targetSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="@drawable/app_icon"
android:label="@string/app_name"
android:description="@string/description"
android:theme="@style/Theme.Custom"
android:name=".MyApplicationObject">
<activity android:name=".activity.A"/>
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:anyDensity="true" />
</manifest>
Задание A
public class A extends Activity {
public void onCreate(Bundle a) {
setContentView(R.layout.foo);
}
}
Макет, foo.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<com.foo.bar.CustomView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
CustomView
public class CustomView extends RelativeLayout {
public CustomView(Context context, AttributeSet set) {
this(context, set, R.attr.CustomViewStyle);
}
public CustomView(Context c, AttributeSet set, int defStyle) {
super(c, set, defStyle);
TypedArray array = c.obtainStyledAttributes(set, R.styleable.CustomViewAttrs, defStyle, defStyle);
final int layout = array.getResourceId(R.styleable.CustomViewAttrs_layout, 0);
final Drawable icon = array.getDrawable(R.styleable.CustomViewAttrs_icon);
array.recycle();
if (layout == null) {
throw new IllegalStateException("WTF");
}
if (icon == null) {
throw new IllegalStateException("For real, WTF");
}
}
}
Некоторые ресурсы в файле значений
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Used to define a namespace for our special types -->
<declare-styleable name="CustomTypes">
<attr name="CustomViewStyle" format="reference"/>
</declare-styleable>
<!-- Used to define Attributes specific to our new View, "CustomView" -->
<declare-styleable name="CustomViewAttrs">
<attr name="layout" format="reference"/>
<attr name="anotherOne" format="reference"/>
</declare-styleable>
<!-- A usable style that we can reference later to pass to an instance of CustumView -->
<style name="CustomView">
<item name="layout">@layout/foo</item>
<item name="AnotherOne">@drawable/icon</item>
</style>
<!-- A Style to act as our Theme, referenced in our Manifest as the Theme for all activities -->
<style name="Theme.Custom" parent="android:Theme">
<item name="CustomViewStyle">@style/CustomView</item>
</style>
<resources>
Это работает нормально, но когда я использую ActivityUnitTest для загрузки экземпляра A, значения внутри TypedArray пусты.
Некоторые тестовые классы
public class ActivityTester extends ActivityUnitTestCase<A> {
public ActivityTester() {
super(A.class);
}
public void testOne() {
Intent intent = new Intent(getInstrumentation().getTargetContext(), A.class);
// This fails with my IllegalStateException
startActivity(intent, null, null);
}
}
Есть идеи, как / если целевое приложение получило его разбор? Кажется, тема даже не загружается. Документация для startActivity () гласит, что она начнет действие так же, как и context.startActivity (). Я не вижу, чтобы это происходило, так как это не соответствует манифестным данным о деятельности.