2012-04-01 9 views
0

getFinボタンをクリックすると、getfinalという名前のxmlレイアウトに移動するように自分のコードを設定しました。しかし、そのボタンをクリックすると私のアプリケーションはクラッシュします。その状況で使うsetContentviewは間違ったコマンドですか?もしそうなら、どうすればボタンクリックで新しいxmlレイアウトに行くことができますか?On Click Listenerを使用して別のXMLレイアウトに移動する方法

package wilson.GC; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.view.View; 
import android.view.View.OnClickListener; 

public class GradeCalculatorActivity extends Activity { 
/** Called when the activity is first created. */ 
Button getEx, getFin; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    getEx = (Button)findViewById(R.id.getexambutton); 
    getFin = (Button)findViewById(R.id.getfinalbutton); 
    getFin.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      setContentView(R.layout.getfinal); 
      double q1, q2, ex, fin; 
      EditText etq1, etq2, eteg; 
      etq1 = (EditText)findViewById(R.id.editText1); 
       q1 = Double.parseDouble(etq1.getText().toString()); 
      etq2 = (EditText)findViewById(R.id.editText2); 
       q2 = Double.parseDouble(etq2.getText().toString()); 
      eteg = (EditText)findViewById(R.id.editText3); 
       ex = Double.parseDouble(eteg.getText().toString()); 

      fin = 0.4*q1+0.4*q2+0.2*ex; 
       if(fin == (int)fin){ 
        System.out.println((int)fin); 
       } 
       else{ 
        fin = 0.01*((int)(fin*100)); 
        System.out.println(fin); 
       } 
      TextView tvfin = null; 
      tvfin.setText(fin+""); 
      tvfin = (TextView)findViewById(R.id.tvfinalgrade); 

     } 
    }); 
} 
} 

両方のXMLページにコードが必要な場合は教えてください。

+0

は、あなたのクラッシュログは、それを試して病気 – Ishu

答えて

2

setContentView()を同じアクティビティで2回使用することはできません。完全に新しいアクティビティを作成し、setContentView()を使用してgetFinalレイアウトを割り当てる方が良いです。インテントを使用してボタンのonClick()でそのアクティビティを開きます。

+0

おかげで、貼り付けることができます! – Wilson

2

レイアウトgetfinal.xmlをmain.xmlにあるViewSwitcherに移動します。 onClickでは、ViewSwitchersのshowNext()メソッドを使用してレイアウトを切り替えます。

レイアウトファイルは次のようになります(sudo code)。

<ViewSwitcher android="@+id/switcher> 
    <LinearLayout android:id="@+id/first_layout"> ... first content with button ... </LinearLayout> 
    <LinearLayout android:id="@+id/second_layout"> ... content to show when pressed... </LinearLayout> 
</ViewSwitcher> 

は、その後の操作を行います。

setContentView(R.layout.main); 
    getEx = (Button)findViewById(R.id.getexambutton); 
    switch = (ViewSwitcher) findViewById(R.id.switcher); 
    getFin = (Button)findViewById(R.id.getfinalbutton); 
    getFin.setOnClickListener(new OnClickListener() { 

    public void onClick(View v) { 
     switch.showNext(); 
     ..... 
    }); 
関連する問題