我创建了一个少量单位的转换器。我使用微调器来选择要转换的单位,但是在我的设置适配器方法中,当我设置从和没有采取时如果放置仅从它的工作它没有采取确切的值并显示空白我不明白在哪里这个错误是。任何人都可以请帮助。它花了很多时间。
Uniqcon:
public class UniqueConverter extends Fragment {
private Spinner fromDetails, toDetails;
private EditText fromInput, toInput;
public static UniqueConverter setArguments(int position) {
UniqueConverter uniqueConverter = new UniqueConverter();
Bundle args = new Bundle();
args.putInt("position", position);
uniqueConverter.setArguments(args);
return uniqueConverter;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_uniquelayout, container, false);
fromInput.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
Double inp;
double out = 0;
if (!fromInput.getText().toString().equals(""))
{
String ifrom = (String) fromDetails.getSelectedItem();
String rto = (String) toDetails.getSelectedItem();
inp = Double.valueOf(fromInput.getText().toString());
int position = getArguments().getInt("position");
switch (position)
{
case 0:
setAdapter(getResources().getStringArray(R.array.temperatureform), "Celsius", "Fahrenheit");
TemperatureConverter con = new TemperatureConverter();
TemperatureConverter.Units fromUnit = TemperatureConverter.Units.fromString(ifrom);
TemperatureConverter.Units toUnit = TemperatureConverter.Units.fromString(rto);
out = con.TemperatureConvert(fromUnit,toUnit,inp);
break;
case 1:
setAdapter(getResources().getStringArray(R.array.weightform), "Kilograms", "Grams");
WeightConverter converter = new WeightConverter();
WeightConverter.Inus fromInus = WeightConverter.Inus.fromString(ifrom);
WeightConverter.Inus toInus = WeightConverter.Inus.fromString(rto);
out = converter.WeightConvert(fromInus,toInus,inp);
break;
}
toInput.setText(String.valueOf(out));
}else {
toInput.setText("");
}
}
});
onInitVies(view);
return view;
}
private void setAdapter(String[] spinnerItems, String fromDefaultText, String toDefaultText) {
fromDetails.setAdapter(new CustomSpinnerAdapter(getContext(), R.layout.spinner_item, spinnerItems, fromDefaultText));
toDetails.setAdapter(new CustomSpinnerAdapter(getContext(),R.layout.spinner_item,spinnerItems,toDefaultText));
}
private void onInitVies(View view) {
fromDetails = (Spinner) view.findViewById(R.id.fromSpinner);
toDetails = (Spinner) view.findViewById(R.id.toSpinner);
fromInput = (EditText) view.findViewById(R.id.fromInput);
toInput = (EditText) view.findViewById(R.id.toInput);
}
的字符串数组:
<string-array name="temperatureform">
<item name="c">Celsius</item>
<item name="f">Fahrenheit</item>
<item name="k">Kelvin</item>
</string-array>
<string-array name="weightform">
<item name="kg">Kilograms</item>
<item name="g">Grams</item>
<item name="lb">Pounds</item>
</string-array>
logcat的:
Exception dispatching finished signal.
03-15 02:40:25.843 14349-14349/com.cloudicalabs.converters E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
03-15 02:40:25.844 14349-14349/com.cloudicalabs.converters E/MessageQueue-JNI: java.lang.IllegalArgumentException: Cannot find a value for null
03-15 02:40:25.844 14349-14349/com.cloudicalabs.converters E/MessageQueue-JNI: at com.cloudicalabs.converters.datahandlers.WeightConverter$Inus.fromString(WeightConverter.java:26)
03-15 02:40:25.844 14349-14349/com.cloudicalabs.converters E/MessageQueue-JNI: at com.cloudicalabs.converters.fragments.UniqueConverter$1.afterTextChanged(UniqueConverter.java:95)
03-15 02:40:25.844 14349-14349/com.cloudicalabs.converters E/MessageQueue-JNI: at android.widget.TextView.sendAfterTextChanged(TextView.java:8007)
03-15 02:40:25.844 14349-14349/com.cloudicalabs.converters E/MessageQueue-JNI: at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:10168)
03-15 02:40:25.844 14349-14349/com.cloudicalabs.converters E/MessageQueue-JNI: at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:1043)
03-15 02:40:25.844 14349-14349/com.cloudicalabs.converters E/MessageQueue-JNI: at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:560)
03-15 02:40:25.844 14349-14349/com.cloudicalabs.converters E/MessageQueue-JNI: at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:492)
03-15 02:40:25.844 14349-14349/com.cloudicalabs.converters E/MessageQueue-JNI: at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:491)
03-15 02:40:25.844 14349-14349/com.cloudicalabs.converters E/MessageQueue-JNI: at android.text.method.NumberKeyListener.onKeyDown(NumberKeyListener.java:121)
03-15 02:40:25.844 14349-14349/com.cloudicalabs.converters E/MessageQueue-JNI: at android.widget.TextView.doKeyDown(TextView.java:6098)
03-15 02:40:25.844 14349-14349/com.cloudicalabs.converters E/MessageQueue-JNI: at android.widget.TextView.onKeyDown(TextView.java:5911)
03-15 02:40:25.844 14349-14349/com.cloudicalabs.converters E/MessageQueue-JNI: at android.view.KeyEvent.dispatch(KeyEvent.java:2640)
03-15 02:40:25.844 14349-14349/com.cloudicalabs.converters E/MessageQueue-JNI: at android.view.View.dispatchKeyEvent(View.java:9234)
03-15 02:40:25.844 14349-14349/com.cloudicalabs.converters E/MessageQueue-JNI: at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
03-15 02:40:25.844 14349-14349/com.cloudicalabs.converters E/MessageQueue-JNI: at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
03-15 02:40:25.844 14349-14349/com.cloudicalabs.converters E/MessageQueue-JNI: at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
03-15 02:40:25.844 14349-14349/com.cloudicalabs.converters E/MessageQueue-JNI: at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
03-15 02:40:25.844 14349-14349/com.cloudicalabs.converters E/MessageQueue-JNI: at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
03-15 02:40:25.844 14349-14349/com.cloudicalabs.converters E/MessageQueue-JNI: at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
03-15 02:40:25.844 14349-14349/com.cloudicalabs.converters E/MessageQueue-JNI: at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
答案 0 :(得分:0)
您好亲爱的尝试这个不要使用直接
String[] temperatureform,weightform ;
temperatureform =getResources().getStringArray(R.array.weightform);
weightform=getResources().getStringArray(R.array.weightform);
setAdapter(temperatureform,"Celsius", "Fahrenheit");
setAdapter(weightform ,"Celsius", "Fahrenheit");