0

나는 계산기를 실현하기위한 튜토리얼을 따라 갔다. 그것은 내 단추 개체를 정의 할 및 ID를 인스턴스화 할 것 같지 않습니다 왜냐하면 그들에 수신기를 추가 할 때 그들은 null 포인터 예외를 반환하기 때문에 인스턴스화되지 않습니다. 여기에 자바 코드입니다 :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> 

로그 캣 내가 B0 가진 첫번째 리스너 선언에 NullPointerException이 있다고 말한다.

감사합니다.

+0

setContentView(R.layout.activity_main); 

을 변경해야 할 것? – edoardotognoni

+0

nop 그것은 모든 단추가있는 내 tableLayout을 만들도록 정의한 button_classical.xml입니다. – Eydolol

답변

0

문제는 귀하의 활동에서 분명히 버튼 선언을지지하지 않는 activity_main.xml 파일을 팽창 시키려고한다는 것입니다. 단추는 button_classical.xml 파일에 정의되어 있다고 말했습니까?

이이 activity_main.xml가

setContentView(R.layout.button_classical); 
+0

저는 너무 바보입니다.이 U_U를 완전히 잊어 버렸습니다. 파일 사이의 링크가 아니라 내 단추를 선언하고 인스턴스화하는 방법에 대해서만 생각했습니다. . 대단히 감사합니다 edoardotognoni! – Eydolol

+0

여러분을 환영합니다! – edoardotognoni