Android:调用android Alert Dialog中的方法崩溃

时间:2016-04-18 09:35:01

标签: java android android-alertdialog

https://stackoverflow.com/questions/36542936/integrating-solr-to-hbase

我是Android的初学者,也是我的第一个应用程序,我想在我的项目中使用Alert Dialog,所以我可以包含用户从电话联系人中选择联系人的选项或在EditText视图中输入电话号码 else 块中的方法后面临应用程序崩溃,它向我显示androidRunTime的错误:NullPointerException其他部分正常工作,除了else块。

这是我的contact_main.java活动代码 请有人帮帮我!

提前谢谢

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

                );
        }

    }

1 个答案:

答案 0 :(得分:0)

更改final AlertDialog.Builder alert = new AlertDialog.Builder(this) ......

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

我不知道为什么,但我有这个问题,这对我有用 我也希望能为你工作。