为什么我得到EditText.getText()'在null对象引用?

时间:2017-05-27 03:14:10

标签: android user-interface expandablelistview

我正在构建一个在FAB点击时显示警告对话框的应用程序,获取用户输入并使用该用户输入命名ExpandableListView组标题。当我单击FAB按钮并放入文本时,它会给我一个空对象引用。

我在onCreate()

之前初始化了我的学期
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    ExpandableListView expandableListView;
    ExpandableListViewAdapter adapter;
    private FloatingActionButton addItems;
    private String semester = "";

在我的addItems()方法中,当有FAB点击时调用,我创建自定义警报对话框,获取用户输入字符串并将其放在适配器的ArrayList中。

public void addItems() {
        final EditText et_semester = (EditText)findViewById(R.id.et_semester);

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Create A Semester");
        builder.setView(R.layout.custom_layout);
        builder.setCancelable(false);
        builder.setPositiveButton("DONE", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                semester = et_semester.getText().toString();
                adapter.groupNames.add(semester);
            }
        });

        builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        adapter.notifyDataSetChanged();
        builder.show();
    }

但是当我点击AlertDialog上的DONE按钮时,这是我的堆栈跟踪:

05-30 05:35:10.295 20943-20943/myapp.onur.expandablelistviewexample E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                      Process: myapp.onur.expandablelistviewexample, PID: 20943
                                                                                      java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
                                                                                          at myapp.onur.expandablelistviewexample.MainActivity$1.onClick(MainActivity.java:45)

编辑这是我的MainActivity

public class MainActivity extends AppCompatActivity {

    ExpandableListView expandableListView;
    ExpandableListViewAdapter adapter;
    private FloatingActionButton addItems;
    private String semester;

    int counter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        expandableListView = (ExpandableListView) findViewById(R.id.expListView);

        adapter = new ExpandableListViewAdapter(MainActivity.this);
        expandableListView.setAdapter(adapter);
        addItems = (FloatingActionButton) findViewById(R.id.add_items);

        final EditText et_semester = (EditText)findViewById(R.id.et_semester);
    }

    public void addSemester(View v){
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("Create A Semester");
        builder.setView(R.layout.custom_layout);
        builder.setCancelable(false);

        builder.setPositiveButton("DONE", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                final EditText et_semester = (EditText).findViewById(R.id.et_semester);
                semester = etSemester.getText().toString();
                adapter.groupNames.add(semester);
            }
        });

        builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        adapter.notifyDataSetChanged();
        builder.show();
    }

}

2 个答案:

答案 0 :(得分:0)

您的EditText R.id.et_semester不在您的活动内容视图中,因此您必须在对话框视图中找到此视图

    // Inflate your view
    View dialogView = LayoutInflater.from(context).inflate(R.layout.custom_layout, null);
    // Bind views
    final EditText et_semester = (EditText) dialogView.findViewById(R.id.et_semester);
    // Create dialog
    AlertDialog.Builder builder = new AlertDialog.Builder(context)
            .setTitle("Create A Semester")
            .setView(dialogView)
            .setCancelable(false);

答案 1 :(得分:0)

Hank Moody的回答是正确的。这就是我所做的。

public void addSemester(View v){

        View dialogView = LayoutInflater.from(MainActivity.this).inflate(R.layout.custom_layout, null);
        final EditText et_semester = (EditText)dialogView.findViewById(R.id.et_semester);

        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("Create A Semester");
        builder.setView(dialogView);
        builder.setCancelable(false);

        builder.setPositiveButton("DONE", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                semester = et_semester.getText().toString();
                adapter.groupNames.add(semester);
            }
        });

        builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        adapter.notifyDataSetChanged();
        builder.show();
    }

addSemester()中调用方法onCreate()并不是一个好主意,因为方法中的某些设置是通过FAB点击触发的,但是如果将其放在onCreate()方法中,它们会在创建活动的那一刻运行。