New android4.4.2 project's layout in the res directory becomes fragment_main.xml and activity_main.xml two files In MainActivity, the overridden method can have, as follows:
public class MainActivity extends ActionBarActivity {
/ ** * A placeholder fragment containing a simple view. * / public static class PlaceholderFragment extends Fragment {
if (savedInstanceState == null) { getSupportFragmentManager (). beginTransaction () . Add (R.id.container, new PlaceholderFragment ()) commit ();. }
}
@ 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); } }
Now I have a problem that I fragment_main.xml Button component initialization file will be reported under the null pointer in the onCreate method exception Wrote in setContentView (R.layout.activity_main); below.
btn = (Button) findViewById (R.id.btn); btn.setOnClickListener (new OnClickListener () { @ Override public void onClick (View v) { / / TODO Auto-generated method stub Toast.makeText (MainActivity.this, "click on the button", Toast.LENGTH_LONG); }
});
If I change my btn = (Button) findViewById (R.id.btn);'s position, change to super.onCreate (savedInstanceState); front, will be reported: requestFeature () must be called before adding content errors. Why is this? I should write a component initialization position where? Solving ~ ~ ~
<-! Baidu Button BEGIN -> more Share to: <-! Baidu Button END ->
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) -> Reply: lz posted the complete code Reply:
btn = (Button) findViewById (R.id.btn); btn.setOnClickListener (new OnClickListener () {
@ Override public void onClick (View v) { / / TODO Auto-generated method stub Toast.makeText (MainActivity.this, "click on the button", Toast.LENGTH_LONG); }
});
if (savedInstanceState == null) { getSupportFragmentManager (). beginTransaction () . Add (R.id.container, new PlaceholderFragment ()) commit ();. }
}
@ 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); } }
Reply: Button button is not equal to null it .... Reply: I do not know lz not solve the problem, I do not have to deploy your code, I use 4.4, not ActionBarActivity, but the landlord said I should know where the problem lies null pointer
setContentView (R.layout.activity_main);
btn = (Button) findViewById (R.id.btn);
Layout file in onCreate method you give ActionBarActivity settings is activity_main, and the button layout is not in this document, so here is out of reach, there is little you use the Fragment, this is the increase in Android3, and your AndroidManifest.xml file:
Find the problem, lz can try to modify this implementation, in other places to go to get the Button, for instance initialization Fragment, but to call attention to the order of, I do not know lz lz can help solve the problem. Reply: Landlord variables you define two layouts confusion
btn = (Button) findViewById (R.id.btn);
You try to change the next button layout Reply:
Great God, the phrase ~ seeking advice, I would like to know which parts of the knowledge you need to know so I can understand the problem with this call sequence. Question exactly as you said, I just do not understand the configuration files and layout files, and the calling sequence MainActivity. There is how to build the project can not inherit ActionBarActivity, before the establishment of the project when the class is not inherited Activity, no two layout files. Now there are two, why? Reply:
Well - a lot of online search, they say that there is a call to order layout problem, but I do not know how to solve it. Button code to initialize static method written in the words that would not be an error, but the event does not respond. Continue debugging. . . Reply:
In fact, this does not affect. Reply:
This is the latest ADT modify the place, the previous version of the default build of Android Activity project is inherited Activity, nor engage in so complicated, I have not updated this, did not do it, when relatively fast method recommended lz Activity by modifying a version prior to the latest version of the sdk and adt, or manually, and I hope to help you ~ Reply:
I do not know lz not solve the problem, I do not have to deploy your code, I use 4.4, not ActionBarActivity, but the landlord said I should know where the problem lies null pointer
setContentView (R.layout.activity_main);
btn = (Button) findViewById (R.id.btn);
Layout file in onCreate method you give ActionBarActivity settings is activity_main, and the button layout is not in this document, so here is out of reach, there is little you use the Fragment, this is the increase in Android3, and your AndroidManifest.xml file:
Find the problem, lz can try to modify this implementation, in other places to go to get the Button, for instance initialization Fragment, but to call attention to the order of, I do not know lz lz can help solve the problem.
Great God, the phrase ~ seeking advice, I would like to know which parts of the knowledge you need to know so I can understand the problem with this call sequence. Question exactly as you said, I just do not understand the configuration files and layout files, and the calling sequence MainActivity. There is how to build the project can not inherit ActionBarActivity, before the establishment of the project when the class is not inherited Activity, no two layout files. Now there are two, why?
This is the latest ADT modify the place, the previous version of the default build of Android Activity project is inherited Activity, nor engage in so complicated, I have not updated this, did not do it, when relatively fast method recommended lz Prior to the latest version with a version of the sdk and adt, or manually modify Activity, I hope to help you ~
I also would like to ask, you know, in
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {}
Is the corresponding "activity_main.xml" in the components of the how to initialize? I do not know I'm asking if you can understand? Anyway, on this post, I was really grateful to you for giving me the inspiration ~ ~ ~ Reply: This will certainly be an error ah. Because btn is fragment_main.xml inside, but you setContentView (R.layout.activity_main); Get btn below, due activity_main.xml only one FrameLayout layout.
So get btn code to write in View rootView = inflater.inflate (R.layout.fragment_main, container, false); Below: btn = (Button) rootView.findViewById (R.id.btn);
I do not know whether lz resolved, I hope you can help.
Reply:
this certainly will complain ah. Because btn is fragment_main.xml inside, but you setContentView (R.layout.activity_main); Get btn below, due activity_main.xml only one FrameLayout layout.
So get btn code to write in View rootView = inflater.inflate (R.layout.fragment_main, container, false); Below: btn = (Button) rootView.findViewById (R.id.btn);
I do not know whether lz resolved, I hope you can help.
Solved, thank you ~ But you can tell me this load order it? fragment_main.xml and activity_main.xml two loaded in the order in which MainActivity. Reply: How did you solve, I'm still stuck in this issue, I still can not find the controls Reply:
How do you solve, I am now stuck in this issue, I still can not find the controls
Components written in fragment_main.xml Lane, Initialization write
public static class PlaceholderFragment extends Fragment {
View rootView = back ......., look at this example. To add this rootView front findViewById Reply: How would become so troublesome after ADT update? What good is it doing? I created the first project where the error after updating the thought of it! Reply:
How did you solve, I'm still stuck in this issue, I still can not find the controls
Components written in fragment_main.xml Lane, Initialization write
public static class PlaceholderFragment extends Fragment {
View rootView = back ......., look at this example. findViewById front to add this rootView
I am also the same problem, ask the landlord what the Settings button in the listening. Reply:
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); Button bt_dial = (Button) rootView.findViewById (R.id.bt_dial); bt_dial.setOnClickListener (new MyListener ()); EditText st_number = (EditText) rootView.findViewById (R.id.st_number); return rootView;
}
}
findViewById not reported null pointer wrong But there is no way to call the method defined in other EditText st_number = (EditText) rootView.findViewById (R.id.st_number); bt_dial.setOnClickListener (new MyListener ()); there is no other way to call a method not know heroes can be difficult to solve this problem, this problem has troubled me for a long time. Reply: I do not know the great God has not been resolved, I also encountered the same problem here, wrote a dialer, but on the phone a test on abnormal exit, unable to perform setOnListener () method Reply: You use Log.i ("->", "1"); Log.i ("->", "2"); such a label to each method, and then run the program once you know that after the first load loaded. Reply: Get fragment_main.xml the controls inside the static class PlaceholderFragment class MainActivity class method in onCreateView
public static class PlaceholderFragment extends Fragment {
No comments:
Post a Comment