【问题标题】:problem with activity crashing when a function is called调用函数时活动崩溃的问题
【发布时间】:2011-10-20 16:19:14
【问题描述】:

这是我第一次上这个论坛,如果我的问题看起来很奇怪,请原谅我。我会尽量做到彻底。 我正在创建一个翻译程序。 这个程序有一个菜单活动,翻译活动,添加词活动。 这三个活动通过意图链接在一起,它们是 在清单文件中添加。 在翻译活动中,我想创建一种翻译方法。 在我按下翻译按钮后,程序崩溃了。

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.indexOf(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);

        }
    });

}

}

【问题讨论】:

  • 发生这种情况时能否在 LogCat 中发布错误消息?
  • 能否也请您发布清单文件?
  • 不确定,但应该是这样吗?:Intent intent = new Intent(view.getContext(),MenuActivity.class);
  • 今晚我会改变它,看看会发生什么。到目前为止,代码可以正常工作,感谢大家的帮助。还将发布清单文件。它仍在进行中,我想我需要编辑清单文件。

标签: android methods android-intent


【解决方案1】:

我看不到您从 XML 中获取 EditTexts(就像您使用按钮一样)。在使用 ET_NL 之前,您需要执行以下操作:

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

ET_EN 变量也是如此。否则,您的 Vertaal() 方法中将为 null,导致应用崩溃。

【讨论】:

  • 好的谢谢你的回复。我犯了一个愚蠢的错误。会试试这个。
  • 我们都会时不时地犯一些愚蠢的错误。如果它可以帮助您解决问题,请确保接受答案。
【解决方案2】:

在使用 editText 字段之前尝试此代码

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

【讨论】:

    猜你喜欢
    • 2013-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多