在按键功能空指针异常指针、按键、异常、功能

2023-09-07 14:51:54 作者:Roy?

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.