找到空指针异常

时间:2014-02-21 10:49:27

标签: android

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。如果在另一个活动中单击一个图像,则会显示单击的图像。请帮帮我。

3 个答案:

答案 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

之前没有调用setContentView()

答案 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

也许它可以帮到你