Ich habe eine Bibliothek erstellt, die ich in mein Projekt einbeziehen möchte.Benutzerdefiniertes Attribut, das null zurückgibt
Darin habe ich dies in meinem attr.xml:
<declare-styleable name="MyCustomView">
<attr name="MyPrivateID" format="string"/>
</declare-styleable>
ich die Bibliothek in meiner App enthalten, und es funktioniert gut. Aber ich kann mein benutzerdefiniertes Attribut nicht abrufen.
Das ist mein activity_main.xml ist
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
...........>
<something.myown.MyCustomView
android:layout_width="match_parent"
android:layout_height="wrap_content"
custom:MyPrivateID="GjM4X9QfK2VT05BKlEyffkAKGLU=dhbdsljhbdDjTchvNTQwMg=="/>
Und in MyCustomView.java, ich habe:
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyCustomView);
try {
String string = a.getString(R.styleable.MyCustomView_MyPrivateID);
return string;
} finally {
a.recycle();
}
Irgendwie String kehrt als null. Jede Hilfe wird geschätzt.
Geändert, das ist, was der Code, den ich lief, war –
Versuchen Sie, 'MyPrivateID'-Wert im Konstruktor zu bekommen? – betorcs
Ja? Wäre das ein Problem? –