-2

I have my code defined the way below. There are two crucial activities. Activity (1) shows some images in a ViewFlipper. It uses methods to load desired image directly. The onOptionsItemSelected() method fetches data from a menu defined within linked XML layout R.layout.browse. The other method, displaySelectedFlag(), gets a tag parameter passed from a different activity, let's call it activity (2).

Activity (1):

public class BrowserActivity extends AppCompatActivity implements SimpleGestureListener, View.OnClickListener {

public ViewFlipper vFlipper;

(...)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

setContentView(R.layout.browse);
vFlipper = (ViewFlipper) findViewById(R.id.viewFlipperBrowser);

(...)

}  // onCreate() ends here

// this method below works fine:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    vFlipper.setDisplayedChild(item.getOrder());
    return true;
}

// and this one doesn't:

public void displaySelectedFlag(int orderTag) {
    vFlipper.setDisplayedChild(orderTag); // crashes here
}
}

Activity (2):

public class ListActivity extends Activity implements View.OnClickListener {

private BrowserActivity browserActivity = new BrowserActivity();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list);

    ImageButton imageA = (ImageButton) findViewById(R.id.img_a);
    imageA.setOnClickListener(this);

    ImageButton imageB = (ImageButton) findViewById(R.id.img_b);
    imageB.setOnClickListener(this);
}

public void displayImageInfo(View view) {
    String tagValue = (String) view.getTag();
    int tagId = Integer.parseInt(tagValue);
    Intent intent = new Intent(this, BrowserActivity.class);
    startActivity(intent);
    browserActivity.displaySelectedImage(imageId);
}

@Override
public void onClick(View view) {
    displayImageInfo(view);
}
}

As I checked, the method onClick() called in activity (2) fetches an ID of an ImageButton and passes it to activity (1). Unfortunately, I get a NullPointerException when calling the ViewFlipper (the line is marked in the code above, activity (1)).

Any idea why it happens?

AbreQueVoy
  • 1,748
  • 8
  • 31
  • 52
  • 1
    Check this answer it explains why you shouldn't create objects of Activities. http://stackoverflow.com/questions/14956018/can-i-create-the-object-of-a-activity-in-other-class – Apoorv Jun 29 '16 at 23:14

1 Answers1

1

You cannot reference one Activity from another activity. You must let the Android OS create the Activity object via the call to "startActivity". Allocating a local variable as an instance of an Activity doesn't actually mean anything (like your instantiation of the BrowserActivity). Apoorv's comment links to a decent article on the subject.

If you want to pass data from one Activity to another, you need to pass extras within the Intent's bundle. This post goes into detail: https://stackoverflow.com/a/819427/504252

Community
  • 1
  • 1
FishStix
  • 4,994
  • 9
  • 38
  • 53