我希望对我的微调器进行验证(使用按钮),如果我的应用程序的用户没有选择项目或用户选择了相同的值(微调器有一个值“请选择一些东西”和其他值)例如在微调器项的数组列表中:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="condition_prompt">the condition of the car</string>
<string-array name="condition_arrays">
<item>-Please select something-</item> <!-- Hint -->
<item>New</item>
<item>Used</item>
</string-array>
</resources>
并且只要用户点击(点击)按钮就会触发此验证。我正在使用按钮来验证这些微调器,因为此按钮还将验证我的edittext的值,我的应用程序有一些edittexts和spinners,然后当所有填充然后按钮将开始新的活动。对于edittext我已经完成验证并且工作完美(它将向文本“请填写编辑文本”)。现在我需要的是我的微调器的验证。我为它做了代码,但它有一个错误
代码
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View seletedItem, int pos, long id)
{
String item1 = (String) parent.getItemAtPosition(pos);
}
public void onNothingSelected(AdapterView<?> parent)
{
}
});
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View selectedItem, int pos, long id)
{
String item2 = (String) parent.getItemAtPosition(pos);
}
public void onNothingSelected(AdapterView<?> parent)
{
}
});
spinner3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View selectedItem, int pos, long id)
{
String item3 = (String) parent.getItemAtPosition(pos);
}
public void onNothingSelected(AdapterView<?> parent)
{
}
});
buttonvalidate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String ethint2 = ethint1.getText().toString();
String etpersen3 =etpersen2.getText().toString();
if(ethint2.trim().equals("")) {
Toast.makeText(Mastercode.this, "pls fill the price", Toast.LENGTH_SHORT).show();
return;
}
else if(etpersen3.trim().equals("")) {
Toast.makeText(Mastercode.this, "pls fill the percentage", Toast.LENGTH_SHORT).show();
return;
}
/*i want to add more else if whenen button clicked it will also remind the user if the user doesn't select an item from the spinner or the user will select the "please select something" again with a toast text saying"please pick lol or please pick blaalalal(just example)" */
}
});
如果我在public void onClick
else if(item1.equals("Please select something")){ do the toast text here }
上添加else,则会出现错误,指出item1无法在变量中解析。我正在寻找更好的实现或更正我的代码。谢谢提前
尝试更改代码,记录cat错误:
02-18 16:26:47.793: E/AndroidRuntime(320): FATAL EXCEPTION: main
02-18 16:26:47.793: E/AndroidRuntime(320): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.m4nd1r1tun45f1n4nc3.abcdefg/com.m4nd1r1tun45f1n4nc3.abcdefg.Mastercode}: java.lang.NullPointerException
02-18 16:26:47.793: E/AndroidRuntime(320): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-18 16:26:47.793: E/AndroidRuntime(320): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-18 16:26:47.793: E/AndroidRuntime(320): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-18 16:26:47.793: E/AndroidRuntime(320): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-18 16:26:47.793: E/AndroidRuntime(320): at android.os.Handler.dispatchMessage(Handler.java:99)
02-18 16:26:47.793: E/AndroidRuntime(320): at android.os.Looper.loop(Looper.java:123)
02-18 16:26:47.793: E/AndroidRuntime(320): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-18 16:26:47.793: E/AndroidRuntime(320): at java.lang.reflect.Method.invokeNative(Native Method)
02-18 16:26:47.793: E/AndroidRuntime(320): at java.lang.reflect.Method.invoke(Method.java:521)
02-18 16:26:47.793: E/AndroidRuntime(320): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-18 16:26:47.793: E/AndroidRuntime(320): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-18 16:26:47.793: E/AndroidRuntime(320): at dalvik.system.NativeStart.main(Native Method)
02-18 16:26:47.793: E/AndroidRuntime(320): Caused by: java.lang.NullPointerException
02-18 16:26:47.793: E/AndroidRuntime(320): at com.m4nd1r1tun45f1n4nc3.abcdefg.Mastercode.onCreate(Mastercode.java:84)
02-18 16:26:47.793: E/AndroidRuntime(320): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-18 16:26:47.793: E/AndroidRuntime(320): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-18 16:26:47.793: E/AndroidRuntime(320): ... 11 more
工作代码:
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View seletedItem, int pos, long id)
{
String item1 = (String) parent.getItemAtPosition(pos);
}
public void onNothingSelected(AdapterView<?> parent)
{
}
});
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View selectedItem, int pos, long id)
{
String item2 = (String) parent.getItemAtPosition(pos);
}
public void onNothingSelected(AdapterView<?> parent)
{
}
});
spinner3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View selectedItem, int pos, long id)
{
String item3 = (String) parent.getItemAtPosition(pos);
}
public void onNothingSelected(AdapterView<?> parent)
{
}
});
public void buttonvalidator(View v){
String ethint2 = ethint1.getText().toString();
String etpersen3 =etpersen2.getText().toString();
if(ethint2.trim().equals("")) {
Toast.makeText(Mastercode.this, "Please fill the price", Toast.LENGTH_SHORT).show();
return;
}
else if(etpersen3.trim().equals("")) {
Toast.makeText(Mastercode.this, "Please fill the percentage", Toast.LENGTH_SHORT).show();
return;
}
else if(item1.toString().equals("-Please select the car condition-")){
Toast.makeText(Mastercode.this, "Please select the condition of the car", Toast.LENGTH_SHORT).show();
return;
}
else if(item2.toString().equals("-Please select the kind of your car insurance-")){
Toast.makeText(Mastercode.this, "Please select the kind of your car insurance", Toast.LENGTH_SHORT).show();
return;
}
else if(item3.toString().equals("-Please select the zone-")){
Toast.makeText(Mastercode.this, "Please select the zone", Toast.LENGTH_SHORT).show();
return;
}
}
按钮代码也可以这样写
public void buttonvalidator(View v){
String ethint2 = ethint1.getText().toString();
String etpersen3 =etpersen2.getText().toString();
if(ethint2.trim().equals("")) {
Toast.makeText(Mastercode.this, "Please fill the price", Toast.LENGTH_SHORT).show();
return;
}
else if(etpersen3.trim().equals("")) {
Toast.makeText(Mastercode.this, "Please fill the percentage", Toast.LENGTH_SHORT).show();
return;
}
else if(spinner1.getSelectedItem().toString().trim().equals("-Please select something-")){
Toast.makeText(Mastercode.this, "Please select something from spinner1", Toast.LENGTH_SHORT).show();
return;
}
else if(spinner2.getSelectedItem().toString().trim().equals("-Please select something-")){
Toast.makeText(Mastercode.this, "Please select something from spinner2", Toast.LENGTH_SHORT).show();
return;
}
else if(spinner3.getSelectedItem().toString().trim().equals("-Please select something-")){Toast.makeText(Mastercode.this, "Please select something from spinner3", Toast.LENGTH_SHORT).show();
return;
}
}
buttonvalidator来自按钮
上的layout.xml<button
android:OnClick="buttonvalidator"/>
答案 0 :(得分:1)
在类级别而不是在整个类中访问的方法范围内声明item1,item2,item3
。
建议在Button的spinner1.getSelectedItem()
方法中使用onClick
来从Spinner获取选定的值:
if(spinner1.getSelectedItem().toString().equals(
"Please select something")){
// do the toast text here
}else if(spinner2.getSelectedItem().toString().equals(
"Please select something")){
// do the toast text here
}
.....