简体   繁体   中英

Problems with getText() from EditText

http://pastebin.com/babqrrqa

Like the title says, i have problems with getting the text from a EditText.

My app crashes when reads this line :

String value = aux.getText().toString(); , probably because aux.getText() is null still i have setText("lol"), at this method

 public boolean onOptionsItemSelected(MenuItem item) {  
    switch (item.getItemId()) {  
    case R.id.adicionar:  
        //Toast.makeText(this, "This is the Toast message", Toast.LENGTH_LONG).show();
        final AlertDialog.Builder alert = new AlertDialog.Builder(this);
        final EditText input = new EditText(this);

         LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         LinearLayout dialerLayout = (LinearLayout) layoutInflater.inflate(R.layout.input, null);
         LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
            dialerLayout.setLayoutParams(params);


        alert.setView(dialerLayout);



        alert.setPositiveButton("Adicionar", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {


                aux = (EditText)findViewById(R.id.cadeira);
                aux.setText("lol");
                String value = aux.getText().toString();
                //String value2="TESTE";

            lista.add(value);
             adapter.notifyDataSetChanged();

            }

        });

        alert.setNegativeButton("Cancelar",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        dialog.cancel();
                    }
                });
        alert.show();



    return true;  
    default: return super.onOptionsItemSelected(item);  
    }
    }  

Probably the "aux" is null.

As I suppose from your code the "cadeira" EditText is in the LinearLayout that you inflate. But when you execute "findViewByById" it searches the main layout. Thus you should try:

aux = (EditText) dialerLayout.findViewById(R.id.cadeira);

This might be because of the .toString() ! The result of the getText() will be a string already so the toString() might cause it to crash.

尽管您没有显示确切的错误日志措辞,但声明或初始化“ aux”可能会出现人为错误 ,如果您可以详细说明它,则可以更快地修复问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM