Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android:在Android警报对话框中调用方法崩溃_Java_Android_Android Alertdialog - Fatal编程技术网

Java Android:在Android警报对话框中调用方法崩溃

Java Android:在Android警报对话框中调用方法崩溃,java,android,android-alertdialog,Java,Android,Android Alertdialog,我是android的初学者,也是我的第一个应用程序,我想在我的项目中使用Alert Dialog,这样我就可以包括用户从电话联系人中选择联系人或在EditText视图中输入电话号码的选项,但在我调用setPositiveButton ClickListener中的方法后,将面临应用程序崩溃,并显示androidRunTime的错误:NullPointerException除else块外,其他部分工作正常。这是我的联系人main.java活动代码。 谁能帮帮我吗 提前谢谢 package com.


我是android的初学者,也是我的第一个应用程序,我想在我的项目中使用Alert Dialog,这样我就可以包括用户从电话联系人中选择联系人或在EditText视图中输入电话号码的选项

,但在我调用setPositiveButton ClickListener中的方法后,将面临应用程序崩溃,并显示androidRunTime的错误:NullPointerException除else块外,其他部分工作正常。

这是我的联系人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;
                                       }
                                   }

                );
        }

    }
package com.example.ghaznavi.contacts;
导入android.app.Activity;
导入android.app.AlertDialog;
导入android.app.Dialog;
导入android.app.Fragment;
导入android.content.Context;
导入android.content.DialogInterface;
导入android.content.Intent;
导入android.os.Bundle;
导入android.provider.contacts合同;
导入android.text.method.PasswordTransformationMethod;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.widget.Button;
导入android.widget.CheckBox;
导入android.widget.CompoundButton;
导入android.widget.EditText;
导入android.widget.Toast;
公共类联系人主要扩展活动{
按钮对话框;
设置mSettings=新设置();
允许列表的私有静态最终int联系人\u选择器\u结果=301;
查看mActiveView=null;
Activity mActivity=null;
上下文mContext=null;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u contact\u main);
btnAlertDialog=(按钮)findViewById(R.id.btnAdd);
btnAlertDialog.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
contactPickerDialog();
}
});
}
公共无效联系人PickerDialog(){
LayoutInflater充气机=this.getLayoutInflater();
最终视图alertLayout=充气机。充气(R.layout.contact\u number\u input\u options,空);
final AlertDialog.Builder alert=新建AlertDialog.Builder(Contact_main.this).setTitle(“添加联系人号码”);
alert.setView(alertLayout);
alert.setPositiveButton(“确定”,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface paranameous,int-which){
字符串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();
副同名的。解散();
返回;
}否则{
Contact_main.this.mSettings.Initialize(Contact_main.this.mActivity);
Contact_main.this.mSettings.AddtoContactList(null,str);
联系_main.this.mSettings.SaveSettings();
Toast.makeText(Contact_main.this.getApplicationContext(),“它有效”,Toast.LENGTH_SHORT.show();
副同名的。解散();
返回;
}
}
}
);
final AlertDialog AlertDialog=alert.create();
alertDialog.show();
((按钮)alertLayout.findViewById(R.id.contact_input))。
setOnClickListener(新视图。OnClickListener(){
公共void onClick(视图v){
试一试{
如果((alertDialog!=null)&&(alertDialog.isShowing()){
alertDialog.disclose();
}
Intent localcontent=newintent(“android.Intent.action.PICK”,ContactsContract.Contacts.CONTENT\u URI);
联系本公司的startActivityForResult(Localitent,301);
返回;
}catch(异常localException){
alertDialog.disclose();
}
返回;
}
}
);
}
}

更改
最终警报对话框。生成器警报=新建警报对话框。生成器(此)

final AlertDialog.Builder alert=new AlertDialog.Builder(contactmain activity.this)

我不知道为什么,但我有这个问题,这对我起了作用
我也希望对您有用。

您在哪里初始化
mSettings
?我的理解正确吗?您可以从
EditText
获取文本?@0xDEADC0DE是的,我可以获取EditText的文本,并且我声明了此设置mSettings=new Settings();在contact_main block启动后POST full
contact_main
您的NPE在
初始化
功能中的某个位置。请发布是的,你是对的,问题出在我试图解决的AddtoContactList函数的代码中。我尝试了,但这不是问题,如果我从其他方法中删除了方法,它会工作,但我需要调用这些方法