应用程序不断在一项新活动中崩溃。主要活动启动后,我按任意按钮,应用程序停止或崩溃。
我不知道我可能做得不对,但这是我的mainActivity
代码
我正在使用android studio
public class Calculator extends ActionBarActivity{
public Button loan;
public Button exit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calc);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
loan = (Button) findViewById(R.id.loans);
loan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, loancalculator.class);
startActivity(intent);
finish();
}
});
exit = (Button) findViewById(R.id.exits);
exit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, presentvaluecalculator.class);
startActivity(intent);
finish();
}
});
}
}
答案 0 :(得分:0)
public class Calculator extends ActionBarActivity{
public Button loan,exit;
Calculator context;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calc);
context = this;
addListenerOnButton();
}
public void addListenerOnButton() {
loan = (Button) findViewById(R.id.loans);
loan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, loancalculator.class);
startActivity(intent);
finish();
}
});
exit = (Button) findViewById(R.id.exits);
exit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, presentvaluecalculator.class);
startActivity(intent);
finish();
}
});
}
}
// And make sure you have declared all the activities in manifest
<activity android:name="com.yourpackage.loancalculator"/>
<activity android:name="com.yourpackage.presentvaluecalculator"/>