2016-03-07 51 views
0

我想为老师(我的妈妈)提供一个应用程序来计算等级字母和一定数量的可能数量错误的百分比。按下第一个屏幕上的按钮后,应用程序崩溃。我想知道是否有人可以帮助我。不适用于Android Studio的教师的百分比应用

import android.content.Context; 
import android.media.MediaPlayer; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class MainActivity extends AppCompatActivity{ 

int MaxInt; 

private Button button; 

private EditText ETmax; 

private TextView TV1; 
private TextView TV2; 
private TextView TV3; 
private TextView TV4; 
private TextView TV5; 
private TextView TV6; 
private TextView TV7; 
private TextView TV8; 
private TextView TV9; 
private TextView TV10; 
private TextView TV11; 
private TextView TV12; 
private TextView TV13; 
private TextView TV14; 
private TextView TV15; 
private TextView TV16; 
private TextView TV17; 
private TextView TV18; 
private TextView TV19; 
private TextView TV20; 
private TextView TV21; 
private TextView TV22; 
private TextView TV23; 
private TextView TV24; 

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

    ETmax = (EditText) findViewById(R.id.editText); 

    TV1 = (TextView) findViewById(R.id.textView); 
    TV2 = (TextView) findViewById(R.id.textView2); 
    TV3 = (TextView) findViewById(R.id.textView3); 
    TV4 = (TextView) findViewById(R.id.textView4); 
    TV5 = (TextView) findViewById(R.id.textView5); 
    TV6 = (TextView) findViewById(R.id.textView6); 
    TV7 = (TextView) findViewById(R.id.textView7); 
    TV8 = (TextView) findViewById(R.id.textView8); 
    TV9 = (TextView) findViewById(R.id.textView9); 
    TV10 = (TextView) findViewById(R.id.textView10); 
    TV11 = (TextView) findViewById(R.id.textView11); 
    TV12 = (TextView) findViewById(R.id.textView12); 
    TV13 = (TextView) findViewById(R.id.textView13); 
    TV14 = (TextView) findViewById(R.id.textView14); 
    TV15 = (TextView) findViewById(R.id.textView15); 
    TV16 = (TextView) findViewById(R.id.textView16); 
    TV17 = (TextView) findViewById(R.id.textView17); 
    TV18 = (TextView) findViewById(R.id.textView18); 
    TV19 = (TextView) findViewById(R.id.textView19); 
    TV20 = (TextView) findViewById(R.id.textView20); 
    TV21 = (TextView) findViewById(R.id.textView21); 
    TV22 = (TextView) findViewById(R.id.textView22); 
    TV23 = (TextView) findViewById(R.id.textView23); 
    TV24 = (TextView) findViewById(R.id.textView24); 

    //TV1-8 how many wrong 
    //TV9-16 percentage 
    //TV17-24 letter grade 

    button = (Button) findViewById(R.id.button); 
    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      MaxInt = Integer.parseInt(ETmax.getText().toString()); 
      setContentView(R.layout.activity_main); 
      TV1.setText(MaxInt - 1); 
      TV2.setText(MaxInt - 2); 
      TV3.setText(MaxInt - 3); 
      TV4.setText(MaxInt - 4); 
      TV5.setText(MaxInt - 5); 
      TV6.setText(MaxInt - 6); 
      TV7.setText(MaxInt - 7); 
      TV8.setText(MaxInt - 8); 
      TV9.setText((int) Math.round((MaxInt - 1)/MaxInt) + "%"); 
      TV10.setText((int) Math.round((MaxInt - 2)/MaxInt) + "%"); 
      TV11.setText((int) Math.round((MaxInt - 3)/MaxInt) + "%"); 
      TV12.setText((int) Math.round((MaxInt - 4)/MaxInt) + "%"); 
      TV13.setText((int) Math.round((MaxInt - 5)/MaxInt) + "%"); 
      TV14.setText((int) Math.round((MaxInt - 6)/MaxInt) + "%"); 
      TV15.setText((int) Math.round((MaxInt - 7)/MaxInt) + "%"); 
      TV16.setText((int) Math.round((MaxInt - 8)/MaxInt) + "%"); 


     } 
    }); 

} 

}

回答

1

删除此行从公共无效的onClick

setContentView(R.layout.activity_main); 

,它看起来像它应该为你工作,如果你想要做的一切都更新文本视图中显示的文本。如果有任何预期的布局更改,您将不得不开始新的活动或片段。

0

你不能调用的setContentView按钮被点击时改变布局。如果您想更改显示内容,则需要手动设置视图可见/不可见,使用ViewFlipper,在您的活动容器中加载新的片段或加载新的活动。从您的代码看来,您可能想要使用main_activity布局启动一个新的Activity。

+0

我不知道那是什么,我期待的。有没有其他方法? @Francesc – LUKER

0

使用这个,如果你想开始一个新的活动

Intent intent=new Intent(MainActivity.this, the_name_of_class_to_go.class); 
startActivity(intent); 
相关问题