Friday, February 28, 2014

Consult the android on View: onClick problem


In the following add android: onClick always wrong
 android: id = "@ + id / txtsex" 
android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
android: layout_weight = "1"
android: ems = "10"
android: text = ""
android: onClick = "setSex" />

Error message:
 
java.lang.NullPointerException
at android.widget.AutoCompleteTextView $ PassThroughClickListener.access $ 302
at android.widget.AutoCompleteTextView.setOnClickListener
at android.view.View.
at android.widget.TextView.
at android.widget.EditText.
at android.widget.AutoCompleteTextView.
at android.widget.AutoCompleteTextView.
at java.lang.reflect.Constructor.newInstance
at android.view.LayoutInflater.createView
at android.view.LayoutInflater.onCreateView
at android.view.LayoutInflater.createViewFromTag
at android.view.LayoutInflater.rInflate_Original
at android.view.LayoutInflater_Delegate.rInflate
at android.view.LayoutInflater.rInflate
at android.view.LayoutInflater.rInflate_Original
at android.view.LayoutInflater_Delegate.rInflate
at android.view.LayoutInflater.rInflate
at android.view.LayoutInflater.rInflate_Original
at android.view.LayoutInflater_Delegate.rInflate
at android.view.LayoutInflater.rInflate
at android.view.LayoutInflater.inflate
at android.view.LayoutInflater.inflate

Function
corresponding java page
 public void setSex (View view) 
{
final EditText txtsex = (EditText) this.findViewById (R.id.txtsex);
String [] strSex = getResources () getStringArray (R.array.sex);.
AlertDialog.Builder builder;
builder = new AlertDialog.Builder (this);
builder.setTitle ("sex selection");
builder.setCancelable (false);
builder.setSingleChoiceItems (strSex, 0, new DialogInterface.OnClickListener () {

@ Override
public void onClick (DialogInterface dialog, int which) {
/ / TODO Auto-generated method stub
index = which;
}
});
builder.setPositiveButton (R.string.confirm, new DialogInterface.OnClickListener () {

@ Override
public void onClick (DialogInterface dialog, int which) {
/ / TODO Auto-generated method stub
txtsex.setText (getResources () getStringArray (R.array.sex) [index].);
}
});
builder.setNegativeButton (R.string.cancel, new DialogInterface.OnClickListener () {

@ Override
public void onClick (DialogInterface dialog, int which) {
/ / TODO Auto-generated method stub
txtsex.setText ("");
}
});
builder.create () show ();.
}
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
xml defined android: onClick will report this error, the code will not be registered, is a bit strange.
Your registration code in it.
Reply:
cited a floor tantahe reply:
xml defined android: onClick will report this error, the code will not be registered, is a bit strange.
Your registration code in it.
Where do? Please enlighten me!
Reply:
The android: onClick = "setSex" remove the onCreate method in activity rearmost plus
 ((AutoCompleteTextView) findViewById (R.id.txtsex)) setOnClickListener (new OnClickListener () {. 

@ Override
public void onClick (View v) {
setSex (v);
}
});

Reply:
reference to the third floor tantahe reply:
the android: onClick = "setSex" removed, the activity's onCreate method rearmost plus
 ((AutoCompleteTextView) findViewById (R.id.txtsex)) setOnClickListener (new OnClickListener () {

@ Override
public void onClick (View v) {
setSex (v);
}
});


Yo West, I try
Reply:
Seemingly defined in xml android: onClick only for the Button class (including subclasses), otherwise an error, the best landlord is registered onClick event in code, 3rd Floor, positive solution!
Reply:
references, 5th Floor ncepu307 reply:
seemingly defined in xml android: onClick only for the Button class (including its sub-class), otherwise it will error, The best landlord is registered onClick event in code, 3rd Floor, positive solution!

button and other controls or a little different, I had to add Button in GridView in each sub-View, click the event turned out to be useful for wood
Reply:
cited a floor tantahe reply:
xml defined android: onClick will report this error, the code will not be registered, is a bit strange.
Your registration code in it.
Well, it. Another question, do you have used Android-studio, why compiled program which characters will be garbled ah
Reply:
reference to the 7th floor leyo6yilin reply:
Quote: references to a floor tantahe reply:

xml defined android: onClick will report this error, the code will not be registered, is a bit strange.
Your registration code in it.
Well, it. Another question, do you have used Android-studio, why compiled program which characters will be garbled ah

Never used, I have been using eclipse
Reply:
reference to the 8th floor tantahe reply:
Quote: references to the 7th floor leyo6yilin reply:

Quote: references to a floor tantahe reply:

xml defined android: onClick will report this error, the code will not be registered, is a bit strange.
Your registration code in it.
Well, it. Another question, do you have used Android-studio, why compiled program which characters will be garbled ah

Never used, I have been using eclipse
ah! ! ! Well, finally, all in English, not to force ah (although the finished package, but I do not have to exercise their own). . . . . . . . . . . . . . . . . . . . . Recommend you use this software, Google themselves than Eclipse smooth
Reply:
reference to the 7th floor leyo6yilin reply:
Quote: references to a floor tantahe reply:

xml defined android: onClick will report this error, the code will not be registered, is a bit strange.
Your registration code in it.
Well, it. Another question, do you have used Android-studio, why compiled program which characters will be garbled ah

Never used, Google introduced the IDE did not take long, according to some feedback and some bug, need to be improved. I am from eclipse to Intellij IDEA, I feel stronger than Eclipse, easy to use and more, stdio Based on this, and the idea to support Chinese encoding, and it's smart tips, integrated version control is very powerful, it is recommended to use the landlord!

No comments:

Post a Comment