2012-01-11 8 views
0

Eclipse에서 내 코드를 작성하면 응용 프로그램이 예기치 않게 중지되었다는 오류가 에뮬레이터에서 발생했습니다. sdk에 대한 설치 오류가 없습니다. 여기 내 코드가있다.Android 응용 프로그램이 예기치 않게 중지되었습니다.

public class startingPoint extends Activity { 
    /** Called when the activity is first created. */ 

    int counter1,counter2; 
    Button add; 
    Button sub; 
    TextView display; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     counter1=counter2 = 0; 
     add = (Button) findViewById(R.id.bAdd); 
     add = (Button) findViewById(R.id.bSub); 
     display = (TextView) findViewById(R.id.tvDisplay); 
     add.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View arg0) { 
       counter1=counter1+1; 
       display.setText("Your total is "+counter1); 

      } 
     }); 
     sub.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View arg0) { 
       counter2=counter2-1; 
       display.setText("Your total is "+counter2); 
      } 
     }); 
    } 
} 
+0

로그 캣 오류가 무엇을 : 코드를 수정? – JoxTraex

답변

1

동일한 변수에 새 값을 두 번 할당합니다.

add = (Button) findViewById(R.id.bAdd); 
add = (Button) findViewById(R.id.bSub); 

나는이 있어야한다고 생각 : sub가 null 인 NullPointerException 때문에 코드에서

add = (Button) findViewById(R.id.bAdd); 
sub = (Button) findViewById(R.id.bSub); 

sub.setOnClickListener가 발생합니다.

1

당신은 당신이 다음 질문에 대해 sub

add = (Button) findViewById(R.id.bAdd); 
add = (Button) findViewById(R.id.bSub); // should be sub instead of add 

를 초기화하지, 복사 & 붙여 넣기 오류가, 당신의 로그 캣를 살펴보고이 쉽게 오류를 찾을 수 우리에게 도움이 될 수로 스택 추적을 게시하시기 바랍니다.

1

초기화 된 하위 변수가 없어 nullPointer 예외가 발생합니다.

add = (Button) findViewById(R.id.bAdd); 
add = (Button) findViewById(R.id.bSub); 

add = (Button) findViewById(R.id.bAdd); 
sub = (Button) findViewById(R.id.bSub); 
관련 문제