2011-08-05 43 views
0

这是我第一次在这个论坛上,所以如果我的问题看起来很奇怪,请原谅我。我会尽量做到尽可能彻底。 我正在创建一个翻译程序。 这个程序有一个菜单活动,翻译活动,addword活动。 这三个活动通过intent连接在一起,它们在清单文件中添加了 。 在翻译活动中,我想创建翻译方法。 当我按下翻译按钮后,程序崩溃。当一个函数被调用时,活动崩溃的问题

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); 

     } 
    }); 

} 

}

+1

你能后的错误消息,当这种情况发生之前,试试这个代码? – TofferJ

+0

你也可以发布清单文件吗? –

+0

不确定,但应该是: Intent intent = new Intent(view.getContext(),MenuActivity.class); – Joost

回答

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,导致应用程序崩溃。

+0

确定thx的答复。愚蠢的错误在我的角色。会试试这个。 – user880081

+1

我们都会偶尔做出愚蠢的错误。请确保接受答案,如果它可以帮助你解决你的问题。 – TofferJ

+0

thx回复。尝试了它,它就像一个魅力。 – user880081

1

在logcat中使用EDITTEXT场

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

thx此代码工作正常。 thx所有的帮助! – user880081

相关问题