在创建新的Android类的实例 [英] Create instance of new class in Android
本文介绍了在创建新的Android类的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是新来的Android程序,但有与Java一点点经验。不过,我创建一个Android应用程序,当用户点击一个按钮,我想不同的类实例化...
这是我的 MainActivity.java
I am new to Android programming but have a little bit of experience with Java. However, I am creating an Android application and when a user clicks a button I want a different class to instantiated...
This is my MainActivity.java
private void setButtonClickListener() {
Button budgetPeriodButton = (Button)findViewById(R.id.budgetPeriodButton);
Button incomingsButton = (Button)findViewById(R.id.incomingsButton);
Button outgoingsButton = (Button)findViewById(R.id.outgoingsButton);
Button resultsButton = (Button)findViewById(R.id.resultsButton);
budgetPeriodButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
BudgetPeriod bp = new BudgetPeriod();
bp.changeUI();
}
这是BudgetPeriod类
And this is the BudgetPeriod class
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;
public class BudgetPeriod extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_budget);
super.onCreate(savedInstanceState);
changeUI();
}
public void changeUI() {
ImageView imageView = (ImageView) findViewById(R.id.budget_icon);
Drawable newBudgetImage;
newBudgetImage = getResources().getDrawable(R.drawable.budget_period);
imageView.setImageDrawable(newBudgetImage);
}
}
如果用户点击该按钮,然后在模拟器上的错误信息说:抱歉,系统这个程序已关闭
If the user clicks on the button, then an error message on the emulator says "Unfortunatley, this app has had to close"
这是什么,我做错了任何想法?
谢谢
Any ideas on what I am doing wrong? Thanks
推荐答案
开始这样的活动。
budgetPeriodButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), BudgetPeriod.class);
startActivity(intent);
}
});
,并确保你宣布在AndroidManifest.xml活动
and make sure you declared the activity in AndroidManifest.xml
<activity name=".BudgetPeriod" android:name="Budget" />
这篇关于在创建新的Android类的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文