嗨我正在使用显示在每个活动底部的几个图像按钮,我想知道是否有任何方法可以在各自的活动之外实例化对象Oncreate,因为它们在每个活动中执行相同的操作但我做的不想重复代码..
非常感谢任何建议。 马克
目前我在尝试调用build()函数时收到空指针异常。
这是我的菜单,其中标签将显示在底部
package koodoo.hcp.plus;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import koodoo.hcp.utilities.Tabbuilder;
public class Hcp_Menu extends Activity implements OnClickListener {
/**
* Stores all the buttons within the HCP_Menu Activity
*/
ImageButton groupBtn;
ImageButton readingBtn;
ImageButton activityBtn;
ImageButton calendarBtn;
ImageButton ongoingBtn;
Context context = this;
View view = new View(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hcp__menu);
Tabbuilder tb = new Tabbuilder();
tb.build(view, context);
/**
* Create Activity Buttons
*/
//group button
groupBtn = (ImageButton) findViewById(R.id.imageButtonGroup);
groupBtn.setOnClickListener(this);
//reading button
readingBtn = (ImageButton) findViewById(R.id.imageButtonReading);
readingBtn.setOnClickListener(this);
//activity button
activityBtn = (ImageButton) findViewById(R.id.imageButtonActivity);
activityBtn.setOnClickListener(this);
//calendar button
calendarBtn = (ImageButton) findViewById(R.id.imageButtonCalender);
calendarBtn.setOnClickListener(this);
//ongoing button
ongoingBtn = (ImageButton) findViewById(R.id.imageButtonOngoing);
ongoingBtn.setOnClickListener(this);
/**
* create Tab buttons
*//*
//dash tab
dashTab = (ImageButton) findViewById(R.id.dashButton);
dashTab.setOnClickListener(this);
//stats tab
statsTab = (ImageButton) findViewById(R.id.statsButton);
statsTab.setOnClickListener(this);
//invite tab
inviteTab = (ImageButton) findViewById(R.id.inviteButton);
inviteTab.setOnClickListener(this);
//settings tab
settingsTab = (ImageButton) findViewById(R.id.settingsButton);
settingsTab.setOnClickListener(this);
//log tab
logTab = (ImageButton) findViewById(R.id.logButton);
logTab.setOnClickListener(this);*/
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.hcp__menu, menu);
return true;
}
/**
* onclick function for each button on the Activity.
* @param v
*/
@Override
public void onClick(View v) {
Intent i;
switch (v.getId()){
case R.id.imageButtonGroup: i = new Intent(this, Group.class);
startActivity(i);
break;
case R.id.imageButtonActivity: i = new Intent(this, Activities.class);
startActivity(i);
break;
case R.id.imageButtonCalender: i = new Intent(this, Calender.class);
startActivity(i);
break;
case R.id.imageButtonOngoing: i = new Intent(this, Ongoing.class);
startActivity(i);
break;
case R.id.imageButtonReading: i = new Intent(this, Reading.class);
startActivity(i);
break;
default:
break;
}
}
}
Tabbuilder类
package koodoo.hcp.utilities;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View.OnClickListener;
import android.view.View;
import android.widget.ImageButton;
import koodoo.hcp.plus.Hcp_Menu;
import koodoo.hcp.plus.Invite;
import koodoo.hcp.plus.Log;
import koodoo.hcp.plus.R;
import koodoo.hcp.plus.Settings;
import koodoo.hcp.plus.Stats;
/**
* Created by Marc Davies on 18/09/2013.
*/
public class Tabbuilder {
ImageButton dashTab;
ImageButton statsTab;
ImageButton inviteTab;
ImageButton settingsTab;
ImageButton logTab;
public void build(View v, final Context context) {
/**
* create Tab buttons
*/
//dash tab
dashTab = (ImageButton) v.findViewById(R.id.dashButton);
dashTab.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(context, Hcp_Menu.class);
}
});
//stats tab
statsTab = (ImageButton) v.findViewById(R.id.statsButton);
statsTab.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(context, Stats.class);
}
});
//invite tab
inviteTab = (ImageButton) v.findViewById(R.id.inviteButton);
inviteTab.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(context, Invite.class);
}
});
//settings tab
settingsTab = (ImageButton) v.findViewById(R.id.settingsButton);
settingsTab.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(context, Settings.class);
}
});
//log tab
logTab = (ImageButton) v.findViewById(R.id.logButton);
logTab.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(context, Log.class);
}
});
}
}
答案 0 :(得分:2)
创建基本活动并在其中实例化按钮。现在让其他活动继承基本活动。不要忘记正确处理布局
答案 1 :(得分:2)
您可以使用静态启动方法创建一个utils类。
这些将采用上下文和图像并返回imageButton。您仍然会在相应活动的onCreate中调用它,但您不必再复制粘贴代码。