안드로이드 개발 질문/답변
(글 수 45,052)
package com.test.nansu;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Testnansu extends Activity
{
private int i;
Random oRandom = new Random();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView ntext = (TextView)findViewById(R.id.ntext);
findViewById(R.id.nbtn).setOnClickListener(buttonClick);
}
// 버튼클릭
Button.OnClickListener buttonClick = new Button.OnClickListener()
{
public void onClick(View view)
{
switch (view.getId())
{
case R.id.nbtn:
i = oRandom.nextInt(3) + 1;
ntext.setText(i);
break;
}
}
};
}
이런식으로 코딩중인데 ntext 에서 오류나더라고요
오류는 ntext cannot be resolved 인데
ntext를 참조라고 해야되나 할 수 없는거잖아요
이거를 위에 int i 처럼
전역변수라고해야되나..이 클래스에서만 사용할수있게 하고싶은데 어떻게 해야되나요?
코드최적화라해서 글을 읽어본게 있어서 적응시켜보려는데
UI들은 어떻게 변수를 지정하는지 잘모르겠씁니다




TextView ntext ;
-----
그리고나서
TextView ntext = (TextView)findViewById(R.id.ntext); 대신
ntext = (TextView)findViewByid(R.id.ntext); 로 바꾸시면 됩니다....