如何在Android中正确使用类

时间:2013-04-11 15:44:39

标签: android class nullpointerexception findviewbyid

我是编程和Android的新手。我已经建立了一个小数钱的应用程序。 目前它只是一个大文件,其中包含许多方法。 所以我想我会把它整理一下然后去上课。 我有这个clearAll方法,我把它放在ClearAll类中:

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 clear = new ClearAll();
clear.clearAll();

但是当调用它时,app会在findViewById上发生NullPointerException崩溃。 我一直在寻找一段时间,它给我留下了比我开始时更多的问题。 看来你不能使用findViewById ouside主要活动,因为你还没有设置setContentView()。 我不会这样做,因为它不是主要的活动。 我还尝试在一个单独的类中放入一个简单的toast并调用它,但这也会崩溃。 所以我只是做得不对。

有人愿意给我一些关于如何在Android中正确使用类的指示吗?

干杯,

大安

2 个答案:

答案 0 :(得分:0)

你需要在onCreate()中使用setContentView(R.layout.activity_main),然后通过findViewById()获取id,然后将text设置为null。

由于您尚未设置布局,因此无法通过findViewById()获取ID。因此您将获得nullpointerexception。

  public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
            clearAll();
           }
       public void clearAll() {
         // Set EditTexts to ""
         ((EditText) findViewById(R.id.euro500)).setText("");
         ((EditText) findViewById(R.id.euro200)).setText("");

           }

   }

答案 1 :(得分:0)

这不是关于android的,这纯粹是关于java的。您必须了解实例如何协同工作。

您的ClearAll实例不包含任何内容。

您的clearAll方法必须在其工作的范围内。它使用findViewById,表示范围应该是当前活动。

当前活动是(我猜)您调用ClearAll的MainActivity实例。

有几种选择:

  1. 在MainActivity中放回clearAll
  2. 使ClearAll成为扩展Activity的抽象类。使MainActivity扩展ClearAll。从MainActivity,您现在可以致电this.clearAll()
  3. 使ClearAll成为一个不扩展任何内容的抽象类。 make clearAll是一个以Activity作为参数的静态方法。使用此参数在其上调用findViewById。从MainActivity,您现在可以调用ClearAll.clearAll(this)
  4. 我建议选项1.如果您将在多个类中使用此方法,则首选选项2和3。

    另外,关于减少代码量,更重要的是减少重复行,我建议为EditText创建一个Collection(数组,列表...),为TextView创建另一个,并循环这个集合清除内容。

    (在MainActivity中):

    private final static int[] EDIT_IDS = new int[] {R.id.euro500, ... }
    private final static int[] TEXT_IDS = new int[] {R.id.tvEuro500Totaal, ... }
    
    private void clearAll() {
        for (int id : EDIT_IDS) {
            ((EditText) findViewById(id)).setText("");
        }
        for (int id : TEXT_IDS) {
            ((TextView) findViewById(id)).setText(R.string.puntjes);
        }
    }