2013-06-11 12 views
-1

아무도 도와 줄 수 있습니까? 난 임의의 숫자를 생성해야합니다 그리고 나서난수 생성기, android

public class MainActivity extends Activity implements OnClickListener { 

    TextView tv1; 
    Button bt1; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     bt1 = (Button) findViewById(R.id.button1); 
     bt1.setOnClickListener(this); 
     tv1 =(TextView) findViewById(R.id.textView1); 
    } 

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

    @Override 
    public void onClick(View v) { 
     switch(v.getId()){ 
      case R.id.button1: 
       Random r = new Random(); 
       int i1=r.nextInt(80-65) + 65; 
       tv1 = (TextView) findViewById(R.id.textView1); 
       tv1.setText(i1); 
       break;}; 
     } 
    } 

그리고 나서 그것을 누르면 버튼을 누르면 응용 프로그램이 닫힙니다.

+0

로그 파일 (CatLog' 바위)에서 무엇을 보았습니까 – ashes999

답변

3

tv1.setText(String.valueOf(i1)); 

로 변경

tv1.setText(i1); 

tv1.setText(i1); 당신은 string.xml 내부 ID i1와 문자열을 찾고 있습니다. 그 id은 아마도 존재하지 않으며 충돌을 일으킬 것입니다. ResourcesNotFoundException

편집 : onClick 내에서 동일한 초기화를 수행하므로 onClick 내의 tv1 = (TextView) findViewById(R.id.textView1);은 쓸모가 없습니다. 또한 Random r = new Random();을 onCreate 내부로 이동할 수 있습니다. 물론 r을 클래스 멤버로 유지해야합니다.

+0

'tv1 = (TextView) findViewById (R.id.textView1)'; 버튼을 클릭 할 때마다 초기화가 제거 될 수 있으며, 아마도'Random r = new Random()'; 버튼을 누르지 않고 이동할 수 있습니다. – Raghunandan

+0

@Raghunandan 당신이 맞습니다. – Blackbelt

+0

방금 ​​언급 했으므로 답변의 일부로 추가 할 수 있습니다. 당신이 슈퍼 대답했다 대답했다 – Raghunandan