2016-04-03 97 views
-5

我刚刚用50个问题完成了我的测验应用程序,但我无法进行“再次播放”按钮。我使用了两个Acitivities。第一个是包含所有50个问题片段的主要Acitivity,第二个活动是评分活动,显示评分。在评分活动,我想创建一个 “再玩一次” 按钮,我想这样的代码:如何创建“再次播放”按钮?

'公共类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); 

     } 
    }); 

} 

} `

但是,当我点击“再次播放”按钮,应用程序崩溃并重新打开重复布局(主要活动第一个片段+片段问题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();