【问题标题】:Null Pointer Exception on EditText to String [duplicate]EditText 上的空指针异常到字符串 [重复]
【发布时间】:2017-10-24 00:45:07
【问题描述】:

我有一个EditText,我在其中获取数字并需要将它们转换为float,但仍然得到相同的错误 - Null Pointer Exception

这是我的代码:

public class Basic extends Fragment implements AdapterView.OnItemSelectedListener {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


        View view = inflater.inflate(R.layout.basic, container, false);
        Spinner spinnerLenght = (Spinner) view.findViewById(R.id.spinnerLenght);
        spinnerLenght.setOnItemSelectedListener(this);
        ArrayAdapter<String> lenghtAdapter;
        lenghtAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.basic_string_lenght));
        lenghtAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerLenght.setAdapter(lenghtAdapter);

        return view;
    }


    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        String itemLenght = parent.getItemAtPosition(position).toString();
        EditText editTextLenght = (EditText) view.findViewById(R.id.editTextLenght);
        float floatLenght;
        floatLenght = Float.parseFloat(editTextLenght.getText().toString());
        float result_mm = 0;
        if (itemLenght.equals("mm")){


            result_mm = floatLenght * 1000;
        }


        Toast.makeText(parent.getContext(), (int) result_mm, Toast.LENGTH_LONG).show();
    }




    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}

这是日志猫:

E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      Process: com.example.dusan.unit, PID: 5181
                                                                      java.lang.NullPointerException
                                                                          at com.example.dusan.unit.Basic.onItemSelected(Basic.java:46)
                                                                          at android.widget.AdapterView.fireOnSelected(AdapterView.java:956)
                                                                          at android.widget.AdapterView.access$200(AdapterView.java:49)
                                                                          at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:920)
                                                                          at android.os.Handler.handleCallback(Handler.java:733)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                          at android.os.Looper.loop(Looper.java:157)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5356)
                                                                          at java.lang.reflect.Method.invokeNative(Native Method)
                                                                          at java.lang.reflect.Method.invoke(Method.java:515)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
                                                                          at dalvik.system.NativeStart.main(Native Method)

我也在double 上尝试过。 我也遇到了null = "" 的问题 我阅读了很多关于此的内容,但找不到解决方案。

【问题讨论】:

    标签: android string nullpointerexception android-edittext


    【解决方案1】:

    1)检查您是否写了正确的editText id; 2)在onCreateView中从膨胀布局中找到你的editText,或者将你的布局视图作为类变量,然后在onItemClick中从她那里找到你的编辑文本

    【讨论】:

    • 我在这里使用选项卡式活动。我在这里没有 onCreate 我有 onCreateView 有人告诉我这是问题。对吗?
    • @DusanCulum 选项卡式活动确实有onCreate。选项卡中的片段有onCreateView,但这不是问题
    【解决方案2】:

    主要问题

    您使用了错误的 ID 和类别。

    <b>EditText</b> editTextLenght = <b>(EditText)</b> view.findViewById(<b>R.id.editTextLenght</b>);

    正确的 ID 是android.R.id.text1
    正确的类是TextView

    请参阅What is "android.R.layout.simple_list_item_1"?,因为这是您在适配器中设置的布局。


    替代解决方案

    如果问题是parent.getItemAtPosition(position).toString();,那么不要使用它。

    如果要访问适配器中的数据,请将其移至可以使用成员变量访问的范围

    public class Basic extends Fragment implements AdapterView.OnItemSelectedListener {
    
        private ArrayAdapter<String> lenghtAdapter;
    
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.basic, container, false);
            Spinner spinnerLenght = (Spinner) view.findViewById(R.id.spinnerLenght);
            spinnerLenght.setOnItemSelectedListener(this);
            this.lenghtAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.basic_string_lenght));
    

    然后,使用getItem 方法而不是试图找到视图

    float floatLenght = Float.parseFloat(lenghtAdapter.getItem(position));
    

    另外,请正确拼写长度

    【讨论】:

    • 仍然有问题 logcat: java.lang.NullPointerException at com.example.dusan.unit.Basic.onItemSelected(Basic.java:39) ( String itemLenght = spinnerLenght.getSelectedItem().toString() ;)
    • spinnerLenght 在您的问题中无法在onItemSelected 中访问,并且您粘贴的代码与问题不匹配。所以这是一个新的错误,不一样
    • 致命异常:主进程:com.example.dusan.unit,PID:24497 java.lang.ClassCastException:android.support.v7.widget.AppCompatTextView 无法转换为 android.widget.EditText com.example.dusan.unit.Basic.onItemSelected(Basic.java:44) 44 是这样的:EditText editTextLenght = (EditText) view.findViewById(android.R.id.text1);
    • 这很有道理...它不是可编辑的文本字段。那么请尝试替代解决方案
    • 但是在 Spinner 我选择了 mm cm 或 m 所以我首先需要它作为 if (itemLenght.equals("mm")){ result_mm = floatLenght * 1000; }
    【解决方案3】:

    看看主要(你问的)问题,你想把numberEditText转换成float

    第一次导入: import android.widget.EditText;

    EditText editTextLenght = (EditText) View.findViewById(R.id.editTextLenght);
    
    floatLenght = Float.parseFloat(editTextLenght.getText().toString());
    //this code used in java programs.in android you can use below
    
    try{
       float floatLenght =  Float.valueOf(editTextLenght.getText().toString());
    } catch (NumberFormatException e) {
       e.printStackTrace();
    }
    

    float(第一个字母很小)是原始类型,Float 是用于从string 转换为float 的包装类。 您不需要更改其他代码。

    【讨论】:

    • java.lang.NullPointerException at com.example.dusan.unit.Basic.onItemSelected(Basic.java:53) 是 float floatLenght = Float.valueOf(editTextLenght.getText().toString() );
    猜你喜欢
    • 2020-05-08
    • 2017-07-23
    • 2020-01-24
    • 1970-01-01
    • 1970-01-01
    • 2014-04-11
    • 2011-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多