2014-03-30 3 views
1

맞춤 번호 선택 도구를 만들려고하는데 + 또는 - 버튼을 클릭 할 때마다 앱이 다운됩니다. MainActivity.java에서 오류가 발생하지 않습니다. 누군가가 어떤 상황인지 알 수 있습니까? 당신은 인수로 CharSequence을 기대 setText 메소드를 호출 할 필요가버튼을 클릭하면 앱이 깨집니다.

public class MainActivity extends Activity { 

    TextView tvHeight; 
    int counter = 0; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Button heightMinus = (Button) findViewById(R.id.height_min); 
     Button heightPlus = (Button) findViewById(R.id.height_plus); 
     tvHeight = (TextView) findViewById(R.id.textViewHeight); 

     heightPlus.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       counter++; 
       tvHeight.setText(counter); 

      } 
     }); 

     heightMinus.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       counter--; 
       tvHeight.setText(counter); 

      } 
     }); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 
} 
+0

logcat 출력을 살펴 보았습니까? – donfuxx

+0

왜 이것을 'numberpicker'에 태그 했습니까? – Raghunandan

답변

3

변경이

tvHeight.setText(counter); 

tvHeight.setText(String.valueOf(counter)); 

public final void setText (int resid) 

그리고이

public final void setText (CharSequence text) 
,

첫 번째 방법으로 안드로이드는 id가 발견되지 않으면 id가있는 리소스를 찾습니다. ResourceNotFoundException이 표시됩니다. 리소스 ID 인 int를 사용하는 것이 있고 CharacterSequecne을 사용하는 다른 것이 있습니다.

+1

'counter + ""도 잘 작동해야합니다. – donfuxx

+0

잘 설명해 주셔서 감사합니다. – Markonionini

3

: 다음은 코드입니다. 그래서

tvHeight.setText(String.valueOf(counter)); 

는 현재 당신이 당신의 strings.xml 파일에 정의 된 문자열 리소스 ID를 찾기 위해 노력할 것입니다 setText(int resid)를 호출하고 함께

tvHeight.setText(counter); 

를 교체합니다.

그래서 코드에 ResourceNotFoundException이 발생합니다. 방법에

+0

고마워요. 이것은 매우 도움이되었습니다. – Markonionini

관련 문제