我按照教程來實現一個計算器。看來我的按鈕對象,我定義,並與ID實例化沒有實例化,因爲當我添加一個偵聽器,他們返回一個空指針異常。 這裏的java代碼:id的實例化問題
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");
}
});
(我,因爲它仍然是在9個按鈕一樣,它的簡單方法後不整的代碼鏈接)。
在這裏你可以看到相關的XML:
<?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>
的logcat的說,我已經在與B0第一個監聽聲明一個NullPointerException。
謝謝你們!
這是對activity_main.xml中? – edoardotognoni 2013-05-02 09:08:18
nop它是一個button_classical.xml,我定義了創建我的tableLayout與所有按鈕在 – Eydolol 2013-05-02 09:14:48