0

I have three Activities and two AsynTasks.

  1. Activity 1 - Goes to AsyncTask1
  2. Activity 2 - Goes to AsyncTask2
  3. Activity 3 - No AsyncTask required

I have a user selection wizard that lasts for a number of times defined dynamically. I am successfully able to keep track of that count. Below the flow.

For the sake of explanation, let's keep the count as 3.

Activity 1 - Activity loads for the first time with Count set to 1. The user makes a selection and clicks on a button. This takes the user to Activity 2.

The way I am going to Activity 2 is as below.

I load AsyncTask that is pulling the data from db. In the external AsyncTask file, I load the next activity on onPostExecute.

Activity 2 - User makes a selection and clicks on a button. The button checks if the Count is 1, it sets it to 2. Since the count is still less than 3, it goes back to Activity 1 using the AsyncTask.

The process continues and once the Count reaches 3, I want to go to Activity 3. Now the Activity 3 needs to be loaded without any AsyncTask. How to do that? Below is the code.

ImageButton imgButton = (ImageButton) findViewById(R.id.imageButton);
imgButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //code that I process and define value_i_am_taking_forward_successfully. It is working fine.

            proceedNext();
        }
});

private void proceedNext() {
    if (my_counter > max_count) {
        //This is not working. I want to start Activity3 here.
        Intent intent = new Intent(ctx, Activity3.class);

        ctx.startActivity(intent);
    }
    else {
        //This works fine. 
        String requested_method = value_i_am_taking_forward_successfully;

        LoadAsyncTask2 loadAsyncTask2 = new LoadAsyncTask2(this);
        loadAsyncTask2.execute(requested_method);
    }
}

Below is the code in AsyncTask that loads the Activity. result is coming through doInBackground

@Override
protected void onPostExecute(String result) {
    Intent intent = new Intent(ctx, Activity1.class);

    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    intent.putExtra("value_to_take_forward",result);

    ctx.startActivity(intent);
}

Can anyone help please?

Below is the error I am getting:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference

3 Answers3

2

Use your current ClassName instead of ctx

Source : android.content.Context.getPackageName()' on a null object reference

Community
  • 1
  • 1
John Joe
  • 12,412
  • 16
  • 70
  • 135
1

If this code runs in the UI thread and in from the Activity itself, then you should use this,

Intent intent = new Intent(this, Activity3.class);
startActivity(intent);

You should be using the Activity context to load the next Activity.

John Joe
  • 12,412
  • 16
  • 70
  • 135
Aritra Roy
  • 15,355
  • 10
  • 73
  • 107
0

What is value of max_count?

Try my_counter >= max_count.

babedev
  • 135
  • 3
  • 5