简体   繁体   中英

Android: Calling methods inside android Alert Dialog crashing

here is the error log

I m beginner in android and its my first application, i want to use Alert Dialog in my project so i could include the options for user to pick contact from phone contact or enter phone number in EditText view

But facing application crashing after i call methods in else block of setPositiveButton ClickListener and it shows me the error of androidRunTime: NullPointerException other parts are working perfectly except else block.

here is my contact_main.java activity codes.
can anybody help me please!

thanks in advance

package com.example.ghaznavi.contacts;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.Fragment;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.text.method.PasswordTransformationMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

public class Contact_main extends Activity {
    Button btnAlertDialog;
    Settings mSettings = new Settings();
    private static final int CONTACT_PICKER_FOR_ALLOWED_LIST_RESULT = 301;
    View mActiveView = null;
    Activity mActivity = null;
    Context mContext = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_main);

        btnAlertDialog = (Button) findViewById(R.id.btnAdd);
        btnAlertDialog.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                contactPickerDialog();
            }
        });
    }

    public void contactPickerDialog() {
        LayoutInflater inflater = this.getLayoutInflater();
        final View alertLayout = inflater.inflate(R.layout.contact_number_input_options, null);

        final AlertDialog.Builder alert = new AlertDialog.Builder(Contact_main.this).setTitle("Add Contact Number");
        alert.setView(alertLayout);
        alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface paramAnanymous, int which) {

                        String str = ((EditText) alertLayout.findViewById(R.id.typed_number_input)).getText().toString();
                        if ((str == null) || (str.length() < 3)) {
                            Toast.makeText(Contact_main.this.getApplicationContext(), str.toString(), Toast.LENGTH_SHORT).show();
                            paramAnanymous.dismiss();
                            return;
                        } else {

                            Contact_main.this.mSettings.Initialize(Contact_main.this.mActivity);
                            Contact_main.this.mSettings.AddtoContactList(null, str);
                            Contact_main.this.mSettings.SaveSettings();
                            Toast.makeText(Contact_main.this.getApplicationContext(), "it works", Toast.LENGTH_SHORT).show();
                            paramAnanymous.dismiss();
                            return;
                        }

                    }
        }
        );

        final AlertDialog alertDialog = alert.create();
        alertDialog.show();

        ((Button)alertLayout.findViewById(R.id.contact_input)).
                setOnClickListener(new View.OnClickListener() {

                                       public void onClick(View v) {
                                           try {
                                               if ((alertDialog != null) && (alertDialog.isShowing())) {
                                                   alertDialog.dismiss();
                                               }
                                               Intent localIntent = new Intent("android.intent.action.PICK", ContactsContract.Contacts.CONTENT_URI);
                                               Contact_main.this.startActivityForResult(localIntent, 301);
                                               return;
                                           } catch (Exception localException) {
                                               alertDialog.dismiss();
                                           }
                                           return;
                                       }
                                   }

                );
        }

    }

change final AlertDialog.Builder alert = new AlertDialog.Builder(this) ......

to

final AlertDialog.Builder alert = new AlertDialog.Builder(ContactMainActivity.this) ....

i dont know why but i had this problem and this worked to me i hope work to you too.

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