Вопрос

I've followed a tutorial to realise a calculator. It seems that my buttons objects that i define, and instantiate with the ID are not instantiated because when i add a listener on them, they return a null pointer exception. here's the java code:

package com.example.calculette;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

//Variables declaration
Button b0;
Button b1;
Button b2;
Button b3;
Button b4;
Button b5;
Button b6;
Button b7;
Button b8;
Button b9;
Button bC;
Button bCE;
Button bEgal;
Button bPlus;
Button bMoins;
Button bDiviser;
Button bMultiplier;

EditText saisie;

private double number1 = 0;
private boolean clicOperateur = false;
private boolean update = true;
private String operateur = "";

//onCreate is the first procedure called by the activity
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    b0 = (Button) findViewById(R.id.button0);
    b1 = (Button) findViewById(R.id.button1);
    b2 = (Button) findViewById(R.id.button2);
    b3 = (Button) findViewById(R.id.button3);
    b4 = (Button) findViewById(R.id.button4);
    b5 = (Button) findViewById(R.id.button5);
    b6 = (Button) findViewById(R.id.button6);
    b7 = (Button) findViewById(R.id.button7);
    b8 = (Button) findViewById(R.id.button8);
    b9 = (Button) findViewById(R.id.button9);
    bC = (Button) findViewById(R.id.buttonC);
    bCE = (Button) findViewById(R.id.buttonCE);
    bEgal = (Button) findViewById(R.id.buttonEgal);
    bPlus = (Button) findViewById(R.id.buttonPlus);
    bMoins = (Button) findViewById(R.id.buttonMoins);
    bDiviser = (Button) findViewById(R.id.buttonDiviser);
    bMultiplier = (Button) findViewById(R.id.buttonMultiplier);

    saisie = (EditText) findViewById(R.id.EditText01);


    //Listeners declaration
    b0.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            numberClick("0");
        }
    });

(i don't link the whole code because it's still the same for the 9 buttons and after it's simple methods).

here you can see the XML associated:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TableLayout
  android:layout_width="fill_parent"
    android:layout_height="wrap_content"
  android:stretchColumns="3">

    <EditText android:id="@+id/EditText01"
        android:layout_width="fill_parent"
        android:layout_height="80px"
        android:textSize="20px"
        android:editable="false"
        android:cursorVisible="false"
        />

  <View
    android:layout_height="3dip"
    android:background="#FF999999"
  />

    <TableRow>
        <Button android:id="@+id/buttonCE"
            android:text="CE"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_span="2"
            style="@style/button_text"
            android:background="@drawable/button"
        />
        <Button android:id="@+id/buttonC"
            android:text="C"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_span="2"
            style="@style/button_text"
            android:background="@drawable/button"
        />
    </TableRow>
    <TableRow>
        <Button android:id="@+id/button7"
            android:text="7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/button_text"
            android:background="@drawable/button"
        />
        <Button android:id="@+id/button8"
            android:text="8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/button_text"
            android:background="@drawable/button"
        />
        <Button android:id="@+id/button9"
            android:text="9"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/button_text"
            android:background="@drawable/button"
        />
        <Button android:id="@+id/buttonPlus"
            android:text="+"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            style="@style/button_text"
            android:background="@drawable/button"
        />
    </TableRow>

    <TableRow>
         <Button android:id="@+id/button4"
            android:text="4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/button_text"
            android:background="@drawable/button"
        />
        <Button android:id="@+id/button5"
            android:text="5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/button_text"
            android:background="@drawable/button"
        />
        <Button android:id="@+id/button6"
            android:text="6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/button_text"
            android:background="@drawable/button"
        />
        <Button android:id="@+id/buttonMoins"
            android:text="-"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            style="@style/button_text"
            android:background="@drawable/button"
        />
    </TableRow>

    <TableRow>
         <Button android:id="@+id/button1"
            android:text="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/button_text"
            android:background="@drawable/button"
        />
        <Button android:id="@+id/button2"
            android:text="2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/button_text"
            android:background="@drawable/button"
        />
        <Button android:id="@+id/button3"
            android:text="3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/button_text"
            android:background="@drawable/button"
        />
        <Button android:id="@+id/buttonMultiplier"
            android:text="*"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            style="@style/button_text"
            android:background="@drawable/button"
        />
    </TableRow>

    <TableRow>
         <Button android:id="@+id/button0"
            android:text="0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/button_text"
            android:background="@drawable/button"
        />
        <Button android:id="@+id/buttonPoint"
            android:text="."
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/button_text"
            android:background="@drawable/button"
        />
        <Button android:id="@+id/buttonEgal"
            android:text="="
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/button_text"
            android:background="@drawable/button"
        />
        <Button android:id="@+id/buttonDiviser"
            android:text="/"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            style="@style/button_text"
            android:background="@drawable/button"
        />
    </TableRow>

</TableLayout>

<ImageView 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent" 
    android:src="@drawable/logosafran"
    android:id="@+id/imageView1"
></ImageView>

</LinearLayout>

The logcat says that i have a nullPointerException on the first listener declaration with b0.

Thank you guys!

Это было полезно?

Решение

The problem is that in your activity you're trying to inflate the activity_main.xml file which obviously doesnt'hold your button declaration. You told that your buttons are defined into a button_classical.xml file.

You would have to change

setContentView(R.layout.activity_main);

in

setContentView(R.layout.button_classical);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top