背景 - 我今天早上才开始使用android studio,所以我像草一样绿。
我正在尝试制作一个简单的 Counter 应用程序,在其中从总数中添加/减去一个,然后显示它。但由于某种原因,该应用程序只是在启动时崩溃。
这是我的代码 - 还有几个 OnClickListener 方法我没有包括在内,但它们都是相同的代码。
提前致谢。
public class MainActivity extends AppCompatActivity {
Button counter1Up, counter1Down, counter2Down, counter2Up;
TextView counter1, counter2;
double lifeTotal1 = 20, lifeTotal2 = 20;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUI();
counter1Up.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
lifeTotal1 = Double.parseDouble(counter1.getText().toString());
counter1.setText(String.valueOf(lifeTotal1 + 1));
}
});
counter2Up.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
lifeTotal2 = Double.parseDouble(counter2.getText().toString());
counter2.setText(String.valueOf(lifeTotal2 + 1));
}
});
counter1Down.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
lifeTotal1 = Double.parseDouble(counter1.getText().toString());
counter1.setText(String.valueOf(lifeTotal1 - 1));
}
});
public void setUI(){
counter1Up = findViewById(R.id.counter2UpButton);
counter1Down = findViewById(R.id.Counter1DownButton);
counter2Up = findViewById(R.id.counter2UpButton);
counter2Down = findViewById(R.id.counter2DownButton);
counter1.setText("20");
counter2.setText("20");
}
}
这就是事件日志显示的内容:
1/19/2019
3:29 AM Gradle sync started
3:29 AM Project setup started
3:29 AM Gradle sync finished in 2 s 979 ms
3:29 AM Syncing only active variant
You can disable this experimental feature from
File → Settings → Experimental → Gradle → Only sync the active variant
函数式编程
慕后森
相关分类