Android parse xml to get control of applications, but I always use the controls reported a null pointer exception, when this phenomenon is sometimes no previous clean look, no problem, now clean of no use, do not know this encounter The question is not, please advise the reason for this phenomenon and solutions.<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Wood has initialized, right?
Reply:
Like android is doing so, the release of some weak when not enough memory variables, so you have to do this process in their own applications inside, such as when they are onresume oncreate and determine what, if it is empty on the redistribution
Reply:
+1
Reply:
+1 Oncreate it does not need to determine, directly obtained
Because if carried oncreate, then this will no longer exist Activity
Reply:
Posted log information look at which an object is empty, and then add the appropriate code in front of non-empty judgment, it can be almost
Reply:
Your answers came justified Thank you in advance.
Every time I would perform onCreate method (mean anomaly lies in onCreate in a) to obtain a reference to the control by findViewById method, but get less, why?
xml should by + id, the compiler also passed, but when this control is running on empty reported abnormal, is it because the memory is not enough, not directly to the allocated memory?
Reply:
Set a breakpoint test!
Reply:
You are before setContentView (R.layout.main) on or after obtaining control before if it is in, there will be such a problem
Reply:
Of course, after the
Simply put, is the first findViewById get a reference, then, mistakes, says null.
For example:
setContentView (R.layout.main)
Button button = (Button) this.findViewById (R.id.buttonId)
button.setText () like
Error
This line button.setText () error, said empty. A button empty.
Reply:
There are no parameters?
Reply:
That's the shorthand button.setText ("aaaaaaaaaaaaaaaaaaaaa")
Reply:
Xml may be your problem, there are not that look R.java ID
Reply:
Oh, no, this should be no problem to write ah. You are not in the Button button = (Button) after this.findViewById (R.id.buttonId) has defined a Button, leading to the back of the button with the Times null pointer error, if not, then just look at R file again and see not have this Id. ---- See individual.
Reply:
ID certainly did not find it, search it and see into the R.java
Reply:
id have ah, I read, say and if not, certainly not by the compiler, the entire Activity on this one Button.
Reply:
Your this button with your this layout is separated, meaning that there are no main.xml your button, but the button layout inside the other. Assignment will not display properly, so when you assign a null pointer exception occurs
Reply:
You say this is basically wrong, thank you very much.
I used LayoutInflater to load a layout, and my Button is in this layout, rather than my Activity loaded in the layout.
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.layout_inflater_demo);
LayoutInflater inflater = (LayoutInflater) this.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate (R.layout.defined_dialog, null);
testButton = (Button) view.findViewById (R.id.definedDialog_test_button_id);
testButton.setOnClickListener (this);
}
Before testButton = (Button) view.findViewById (R.id.definedDialog_test_button_id);
Written testButton = (Button) this.findViewById (R.id.definedDialog_test_button_id); therefore wrong.
Reply:
The landlord did not solve? I have also appeared in the same issue
No comments:
Post a Comment