尝试在空对象引用上调用虚拟方法'android.widget.EditText.getText()'。

8

我尝试了所有可能的情况,但都没有解决,需要建议。

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();

        }
    }
}

我最近开始使用Android SDK工作。 - manikanta g
1
请在Logcat中发布完整的错误堆栈跟踪。 - Sharp Edge
可能是pwd=pwd_ele.getText().toString();这一行引起了问题,因为pwd_ele没有被初始化。 - ρяσѕρєя K
你的 pwd_ele 编辑文本声明在哪里? - Rahul Sharma
是的,你说得对 @ρяσѕρєяK - manikanta g
3个回答

8

您声明了12个EditText,但只初始化了11个

您忘记初始化pwd_ele


4
在setContentView之后添加这行代码:
pwd_ele=(EditText) findViewById(R.id.password);

0
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;

请注意,您缺少一个在上面声明的EditText。您错过了在onCreate方法中初始化字符串pwe_ele
        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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接