我一直在初始化(据我所知)对象上获得NullPointerException

问题描述 投票:0回答:1

因此,我正在使用应该从我的视图获取数据并将其分配给Post的class属性的方法来开发我的android应用。问题是我不断在fieldsReader上获取NullPointerException,我正在使用它从视图中获取数据。

这是错误:

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'android.text.Editable android.widget.EditText.getText()'

这是我的代码:


public class NewPostFragment extends Fragment{

    Post post = new Post();
    private NewPostViewModel newPostViewModel;
    static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    private static final String TAG = NewPostFragment.class.getSimpleName();
    private static final int REQUEST_CODE_DATE = 200;
    private static final int REQUEST_CODE_TIME = 201;
    private EditText editText_date;
    private EditText editText_time;

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

        newPostViewModel = ViewModelProviders.of(this).get(NewPostViewModel.class);
        View root = inflater.inflate(R.layout.fragment_new_post, container, false);

        final TextView textView = root.findViewById(R.id.text_new_post);
        newPostViewModel.getText().observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                //textView.setText(s);
            }

        });

        Button btn1 = (Button) root.findViewById(R.id.locationSearchButton);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), MapsActivity.class);
                ( getActivity()).startActivity(intent);
            }
        });


        Button btn2 = (Button) root.findViewById(R.id.post_button);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addPostToDatabase(getDataFromFields(v));
            }
        });

        return root;
    }




    public Post getDataFromFields(View root){

        post.setAvailability(true);                                                                 
        //a.setOwner();

        EditText fieldsReader = (EditText) root.findViewById(R.id.postTitleField);                  
        post.setTitle(fieldsReader.getText().toString()); //ERROR ERROR ERROR ERROR ERROR ERROR

        fieldsReader = (EditText) root.findViewById(R.id.postDescription);                                     
        post.setDescription(fieldsReader.getText().toString());



        return post;
    }
android android-fragments nullpointerexception android-edittext
1个回答
0
投票

您正在通过按钮的视图来查找edittext字段,但是您应该通过根视图来查找edittext字段是错误的,>

 Button btn2 = (Button) root.findViewById(R.id.post_button);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addPostToDatabase(getDataFromFields(root));
            }
        });
© www.soinside.com 2019 - 2024. All rights reserved.