HomeActivity.java
@Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.home);
initialize();
super.onCreate(savedInstanceState);
buildLocationService();
getLocationDetails(fioLocation);
icHair = (ImageButton) findViewById(R.id.ic_hair);
icNail = (ImageButton) findViewById(R.id.ic_nail);
icSpa = (ImageButton) findViewById(R.id.ic_spa);
icTanning = (ImageButton) findViewById(R.id.ic_tanning);
setOnClickListenersToIcons();
}
private void setOnClickListenersToIcons() {
icHair.setOnClickListener(new IconClickListener(this, zipCode, locality));
icNail.setOnClickListener(new IconClickListener(this, zipCode, locality));
icSpa.setOnClickListener(new IconClickListener(this, zipCode, locality));
icTanning.setOnClickListener(new IconClickListener(this, zipCode,locality));
}
private void getLocationDetails(FioLocation fioLocation) {
zipCode = fioLocation.getZipcode();
locality = fioLocation.getArea();
}
public static class IconClickListener implements OnClickListener {
private Context context;
private String zipCode;
private String locality;
public IconClickListener(Context homeActivity, String zip, String loc) {
context = homeActivity;
this.zipCode = zip;
this.locality = loc;
}
@Override
public void onClick(View v) {
Intent intent = new Intent(context, MainActivity.class);
intent.putExtra("businessType", v.getContentDescription());
if (this.zipCode != null) {
intent.putExtra("zipCode", this.zipCode);
}
if (this.locality != null) {
intent.putExtra("locality", this.locality);
}
context.startActivity(intent);
}
}
MainActivity.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ic_Hair = (ImageView) findViewById(R.id.ic_hair);
ic_Nail = (ImageView) findViewById(R.id.ic_nail);
ic_Spa = (ImageView) findViewById(R.id.ic_spa);
ic_Tanning = (ImageView) findViewById(R.id.ic_tanning);
Bundle intentBundle = getIntent().getExtras();
String businessType = intentBundle.getString(BUSINESS_TYPE);
if (businessType != null && businessType.equals(getString(R.string.hair))) {
ic_Hair.setVisibility(View.VISIBLE);
ic_Nail.setVisibility(View.GONE);
ic_Spa.setVisibility(View.GONE);
ic_Tanning.setVisibility(View.GONE);
}
if (businessType != null && businessType.equals(getString(R.string.nail))) {
ic_Hair.setVisibility(View.GONE);
ic_Nail.setVisibility(View.VISIBLE);
ic_Spa.setVisibility(View.GONE);
ic_Tanning.setVisibility(View.GONE);
}
if (businessType != null && businessType.equals(getString(R.string.spa))) {
ic_Hair.setVisibility(View.GONE);
ic_Nail.setVisibility(View.GONE);
ic_Spa.setVisibility(View.VISIBLE);
ic_Tanning.setVisibility(View.GONE);
}
if (businessType != null && businessType.equals(getString(R.string.tanning))) {
ic_Hair.setVisibility(View.GONE);
ic_Nail.setVisibility(View.GONE);
ic_Spa.setVisibility(View.GONE);
ic_Tanning.setVisibility(View.VISIBLE);
}
zipCode = intentBundle.getString("zipCode");
locality = intentBundle.getString(LOCALITY);
setContentView(R.layout.activity_main);
addressProgress = (ProgressBar) findViewById(R.id.addressProgress);
locationLable = (TextView) findViewById(R.id.locationLable);
}
在这种情况下,我想避免NullPointerException,并且我不确切知道哪个对象为null。如果在另一个活动中单击一个图像,则会显示单击的图像。请帮帮我。
答案 0 :(得分:8)
在MainActivity.java中,
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ic_Hair = (ImageView) findViewById(R.id.ic_hair);
ic_Nail = (ImageView) findViewById(R.id.ic_nail);
在findViewById
答案 1 :(得分:0)
您忘了在setContentView()
内MainActivity.java
。例如:setContentView(R.layout.main);
例如:
它应该在onCreate()
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
在HomeActivity.java
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
答案 2 :(得分:0)
尝试setContentView(R.layout.you_xml)
MainActivity.Java
也许它可以帮到你