I have two layout xml file, Main.xml, a button of Android name: id = "@ + id / BtnClose ", and about.xml also has a button named Android: id =" @ + id / btnclose ", is that OK? Thanks!
main.xml about.xml it can be the same Illusion / ambiguity Avoid but it is better to have different IDs as suggested by BlackBelt. You can set the current view for hierarchy of the activity. So if you have the same ID in the different XML layout it's okay. If the code you have You are currently visible can ViewById the hierarchy main.xml If you have below In both the above cases, both have code containing the button with Assume that you get an ID You will get
& lt; Relative layout xmlns: android = "http://schemas.android.com/apk/res / Android" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: paddingTop = "3dip" Android: layout_marginTop = "3dip "Android: Background =" # DCDCDC "& gt; & Lt; Button android: id = "@ + id / btnClose" style = "@ style / MyTextAppearance" Android: Layout_width = "Wrap_content" Android: Layout_height = "Wrap_content" Android: layout_alignParentRight = "true" android: text = "@ string / out Exit "/> & Lt; / RelativeLayout & gt;
& lt; LinearLayout xmlns: android = "http://schemas.android.com/apk/sh/Android" xmlns: Advertisement = "http://schemas.android.com/apk/lib/com.google.ads" Android: layout_width Android: layout_height = "fill_parent" Android: paddingTop = "3dip" Android: paddingLeft = "7dip" Android: background = "@ line / boundary_i" Android: orientation = "vertical" & gt; & Lt; Button android: id = "@ + id / btnClose" Android: Layout_width = "Wrap_content" Android: Layout_height = "Wrap_content" Android: Layout_gravity = "center" style = "@ style / MyTextAppearance" android: text = "@ string / Marryren "/> & Lt; / LinearLayout & gt;
down <> setContentView (R.layout.main) button b = (Button) findViewById (R.id.btnClose); // start button
< Code> setContentView (R.layout.about); Button B = (button) Find VVBIID (RID. BTN close); // initialize button
main.xml and
about.xml < Close the code> @ + ID / BTNC
@ + id / button2 in
about.xml and another The button is down
setContentView (R.layout.main); Button B = (button) Find VVBIID (R.B. button2);
NullPointerException because your current view is set to the hierarchy activity
main.xml not
about.xml . There is no button in the
main.xml with ID
button2 .
No comments:
Post a Comment