Describe an error from my past projects

1、前言

  最近在做安卓APP开发,作为安卓入门级的新手,开发过程中犯的错误当然是层出不穷甚至千奇百怪。下面我就挑选一个最近出现的、印象也最深刻的编程错误谈谈感想。

2、正文 

  问题描述:在安卓APP页面中点击一个添加按钮A后弹出一个对话框(对话框定义代码如下所示),点击对话框内确定按钮B执行某些操作,然而实际上每次点击按钮B都会导致程序崩溃,logcat显示的错误信息如图所示。

 public void openSecure(View v) {
        vibrator.vibrate(VIBRATE_TIME);
        if (userInfo != null && !TextUtils.isEmpty(userInfo.getUserid())) {
            LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
            View view = inflater.from(this).inflate(R.layout.self_defined_secure, null);
//          ImageButton btn_phone = (ImageButton) findViewById(R.id.self_defined_secure_phone); 错误写法
            ImageButton btn_phone = (ImageButton) view.findViewById(R.id.self_defined_secure_phone);
//          ImageButton btn_phone = (ImageButton) findViewById(R.id.self_defined_secure_phone); 错误写法
            ImageButton btn_password = (ImageButton) view.findViewById(R.id.self_defined_secure_password);

     new AlertDialog.Builder(this) .setView(view) .show(); btn_password.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { vibrator.vibrate(VIBRATE_TIME); Intent intent = new Intent(HomePage.this, Validate.class); intent.putExtra("option", "PWD"); startActivity(intent); } }); btn_phone.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { vibrator.vibrate(VIBRATE_TIME); Intent intent = new Intent(HomePage.this, Validate.class); intent.putExtra("option", "PHONE"); startActivity(intent); } }); } else { offLine(); } }
 1  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
 2                                                                              at com.yeong.chen.giftbook.HomePage.openSecure(HomePage.java:147)
 3                                                                              at java.lang.reflect.Method.invoke(Native Method) 
 4                                                                              at android.view.View$DeclaredOnClickListener.onClick(View.java:4502 5                                                                              at android.view.View.performClick(View.java:5267 6                                                                              at android.view.View$PerformClick.run(View.java:21540 7                                                                              at android.os.Handler.handleCallback(Handler.java:815 8                                                                              at android.os.Handler.dispatchMessage(Handler.java:104 9                                                                              at android.os.Looper.loop(Looper.java:20710                                                                              at android.app.ActivityThread.main(ActivityThread.java:571011                                                                              at java.lang.reflect.Method.invoke(Native Method) 
12                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:90013                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761) 

  问题分析:查阅输出的错误信息不难发现错误原因是对象空指针造成的,进过仔细分析之后找到了错误原因是由于对话框中两个ImageButton只有申明,并未实例化。导致错误的代码代码如下:

1        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
2             View view = inflater.from(this).inflate(R.layout.self_defined_secure, null);
3             ImageButton btn_phone = (ImageButton) findViewById(R.id.self_defined_secure_phone);
4             ImageButton btn_password = (ImageButton) findViewById(R.id.self_defined_secure_password);

代码的正确写法如下:

       LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
            View view = inflater.from(this).inflate(R.layout.self_defined_secure, null);
            ImageButton btn_phone = (ImageButton) view.findViewById(R.id.self_defined_secure_phone);
            ImageButton btn_password = (ImageButton) view.findViewById(R.id.self_defined_secure_password);

  解决方法:通过分析我发现俩个ImageButton的xml代码处于自定义的xml文件中,并不在当前Activity所处的context中,因此直接只用findViewById方法无法找到俩按钮的id,故而导致出现空指针错误。因此正确的做法是首先获取自定义xml的实例,然后通过该实例调用findViewById方法查找ImageButton的id。

3、总结

  刚开始出现这种错误着实让人感到莫名其妙,在代码每次都能正常编译且不会报错情况下,反反复复报错重试再报错再重试浪费了很多时间,最终找到导致空指针的根本原因。这个错误确实让我印象深刻,也深受启发。

posted @ 2017-04-25 14:01  CoderYeong  阅读(182)  评论(0编辑  收藏  举报