2016-04-03 13 views
-5

私はクイズアプリを50の質問で終えましたが、私は「再プレイ」ボタンを作ることはできません。私は2つの活動を使いました。最初の1つは、質問の50個のフラグメントをすべて含むメインのアクティビティと、スコアを示すスコアアクティビティである2番目のアクティビティです。スコア活動に私は "もう一度再生する" ボタンを作成しようとしている、と私はこのコードを試してみました:「再生」ボタンを作成するにはどうすればよいですか?

`publicクラスscoreActivityはAppCompatActivity {

int finalscorex; 
int x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17,x18,x19,x20,x21,x22,x23,x24,x25,x26,x27,x28,x29,x30; 
int x31,x32,x33,x34,x35,x36,x37,x38,x39,x40,x41,x42,x43,x44,x45,x46,x47,x48,x49,x50; 
String str; 
double qi; 
String qii; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_score); 

    Typeface mTypeface = Typeface.createFromAsset(getAssets(),"chlo.TTF"); 
    TextView myTextview = (TextView)findViewById(R.id.txtla); 
    myTextview.setTypeface(mTypeface); 

    Typeface m2Typeface = Typeface.createFromAsset(getAssets(),"chlo.TTF"); 
    TextView m2yTextview = (TextView)findViewById(R.id.txtla1); 
    m2yTextview.setTypeface(m2Typeface); 
    initControls(); 



} 




public void initControls() { 

    TextView final_score = (TextView) findViewById(R.id.textView103); 
    TextView final_scoreqi = (TextView) findViewById(R.id.textView104); 
    TextView lvlqi = (TextView) findViewById(R.id.textViewzzz); 
    Button btnxd = (Button)findViewById(R.id.btnx); 


    final SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(this); 

    int x1 = app_preferences.getInt("score1", 0); 
    int x2 = app_preferences.getInt("score2", 0); 
    int x3 = app_preferences.getInt("score3", 0); 
    int x4 = app_preferences.getInt("score4", 0); 
    int x5 = app_preferences.getInt("score5", 0); 
    int x6 = app_preferences.getInt("score6", 0); 
    int x7 = app_preferences.getInt("score7", 0); 
    int x8 = app_preferences.getInt("score8", 0); 
    int x9 = app_preferences.getInt("score9", 0); 
    int x10 = app_preferences.getInt("score10", 0); 
    int x11 = app_preferences.getInt("score11", 0); 
    int x12 = app_preferences.getInt("score12", 0); 
    int x13 = app_preferences.getInt("score13", 0); 
    int x14 = app_preferences.getInt("score14", 0); 
    int x15 = app_preferences.getInt("score15", 0); 
    int x16 = app_preferences.getInt("score16", 0); 
    int x17 = app_preferences.getInt("score17", 0); 
    int x18 = app_preferences.getInt("score18", 0); 
    int x19 = app_preferences.getInt("score19", 0); 
    int x20 = app_preferences.getInt("score20", 0); 
    int x21 = app_preferences.getInt("score21", 0); 
    int x22 = app_preferences.getInt("score22", 0); 
    int x23 = app_preferences.getInt("score23", 0); 
    int x24 = app_preferences.getInt("score24", 0); 
    int x25 = app_preferences.getInt("score25", 0); 
    int x26 = app_preferences.getInt("score26", 0); 
    int x27 = app_preferences.getInt("score27", 0); 
    int x28 = app_preferences.getInt("score28", 0); 
    int x29 = app_preferences.getInt("score29", 0); 
    int x30 = app_preferences.getInt("score30", 0); 
    int x31 = app_preferences.getInt("score31", 0); 
    int x32 = app_preferences.getInt("score32", 0); 
    int x33 = app_preferences.getInt("score33", 0); 
    int x34 = app_preferences.getInt("score34", 0); 
    int x35 = app_preferences.getInt("score35", 0); 
    int x36 = app_preferences.getInt("score36", 0); 
    int x37 = app_preferences.getInt("score37", 0); 
    int x38 = app_preferences.getInt("score38", 0); 
    int x39 = app_preferences.getInt("score39", 0); 
    int x40 = app_preferences.getInt("score40", 0); 
    int x41 = app_preferences.getInt("score41", 0); 
    int x42 = app_preferences.getInt("score42", 0); 
    int x43 = app_preferences.getInt("score43", 0); 
    int x44 = app_preferences.getInt("score44", 0); 
    int x45 = app_preferences.getInt("score45", 0); 
    int x46 = app_preferences.getInt("score46", 0); 
    int x47 = app_preferences.getInt("score47", 0); 
    int x48 = app_preferences.getInt("score48", 0); 
    int x49 = app_preferences.getInt("score49", 0); 
    int x50 = app_preferences.getInt("score50", 0); 

    finalscorex = x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10 + x11 + x12 + x13 + x14 + x15 + x16 + x17 + x18 + x19 + x20 
      + x21 + x22 + x23 + x24 + x25 + x26 + x27 + x28 + x29 + x30 + x31 + x32 + x33 + x34 + x35 + x36 + x37 + x38 + x39 + x40 
      + x41 + x42 + x43 + x44 + x45 + x46 + x47 + x48 + x49 + x50; 

    qi = (finalscorex*10/3); 
    qii = Double.toString(qi); 
     str =Integer.toString(finalscorex); 
    final_score.setText("Você acertou "+str+"/50 Questões"); 
    final_scoreqi.setText("Seu nível de Inglês é: " + qii); 

    if (finalscorex < 10){ 

     lvlqi.setText("Muito Baixo"); 
    } 
    else if (finalscorex > 10 && finalscorex < 20){ 

     lvlqi.setText("Baixo"); 
    } 

    else if(finalscorex > 20 && finalscorex < 30){ 

     lvlqi.setText("Normal"); 
    } 

    else if (finalscorex > 30 && finalscorex < 35){ 

     lvlqi.setText("Alto"); 
    } 

    else if (finalscorex>35 && finalscorex < 47){ 

     lvlqi.setText("Muito Alto"); 
    } 

    else { 
     lvlqi.setText("Seu Inglês é perfeito!"); 
    } 


    btnxd.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      app_preferences.edit().clear().commit(); 
      Intent intent1 = new Intent ("com.example.moresche.englishqigame.MainActivity"); 
      startActivity(intent1); 

     } 
    }); 

} 

} `

しかし拡張します"Play Again"ボタンをクリックすると、アプリがクラッシュし、重複したレイアウト(メインアクティビティの最初のフラグメント+フラグメントの問題50)で再び開きます。そして、断片の質問50がまだ実行中であることに気付きました。フラグメント化された質問50で、この複製と一緒にメインアクティビティの最初のフラグメントに戻すにはどうすればよいですか?フラグメント質問50を閉じるにはどうすればいいですか?

+0

「x1」から「x50」という名前の変数がたくさんあります。このような変数のリストのために配列を使うべきです。 – Jamie

+0

よろしくお願いします、私はこれをします。しかし、私の本当の問題はどうですか? –

+0

スタックトレースは、クラッシュしている場合は常に質問に追加してください。 –

答えて

0

もう一度ボタンを押すと、50個のフラグメントを最初のものに置き換える必要があります。

FragmentManager fm = getSupportFragmentManager(); 
    FragmentTransaction ft = fm.beginTransaction(); 
    ft.replace(R.id.fragment_container, fragmentB, tag); 
    ft.addToBackStack(null); 
    ft.commit(); 
関連する問題