开发者

problem with activity crashing when a function is called

开发者 https://www.devze.com 2023-03-26 06:36 出处:网络
this is my first time on this forum, so forgive me if my question seems odd. I\'ll try to be as thorough as possible.

this is my first time on this forum, so forgive me if my question seems odd. I'll try to be as thorough as possible. I am creating a translation program. this program has a menu activity, translate activity, addword activity. The three activities are linked together via intents and they are added in the manifest file. In the translate activity I want to create a method for translating. After I press the translate button, the program crashes.

public class VertaalActivity extends Activity {
private Button vertaal;
private Button terug;
private EditText ET_NL;
private EditText ET_EN;
private ArrayList<String>nlWoorden = new ArrayList<String>();
private ArrayList<String>enWoorden = new ArrayList<String>();

public void Vertaal(){

    String woord = ET_NL.getText().toString();

        if(nlWoorden.contains(woord)){
            int i = nlWoorden.in开发者_如何学编程dexOf(woord);
            ET_EN.setText(enWoorden.get(i));
        }else{
            ET_EN.setText("Woord niet gevonden");
        }

}

public void ArrayVullen(){
    nlWoorden.add("auto");
    nlWoorden.add("bord");
    nlWoorden.add("trein");
    nlWoorden.add("spel");
    nlWoorden.add("scherm");
    nlWoorden.add("toetsenbord");
    nlWoorden.add("foto");
    enWoorden.add("car");
    enWoorden.add("plate");
    enWoorden.add("train");
    enWoorden.add("game");
    enWoorden.add("screen");
    enWoorden.add("keyboard");
    enWoorden.add("picture");
}

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.vertalerlayout);
    terug = (Button)findViewById(R.id.terug);
    vertaal = (Button)findViewById(R.id.vertalen);

    ArrayVullen();

    vertaal.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            Vertaal();

            /*
             * Tested the toast and the toast shows the text 
             * 
            Context context = getApplicationContext();
            CharSequence text = "Hello toast!";
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
            */
        }

    });

    terug.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            Intent intent = new Intent(VertaalActivity.this,MenuActivity.class);
            startActivity(intent);

        }
    });

}

}


I can't see that you get the EditTexts from your XML (like you do with the buttons). Before using ET_NL you need to do something like this:

ET_NL = (EditText)findViewById(R.id.etnl); // Or whatever id you've declared in your layout XML

Same thing goes for the ET_EN variable. Otherwise the will be null in your Vertaal() method, causing the app to crash.


Try this code before using the editText field

ET_NL= (EditText)findViewById(R.id.edittext1);
ET_EN = (EditText)findViewById(R.id.edittext2);
0

精彩评论

暂无评论...
验证码 换一张
取 消