我有一个片段,其布局由自定义视图(MyCanvasView
)和一个按钮组成。我正在尝试通过MyCanvasView
类中的getter方法访问变量。我可以访问创建OnClickListener
的按钮,但是尝试以相同的方式访问自定义视图无效。 vMapView
仍为null
。如何从包含它的片段中访问自定义视图?
在片段Java文件中:
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.select_anchor, container, false);
Button btnSubmitAnchor = (Button) v.findViewById(R.id.btn_submit_anchor);
MyCanvasView vMapView = (MyCanvasView) v.findViewById(R.id.anchor_selection_view);
btnSubmitAnchor.setOnClickListener((view) - > {
receivedAnchor = vMapView.getTempAnchor();
try {
Log.i(TAG, "Received anchor:\n" + receivedAnchor.getName());
} catch (Exception e) {
Log.i(TAG, "no anchor was received");
}
});
return v;
}
在MyCanvasView.java中:
public Anchor getTempAnchor() {
return tempAnchor;
}
在select_anchor.xml中:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<view
android:id="@+id/anchor_selection_view"
class="com.decawave.argomanager.ui.view.MyCanvasView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
/>
<Button
android:id="@+id/btn_submit_anchor"
style="@style/MtrlBorderlessButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_margin="5dp"
android:text="@string/btn_submit_anchor" />
</RelativeLayout>
答案 0 :(得分:0)
这样尝试:
<com.decawave.argomanager.ui.view.MyCanvasView
android:id="@+id/anchor_selection_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
/>