在按键功能空指针异常 [英] Null Pointer Exception in button function

查看:114
本文介绍了在按键功能空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Button btnEditor;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    btnEditor = (Button) findViewById(R.id.btnEditor);

    //some code

    btnEditor.setOnClickListener(new View.OnClickListener(){
        public void onClick(View arg0) {

        }
    });
}

btnEditor.setOnClickListener(新View.OnClickListener()给我的空指针异常。 btnEditor早连接器通过XML按钮: btnEditor =(按钮)findViewById(R.id.btnEditor);

btnEditor.setOnClickListener(new View.OnClickListener() gives me a Null Pointer Exception. btnEditor is earlier connecter to XML Button by: btnEditor = (Button) findViewById(R.id.btnEditor);

在我的main.xml文件:

In my main.xml file:

<Button
        android:id="@+id/btnEditor"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="16dp"
        android:text="Editor"
        android:textSize="48dp"
        android:textStyle="bold"
        android:typeface="normal" android:layout_gravity="bottom"/>

说真的,我不知道该怎么办......

Seriously, I have no idea what to do...

决议:

我忘了,我有两个的main.xml 文件:

I forgot that I had two main.xml files:

  • / RES /布局
  • / RES /布局大

其中一人(大DIR)不包含按钮里,所以当运行的应用程序在设备上使用大布局我得到一个错误。

One of them (in large dir) didn't contain a Button inside, so I got an error while running application on device using large layout.

推荐答案

最有可能你已经不叫的setContentView()布局,这个按钮是要不你不叫的setContentView() $ C $前的这一行

Most likely you haven't called setContentView() with the layout that this Button is in or else you haven't called setContentView() before this line

btnEditor = (Button) findViewById(R.id.btnEditor);

以下任一情况下,将给予 NPE 在该行和将是它的唯一理由。如果你认为你是,那么请发表你是怎么做这个。

either of these situations would give a NPE at that line and would be the only reason for it. If you think you are then please post how you are doing this.

这篇关于在按键功能空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆