Ich bin wirklich besorgt über die .findViewById()
-Methode und ihre Verwendung in einer benutzerdefinierten Compound-Ansicht erstellen. Ich bin mir nicht sicher über den genauen Ort, an dem es garantiert ist, dass es nie wieder null
.Benutzerdefinierte zusammengesetzte Ansicht und .findViewById()
Sagen wir, ich diese benutzerdefinierte Verbindung Ansicht haben und dass es einige .xml
wie folgt hinzugefügt:
<com.app.path.TwoButtonsView
android:id="@+id/ok_cancel_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
two_buttons_view.xml
:
<?xml version="1.0" encoding="utf-8"?>
<merge>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/first_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:clickable="true"
android:gravity="center"
android:textAllCaps="true"/>
<TextView
android:id="@+id/second_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:clickable="true"
android:gravity="center"
android:textAllCaps="true"/>
</LinearLayout>
</merge>
TwoButtonsView.java
public class TwoButtonsView extends LinearLayout {
// views
private TextView mFirstButtonView;
private TextView mSecondButtonView;
public TwoButtonsView(Context context) {
super(context);
init(context, null);
}
public TwoButtonsView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public TwoButtonsView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public TwoButtonsView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.two_buttons_view, this);
// retrieve views
mFirstButtonView = (TextView) findViewById(R.id.first_button); // is there a chance it will return null?
mSecondButtonView = (TextView) findViewById(R.id.second_button); // is there a chance it will return null?
}
}
Meine Frage ist: Gibt es eine Chance, dass .findViewById(RESOURCE_ID)
null
direkt nach dem Aufruf inflater.inflate(R.layout.two_buttons_view, this);
zurückgibt, oder sollte ich meine init()
Methode auf onFinishInflate()
Rückruf aufrufen?
Vielen Dank für Ihre Antwort, @Blackbelt abrufen! Können Sie mir bitte erklären, was der Zweck von 'onFinishInfllate()' dann ist, bitte? –
Es ist ein Callback, der einen Aufruf erhält, wenn alle untergeordneten Elemente im Layout hinzugefügt wurden. In Ihrem Fall fügen Sie manuell – Blackbelt
Nochmals vielen Dank für die Antwort: D. Eine letzte Frage: Sie haben gesagt, dass ich sie manuell hinzugefügt habe, aber was ist der andere Weg, sie "nicht manuell" hinzuzufügen? Ich konnte an keine =/ –