これらのコンストラクターを使用して、LinearLayoutを拡張するカスタムビューがあります。
public class CustomView extends LinearLayout {
public CustomView(Context context) {
this(context, null);
}
public CustomView(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.customViewStyle);
}
public CustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
/* set custom attributes with TypedArray */
}
/* other methods */
}
私の/res/values/attrs.xml:
<attr name="customViewStyle" format="reference" />
<declare-styleable name="CustomeView">
<attr name="value" format="integer" />
<attr name="android:imeOptions" />
<attr name="android:imeActionId" />
<attr name="android:imeActionLabel" />
</declare-styleable>
私の/res/values/styles.xml:
<style name="AppTheme" parent="@android:style/Theme.Black">
<item name="android:windowBackground">@drawable/bg_dark_fiber</item>
<item name="customViewStyle">@style/CustomViewStyle</item>
</style>
<style name="CustomViewStyle">
<item name="android:clickable">true</item>
<item name="android:focusable">true</item>
<item name="android:focusableInTouchMode">true</item>
</style>
AppThemeは、アプリケーション全体のテーマとして設定されます。望ましい結果は、カスタムビューがデフォルトでフォーカス可能でクリック可能であり、レイアウトファイルにこれらの属性を追加する必要がないことです(通常のLinearLayoutは追加されません)。アプリを実行すると、次のスタックトレースでクラッシュします。
05-17 14:58:53.010: E/ActivityThread(26767): Failed to inflate
05-17 14:58:53.010: E/ActivityThread(26767): android.view.InflateException: Binary XML file line #8: Error inflating class com.example.customviews.CustomView
05-17 14:58:53.010: E/ActivityThread(26767): at android.view.LayoutInflater.createView(LayoutInflater.java:518)
05-17 14:58:53.010: E/ActivityThread(26767): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
05-17 14:58:53.010: E/ActivityThread(26767): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
05-17 14:58:53.010: E/ActivityThread(26767): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
05-17 14:58:53.010: E/ActivityThread(26767): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
05-17 14:58:53.010: E/ActivityThread(26767): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
05-17 14:58:53.010: E/ActivityThread(26767): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:212)
05-17 14:58:53.010: E/ActivityThread(26767): at android.app.Activity.setContentView(Activity.java:1657)
05-17 14:58:53.010: E/ActivityThread(26767): at com.example.pincode.PinCodeActivity.onCreate(PinCodeActivity.java:26)
05-17 14:58:53.010: E/ActivityThread(26767): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-17 14:58:53.010: E/ActivityThread(26767): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
05-17 14:58:53.010: E/ActivityThread(26767): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
05-17 14:58:53.010: E/ActivityThread(26767): at android.app.ActivityThread.access$1500(ActivityThread.java:123)
05-17 14:58:53.010: E/ActivityThread(26767): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
05-17 14:58:53.010: E/ActivityThread(26767): at android.os.Handler.dispatchMessage(Handler.java:99)
05-17 14:58:53.010: E/ActivityThread(26767): at android.os.Looper.loop(Looper.java:130)
05-17 14:58:53.010: E/ActivityThread(26767): at android.app.ActivityThread.main(ActivityThread.java:3835)
05-17 14:58:53.010: E/ActivityThread(26767): at java.lang.reflect.Method.invokeNative(Native Method)
05-17 14:58:53.010: E/ActivityThread(26767): at java.lang.reflect.Method.invoke(Method.java:507)
05-17 14:58:53.010: E/ActivityThread(26767): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
05-17 14:58:53.010: E/ActivityThread(26767): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
05-17 14:58:53.010: E/ActivityThread(26767): at dalvik.system.NativeStart.main(Native Method)
05-17 14:58:53.010: E/ActivityThread(26767): Caused by: java.lang.reflect.InvocationTargetException
05-17 14:58:53.010: E/ActivityThread(26767): at java.lang.reflect.Constructor.constructNative(Native Method)
05-17 14:58:53.010: E/ActivityThread(26767): at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
05-17 14:58:53.010: E/ActivityThread(26767): at android.view.LayoutInflater.createView(LayoutInflater.java:505)
05-17 14:58:53.010: E/ActivityThread(26767): ... 21 more
05-17 14:58:53.010: E/ActivityThread(26767): Caused by: java.lang.NoSuchMethodError: android.widget.LinearLayout.<init>
05-17 14:58:53.010: E/ActivityThread(26767): at com.example.customviews.CustomView.<init>(CustomView.java:98)
05-17 14:58:53.010: E/ActivityThread(26767): at com.example.customviews.CustomView.<init>(CustomView.java:94)
05-17 14:58:53.010: E/ActivityThread(26767): ... 24 more
の呼び出しでInflateExceptionが発生している原因がわかりませんsuper(context, attrs, defStyle)
。私も試しsuper(context, attrs, 0)
ましたが、それは役に立ちません。これは、LinearLayoutでさえ、ソースコードを調べた他のほとんどのビューと同様に、それを使用しているため、本当に奇妙です。