프로그래밍과 Android를 처음 사용합니다. 돈을 계산할 수있는 작은 앱을 만들었습니다. 그 안에 많은 방법으로가는 길에 하나의 큰 파일 만 있습니다. 그래서 나는 그것을 조금 정리하고 수업에 물건을 넣을 것이라고 생각했습니다. Android에서 클래스를 올바르게 사용하는 방법
import android.widget.EditText;
import android.widget.TextView;
public class ClearAll extends MainActivity {
public void clearAll() {
// Set EditTexts to ""
((EditText) findViewById(R.id.euro500)).setText("");
((EditText) findViewById(R.id.euro200)).setText("");
((EditText) findViewById(R.id.euro100)).setText("");
((EditText) findViewById(R.id.euro50)).setText("");
((EditText) findViewById(R.id.euro20)).setText("");
((EditText) findViewById(R.id.euro10)).setText("");
((EditText) findViewById(R.id.euro5)).setText("");
((EditText) findViewById(R.id.euro2)).setText("");
((EditText) findViewById(R.id.euro1)).setText("");
((EditText) findViewById(R.id.cent50)).setText("");
((EditText) findViewById(R.id.cent20)).setText("");
((EditText) findViewById(R.id.cent10)).setText("");
((EditText) findViewById(R.id.cent5)).setText("");
((EditText) findViewById(R.id.cent2)).setText("");
((EditText) findViewById(R.id.cent1)).setText("");
// Set TextViews to "0.00"
((TextView) findViewById(R.id.tvEuro500Totaal)).setText(R.string.puntjes);
((TextView) findViewById(R.id.tvEuro200Totaal)).setText(R.string.puntjes);
((TextView) findViewById(R.id.tvEuro100Totaal)).setText(R.string.puntjes);
((TextView) findViewById(R.id.tvEuro50Totaal)).setText(R.string.puntjes);
((TextView) findViewById(R.id.tvEuro20Totaal)).setText(R.string.puntjes);
((TextView) findViewById(R.id.tvEuro10Totaal)).setText(R.string.puntjes);
((TextView) findViewById(R.id.tvEuro5Totaal)).setText(R.string.puntjes);
((TextView) findViewById(R.id.tvEuro2Totaal)).setText(R.string.puntjes);
((TextView) findViewById(R.id.tvEuro1Totaal)).setText(R.string.puntjes);
((TextView) findViewById(R.id.tvCent50Totaal)).setText(R.string.puntjes);
((TextView) findViewById(R.id.tvCent20Totaal)).setText(R.string.puntjes);
((TextView) findViewById(R.id.tvCent10Totaal)).setText(R.string.puntjes);
((TextView) findViewById(R.id.tvCent5Totaal)).setText(R.string.puntjes);
((TextView) findViewById(R.id.tvCent2Totaal)).setText(R.string.puntjes);
((TextView) findViewById(R.id.tvCent1Totaal)).setText(R.string.puntjes);
((TextView) findViewById(R.id.tvTotaalBedrag)).setText(R.string.puntjes);
}
}
그래서 내 MainActivity에서 내가 할 : 나는 내가 ClearAll 클래스에 넣어이 clearAll 방법이
ClearAll clear = new ClearAll();
clear.clearAll();
을하지만이 같이 findViewById에 NullPointerException이와 응용 프로그램의 충돌을 호출 할 때. 저는 지금 잠시 동안 수색을 해왔고 시작했을 때보 다 더 많은 질문을했습니다. setContentView()를 설정하지 않았으므로 findViewById는 기본 활동을 ouside 사용할 수없는 것으로 보입니다. 그것은 주요 활동이 아니기 때문에 제가하지 않을 것입니다. 나는 또한 간단한 토스트를 별도의 클래스에 넣고 그 것을 호출하려고 시도했지만, 충돌 할 수도있다. 그래서 나는 제대로하지 않을 것입니다.
안드로이드에서 클래스를 올바르게 사용하는 방법에 대한 조언을 해 줄 사람이 있습니까?
건배,
Daan
고마워요. 코드가 어떻게 생겼는지 되돌리고 다른 방법으로 실험 해 보겠습니다. clearAll 메서드에 대한 루프를 만드는 것에 대해 이미 생각하고있었습니다. 예제를 위해 큰 감사드립니다! – stofstik