Question

I'm new to all this, and as you can probably tell by my question, I'm getting very spun in circles after reading all I could find on this. Please help.

I have a ClassA that contains an inner ViewClass that extends view. From ClassA, I call DialogCreator Class that extends ClassA and displays a dialog. From the dialog listview, you can select an item that needs the ViewClass to redraw. No matter how I send context, I get a null pointer context error.

ClassA constructor:

public class ClassA extends Basclass{ // Baseclass extends Activity
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      context = this;
   }

ClassA relevant code:

// Coming in from Menu for deleting or changing database tables
DialogCreator dc = new DialogCreator(context, id);
dc.onCreateDialog(title, list, mode);

    protected class ViewClass extends View{
         // Constructor
        public ViewClass(Context context){
             super(context); // Mainly crashes HERE
        }
           // Initializes rects, then draws via an OnDraw method
   }

DialogCreator constructor:

public class DialogCreator extends ClassA {
    DialogCreator(Context context, int dialogID){
     this.context = context;
     this.dialogID = dialogID;
}
}

Inside DialogCreator:

protected Dialog onCreateDialog(String title, final ArrayList list, final String mode) {
   // build alertdialog listView
   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
       // User choices, etc... 
       ViewClass v = new ViewClass(context);
       setContentView(v); 
   }
}

Alternate OnCreateDialog would not try to setContentView... it calls a method in ClassA that tries to set it. Both fail.

I've tried passing the activity into DialogCreator, I've tried using the getXcontext() from both classes. I've even tried to stick a method in the ClassA that is called from DialogCreator class that uses the ClassA context. Clearly I am missing a very important understanding of context somewhere. Every way I try to define context, I crash with similar error as per LogCat below:

FATAL EXCEPTION: main
java.lang.NullPointerException
at android.app.Activity.startActivityForResult(Activity.java:3351)
at android.app.Activity.startActivityForResult(Activity.java:3312)
at android.app.Activity.startActivity(Activity.java:3522)
at android.app.Activity.startActivity(Activity.java:3490)

I'd be grateful for an unspin in the right direction.

Everything works perfectly if I keep all the code in the ClassA. Once I move the call to redraw into the DialogCreator, I get the context errors.

Was it helpful?

Solution

Took me all day, but found the answer thanks to Correct context to use within callbacks. @Vikram had it right in a somewhat related question about context in callbacks. The answer to my question was to use the original Activity's context by casting it.

((ClassA)context).methodToCall();

where methodToCall comes from the onItemClick() of DialogCreator, instead of calling the redraw directly.

I would never have figured out that casting.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top