cnm2001
手品目
采纳率100%
2015-03-19 16:30 阅读 3.2k

[安卓初学者]添加按钮监听 编译通过 运行空指针[已解决]布局里按钮id不要写成name

自学<第一行代码>到broadcastreceiver 添加按钮监听编译通过 运行提示空指针
把教材源代码复制到新android工程 问题依旧
肯定是添加按钮监听代码的问题 去掉之后可以正常运行

而且我还发现 现在编写最简单 按钮跳转activity都会报同样错误

虽然自学时间不长 可是自从学会看logcat 遇到的问题 都自己解决了
麻痹 这次折腾3天了 找不到问题 百度关键字 就找到一个类似的问题
http://bbs.csdn.net/topics/390808499 没看懂rootview什么意思

log如下

03-19 16:39:53.975: E/AndroidRuntime(5490): FATAL EXCEPTION: main
03-19 16:39:53.975: E/AndroidRuntime(5490): Process: av.jb.broadcastdemo, PID: 5490
03-19 16:39:53.975: E/AndroidRuntime(5490): java.lang.RuntimeException: Unable to start activity ComponentInfo{av.jb.broadcastdemo/av.jb.broadcastdemo.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
03-19 16:39:53.975: E/AndroidRuntime(5490): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
03-19 16:39:53.975: E/AndroidRuntime(5490): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
03-19 16:39:53.975: E/AndroidRuntime(5490): at android.app.ActivityThread.access$800(ActivityThread.java:144)
03-19 16:39:53.975: E/AndroidRuntime(5490): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
03-19 16:39:53.975: E/AndroidRuntime(5490): at android.os.Handler.dispatchMessage(Handler.java:102)
03-19 16:39:53.975: E/AndroidRuntime(5490): at android.os.Looper.loop(Looper.java:135)
03-19 16:39:53.975: E/AndroidRuntime(5490): at android.app.ActivityThread.main(ActivityThread.java:5221)
03-19 16:39:53.975: E/AndroidRuntime(5490): at java.lang.reflect.Method.invoke(Native Method)
03-19 16:39:53.975: E/AndroidRuntime(5490): at java.lang.reflect.Method.invoke(Method.java:372)
03-19 16:39:53.975: E/AndroidRuntime(5490): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
03-19 16:39:53.975: E/AndroidRuntime(5490): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
03-19 16:39:53.975: E/AndroidRuntime(5490): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
03-19 16:39:53.975: E/AndroidRuntime(5490): at av.jb.broadcastdemo.MainActivity.onCreate(MainActivity.java:34)
03-19 16:39:53.975: E/AndroidRuntime(5490): at android.app.Activity.performCreate(Activity.java:5933)
03-19 16:39:53.975: E/AndroidRuntime(5490): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
03-19 16:39:53.975: E/AndroidRuntime(5490): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
03-19 16:39:53.975: E/AndroidRuntime(5490): ... 10 more

代码如下

 package av.jb.broadcastdemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class MainActivity extends Activity {

    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {
        @Override
         public void onClick(View v) {
         Intent intent = new Intent("av.jb.broadcastdemo.MY_BROADCAST");
        sendBroadcast(intent);
            }
         });

    }



}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

4条回答 默认 最新

  • 已采纳
    u014222689 少聪 2015-03-20 07:35

    如果要是按钮报的null的话,可能是没有得到该按钮,你看下你的xml布局里面那个按钮写的是id还是name啊,如果是name就把那么改成id问题就解决了,你可以看下

    点赞 1 评论 复制链接分享
  • devmiao devmiao 2015-03-19 16:47

    检查下findViewById有没有找到

    点赞 3 评论 复制链接分享
  • yiyiyi130 mBenHero 2015-03-19 17:52

    因为没有导入资源引用,按快捷键Ctrl + Shift + O,选择av.jb.broadcastdemo.R进行导入

    点赞 评论 复制链接分享
  • mazhongshan 黔首小民 2015-03-20 01:28

    看看评论功能咋样大大大大大大大

    点赞 评论 复制链接分享

相关推荐