About activity_main.xml and fragment_main.xml, activity_main.xml
Solution 1
The new version of the SDK has two XML files, activity_main.xml and fragment_main.xml,
You can do this if you are not used to it:
1. Delete the entire fragment_main.xml File
2. Delete the content in activity_main.xml. Switch to Graphy Layout and put a LinearLayout.
3. For MainActivity. java, you can delete some content and change MainActivity extends ActionBarActivity to MainActivity extends Activity:
The change is as follows:
Package com. zmcorp. weightcalculator;
Import android. OS. Bundle;
Import android. view. LayoutInflater;
Import android. view. Menu;
Import android. view. MenuItem;
Import android. view. View;
Import android. view. ViewGroup;
Import android. OS. Build;
MainActivity extends Activity {
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
}
@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
// Inflate the menu; this adds items to the action bar if it is present.
GetMenuInflater (). inflate (R. menu. main, menu );
Return true;
}
@ Override
Public boolean onOptionsItemSelected (MenuItem item ){
// Handle action bar item clicks here. The action bar will
// Automatically handle clicks on the Home/Up button, so long
// As you specify a parent activity in AndroidManifest. xml.
Int id = item. getItemId ();
If (id = R. id. action_settings ){
Return true;
}
Return super. onOptionsItemSelected (item );
}
}
}
Method 2 ::
MyTextview and myButton are in fragment_main.xml. Before fragment_main.xml is introduced, you cannot find them through findViewById ..
Public static class PlaceholderFragment extends Fragment {
Public PlaceholderFragment (){
}
@ Override
Public View onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState ){
View rootView = inflater. inflate (R. layout. fragment_main, container,
False );
TextView textview = (TextView) rootView. findViewById (R. id. view );
System. out. println (textview );
Testview. setText ("Hello Android! ");
Return rootView;
}
}
For new Android users, fragment_mainxml and Activity_mainxml
SetContentView (R. layout. fragment_main)
In the latest android version, how does one operate components in fragment_mainxml in MainActivityjava?
You can write this in the onStart () method.
Fragment is the fragment object added to your activity. button = (Button) fragment. getView (). findViewById (R. id. button1 );
Button. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
}
});
You will not report an error.