尝试在空对象引用调用虚拟方法android.text.Editable android.widget.EditText.getText() [英] Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference

查看:4203
本文介绍了尝试在空对象引用调用虚拟方法android.text.Editable android.widget.EditText.getText()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了所有可能的情况,但没有得到解决,需要建议

 公共类WolfActivity扩展ActionBarActivity {   的EditText fname_ele,lname_ele,email_ele,phone_ele,pwd_ele,fax_ele,addr_ele,addr1_ele,city_ele,zip$c$c_ele,facility_ele,state_ele;    串FNAME,LNAME,电子邮件,电话,PWD,传真,地址,ADDR1,城市,邮编code,设施状态;
    Button按钮;
    AlertDialog alertDialog;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        fname_ele =(EditText上)findViewById(R.id.first_name);
        lname_ele =(EditText上)findViewById(R.id.last_name);
        email_ele =(EditText上)findViewById(R.id.email);
        按钮=(按钮)findViewById(R.id.submit_button);
        phone_ele =(EditText上)findViewById(R.id.phone);
        addr_ele =(EditText上)findViewById(R.id.address);
        addr1_ele =(EditText上)findViewById(R.id.address1);
        fax_ele =(EditText上)findViewById(R.id.fax);
        facility_ele =(EditText上)findViewById(R.id.facility);
        拉链code_ele =(EditText上)findViewById(R.id.zip);
        city​​_ele =(EditText上)findViewById(R.id.city);
        state_ele =(EditText上)findViewById(R.id.state);    / *微调下拉=(微调)findViewById(R.id.state);
        的String []项目=新的String [] {1,2,3};
        ArrayAdapter<串GT;适配器=新ArrayAdapter<串GT;(这一点,android.R.layout.simple_spinner_item,项目);
        dropdown.setAdapter(适配器); * /        button.setOnClickListener(新View.OnClickListener(){
        @覆盖
        公共无效的onClick(视图v){
            。FNAME = fname_ele.getText()的toString();
            LNAME = lname_ele.getText()的toString()。
            。电子邮件= email_ele.getText()的toString();
            PWD = pwd_ele.getText()的toString()。
            。手机= phone_ele.getText()的toString();
            地址= addr_ele.getText()的toString()。
            。ADDR1 = addr1_ele.getText()的toString();
            传真= fax_ele.getText()的toString()。
            。设施= facility_ele.getText()的toString();
            。拉链code =拉链code_ele.getText()的toString();
            。城市= city_ele.getText()的toString();
            。状态= state_ele.getText()的toString();            Log.d(电子邮件,电子邮件);
            如果(!isValidEmail(电子邮件)){                Toast.makeText(WolfActivity.this,输入有效的电子邮件,Toast.LENGTH_SHORT).show();
            }其他
                新signmeup()执行();
        }
    });}
@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
//充气菜单;如果是present这增加了项目操作栏。
    。getMenuInflater()膨胀(R.menu.main,菜单);
    返回true;
}@覆盖
公共布尔onOptionsItemSelected(菜单项项){
    //处理动作栏项目点击这里。操作栏会
    //自动处理上点击主页/向上按钮,只要
    //你在AndroidManifest.xml中指定一个父活动。
    INT ID = item.getItemId();    // noinspection SimplifiableIfStatement
    如果(ID == R.id.action_settings){
        返回true;
    }    返回super.onOptionsItemSelected(项目);
}私人布尔isValidEmail(字符串email){
    字符串EMAIL_PATTERN =^ [_ A-ZA-Z0-9 - \\\\ +] +(\\\\ [_ A-ZA-Z0-9 - ] +)* @+[A-ZA-z0-9-] +(\\\\。[A-ZA-Z0-9] +)*(\\\\。[A-ZA-Z] {2})$;
    模式模式= Pattern.compile(EMAIL_PATTERN);
    匹配器匹配= pattern.matcher(电子邮件);
    返回matcher.matches();
}类signmeup扩展的AsyncTask<字符串,字符串,字符串> {        私人ProgressDialog pDialog;
        JSONParser jsonParser =新JSONParser();        @覆盖
        在preExecute保护无效(){
            super.on preExecute();
            pDialog =新ProgressDialog(WolfActivity.this);
            pDialog.setMessage(感谢您的注册...);
            pDialog.setIndeterminate(假);
            pDialog.setCancelable(真);        }        @覆盖
        保护字符串doInBackground(字符串...为arg0){
            // TODO自动生成方法存根            //大厦参数
            清单<&的NameValuePair GT; PARAMS =新的ArrayList<&的NameValuePair GT;();
            params.add(新BasicNameValuePair(部分,用户));
            params.add(新BasicNameValuePair(行动,新));
            params.add(新BasicNameValuePair(FIRST_NAME,FNAME));
            params.add(新BasicNameValuePair(姓氏,LNAME));
            params.add(新BasicNameValuePair(电子邮件,电子邮件));
            params.add(新BasicNameValuePair(手机,手机));
            params.add(新BasicNameValuePair(传真传​​真));
            params.add(新BasicNameValuePair(地址,地址));
            params.add(新BasicNameValuePair(地址1,ADDR1));
            params.add(新BasicNameValuePair(设施,设施));
            params.add(新BasicNameValuePair(密码,PWD));
            params.add(新BasicNameValuePair(ZIP,邮编code));
            params.add(新BasicNameValuePair(城市,市));            //获取JSON对象
            //注意,创建产品网址接受POST方法
            JSONObject的JSON = jsonParser.makeHtt prequest(http://eastendwebsolutions.com/wf/ws/,GET,则params);            Log.d(名,FNAME);
            Log.d(姓,LNAME);
            Log.d(电子邮件,电子邮件);
            //检查日志猫响应
            Log.d(创建回应,json.toString());            //检查成功标记
           尝试{
                字符串成功= json.getString(成功);                如果(电子邮件= NULL&安培;!成功==真){                    Toast.makeText(WolfActivity.this,特殊照顾电子邮件发布..
                            Toast.LENGTH_SHORT).show();                }其他{                }
            }赶上(JSONException E){
                e.printStackTrace();
            }            返回null;
        }        @燮pressWarnings(德precation)
        // @覆盖
        保护无效onPostExecute(字符串FILE_URL){            如果(pDialog.isShowing()){
                pDialog.dismiss();
            }
            AlertDialog.Builder建设者=新AlertDialog.Builder(WolfActivity.this);
            builder.setTitle(信息);
            builder.setMessage(你已经订阅了HHI假期目录通讯录检查电子邮件,以确认。);
            builder.setIcon(R.drawable.app_icon);
            builder.create()显示()。        }
    }
}


解决方案

您有12 的EditText 宣布,但已初始化,11

您忘了初始化 pwd_ele

I have tried all possible cases but not solved , Need suggestions

public class WolfActivity extends ActionBarActivity {

   EditText fname_ele,lname_ele,email_ele,phone_ele,pwd_ele,fax_ele,addr_ele,addr1_ele,city_ele,zipcode_ele,facility_ele,state_ele;

    String fname,lname,email,phone,pwd,fax,addr,addr1,city,zipcode,facility,state;
    Button button;
    AlertDialog alertDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fname_ele=(EditText) findViewById(R.id.first_name);
        lname_ele=(EditText) findViewById(R.id.last_name);
        email_ele=(EditText) findViewById(R.id.email);
        button=(Button) findViewById(R.id.submit_button);
        phone_ele=(EditText) findViewById(R.id.phone);
        addr_ele=(EditText) findViewById(R.id.address);
        addr1_ele=(EditText) findViewById(R.id.address1);
        fax_ele=(EditText) findViewById(R.id.fax);
        facility_ele=(EditText) findViewById(R.id.facility);
        zipcode_ele=(EditText) findViewById(R.id.zip);
        city_ele=(EditText) findViewById(R.id.city);
        state_ele=(EditText) findViewById(R.id.state);

    /* Spinner dropdown = (Spinner)findViewById(R.id.state);
        String[] items = new String[]{"1", "2", "3" };
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items);
        dropdown.setAdapter(adapter);*/

        button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            fname=fname_ele.getText().toString();
            lname=lname_ele.getText().toString();
            email=email_ele.getText().toString();
            pwd=pwd_ele.getText().toString();
            phone=phone_ele.getText().toString();
            addr=addr_ele.getText().toString();
            addr1=addr1_ele.getText().toString();
            fax=fax_ele.getText().toString();
            facility=facility_ele.getText().toString();
            zipcode=zipcode_ele.getText().toString();
            city=city_ele.getText().toString();
            state=state_ele.getText().toString();

            Log.d("Email",email);
            if (!isValidEmail(email)) {

                Toast.makeText(WolfActivity.this, "Enter valid email", Toast.LENGTH_SHORT).show();
            } else
                new signmeup().execute();
        }
    });

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

private boolean isValidEmail(String email) {
    String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"+"[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
    Pattern pattern = Pattern.compile(EMAIL_PATTERN);
    Matcher matcher = pattern.matcher(email);
    return matcher.matches();
}

class signmeup extends AsyncTask<String, String, String> {

        private ProgressDialog pDialog;
        JSONParser jsonParser = new JSONParser();

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(WolfActivity.this);
            pDialog.setMessage("Thanks for your Sign up...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);

        }

        @Override
        protected String doInBackground(String... arg0) {
            // TODO Auto-generated method stub

            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("section","user"));
            params.add(new BasicNameValuePair("action","new"));
            params.add(new BasicNameValuePair("first_name",fname));
            params.add(new BasicNameValuePair("last_name",lname));
            params.add(new BasicNameValuePair("email",email));
            params.add(new BasicNameValuePair("phone",phone));
            params.add(new BasicNameValuePair("fax",fax));
            params.add(new BasicNameValuePair("address",addr));
            params.add(new BasicNameValuePair("address1",addr1));
            params.add(new BasicNameValuePair("facility",facility));
            params.add(new BasicNameValuePair("password",pwd));
            params.add(new BasicNameValuePair("zip",zipcode));
            params.add(new BasicNameValuePair("city",city));

            // getting JSON Object
            // Note that create product url accepts POST method
            JSONObject json = jsonParser.makeHttpRequest("http://eastendwebsolutions.com/wf/ws/", "GET", params);

            Log.d("First Name",fname);
            Log.d("Last Name",lname);
            Log.d("Email",email);
            // check log cat for response
            Log.d("Create Response", json.toString());

            // check for success tag
           try {
                String success = json.getString("success");

                if (email != null & success == "true") {

                    Toast.makeText(WolfActivity.this, "Youre Email Posted..",
                            Toast.LENGTH_SHORT).show();

                } else {

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        @SuppressWarnings("deprecation")
        //@Override
        protected void onPostExecute(String file_url) {

            if(pDialog.isShowing()){
                pDialog.dismiss();
            }
            AlertDialog.Builder builder=new AlertDialog.Builder(WolfActivity.this);
            builder.setTitle("Info");
            builder.setMessage("You have been subscribed to the HHI Vacation Directory Mailing List. Check email to confirm.");
            builder.setIcon(R.drawable.app_icon);
            builder.create().show();

        }
    }
}

解决方案

You have 12 EditText declared but you've initialized 11

You forgot to initialize pwd_ele

这篇关于尝试在空对象引用调用虚拟方法android.text.Editable android.widget.EditText.getText()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆