嗨,我想为我的Android应用程序设置错误消息,但它一直崩溃,我不知道为什么

时间:2014-09-17 17:15:05

标签: android

您好我的错误消息有问题。我试图设置单击我的添加按钮时,如果用户将我的一个编辑文本留空,将显示一条消息。问题是我该如何做到这一点以及同时添加我的联系人?

这是给我带来困难的部分:

while (!name.matches("") && !phone.matches("") && !email.matches("") && !address.matches("") ){
                        Boolean added  = handler.addContactDetails(contact);
                        if(added){
                        Intent intent = new Intent(NewContact.this, Main.class);
                        startActivity(intent);

                        }else{
                            Toast.makeText(getApplicationContext(), "Contact data not added. Please try again", Toast.LENGTH_LONG).show();

这是完整的课程:

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class NewContact extends Activity {  

    private static int RESULT_LOAD_IMAGE = 1;

    private ContactHandler handler;

    private String picturePath = "";

    private String name;
    private String phone;
    private String email;
    private String address;
    private String photograph;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.new_contact);

        handler = new ContactHandler(getApplicationContext());

        ImageView iv_user_photo = (ImageView) findViewById(R.id.iv_user_photo);
        iv_user_photo.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                startActivityForResult(intent, RESULT_LOAD_IMAGE);              

            }
        });


        Button btn_add = (Button) findViewById(R.id.btn_add);
        btn_add.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                EditText et_name = (EditText) findViewById(R.id.et_name);
                phone = et_name.getText().toString();
            //  if (  ( !et_name.getText().toString().equals("")))  
                //{

            //return; }

                //else if (( et_name.getText().toString().equals("")))


                       // Toast.makeText(getApplicationContext(),
                              //  "Password field empty", Toast.LENGTH_SHORT).show();
                {
                //else 

                //return;


                EditText et_phone = (EditText) findViewById(R.id.et_phone);
                phone = et_phone.getText().toString();


                {

                EditText et_email = (EditText) findViewById(R.id.et_email);
                email = et_email.getText().toString();


                {

                EditText et_address = (EditText) findViewById(R.id.et_address);
                address = et_address.getText().toString();


                {

                ImageView iv_photograph = (ImageView) findViewById(R.id.iv_user_photo);
                photograph = picturePath;

                Contact contact = new Contact();
                contact.setName(name);
                contact.setPhoneNumber(phone);
                contact.setEmail(email);
                contact.setPostalAddress(address);
                contact.setPhotograph(photograph);






                while (!name.matches("") && !phone.matches("") && !email.matches("") && !address.matches("") ){
                    Boolean added  = handler.addContactDetails(contact);
                    if(added){
                    Intent intent = new Intent(NewContact.this, Main.class);
                    startActivity(intent);

                    }else{
                        Toast.makeText(getApplicationContext(), "Contact data not added. Please try again", Toast.LENGTH_LONG).show();

                    }





                }


            }
        }}}}});

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
            Uri imageUri = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContentResolver().query(imageUri,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            picturePath = cursor.getString(columnIndex);
            cursor.close();

            ImageView imageView = (ImageView) findViewById(R.id.iv_user_photo);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

        }
    }

}

2 个答案:

答案 0 :(得分:0)

我不太确定你在这里尝试做什么。如果他们将其中一个字段留空,您想要显示一个Toast消息,还要继续保存他们所做的输入的信息?

另外,有些想法......

我不认为你正在初始化你的name类变量。你在课堂上宣布它,但我认为

EditText et_name = (EditText) findViewById(R.id.et_name);
phone = et_name.getText().toString();

应该是

EditText et_name = (EditText) findViewById(R.id.et_name);
name = et_name.getText().toString(); 

此外,我不确定您为什么在此上下文中使用While语句而不仅仅是if

答案 1 :(得分:0)

我真的不明白你为什么使用while语句应该是if语句,你可以使用isEmpty方法检查字符串是否为空并执行其他操作...试试这个

    if(name.isEmpty() || phone.isEmpty() || email.isEmpty() || address.isEmpty()){

                        String error = "Contact data not added please try again";
                        Toast.makeText(NewContact.this, error, Toast.LENGTH_SHORT).show();

                    } else {

Boolean added  = handler.addContactDetails(contact);
                        if(added){
                        Intent intent = new Intent(NewContact.this, Main.class);
                        startActivity(intent);


}