Sunday, February 16, 2014

Android null pointer problem


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:
reference to the second floor cdbdyx reply:
like android is doing so on the release of some weak variables when memory is not enough, so you have to yourself in your own application which make this process, such as when they are onresume oncreate and determine what, if it is empty on redistribution

+1
Reply:
reference to the third floor fengli3863 reply:
reference to the second floor cdbdyx 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 of

+1

+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:
reference to the second floor cdbdyx reply:
like android is doing so on the release of some weak variables when memory is not enough, so you have to yourself in your own application which make this process, such as when they are onresume oncreate and determine what, if it is empty on redistribution

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:
reference to the 8th floor jingmy reply:
you are to get control before or after setContentView (R.layout.main), if it is before, there will be Such problems

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:
reference to the 10th floor mahaiyun18 reply:
there is no argument?

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:
reference to the 12th floor rocky225 reply:
may be a problem with your xml look R.java there was no such ID

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:
reference to the 16th floor weixuejun891229 reply:
you this button with your layout is separate from this, meaning that your main.xml There is no button, but The button layout inside the other. Assignment will not display properly, so when you assign a null pointer exception occurs

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