0

I'm building an app with a screen (activity) which shows an animation, and a second screen which should show options for the animation.

I created my own custom View class (a class which extends View) and it's in that class where I put the onDraw method and the animation. It works great, and I can drag my finger across it to alter the animation. I have methods to change the color and speed of the animation and I can access them within the class, but I don't know how to access them from the rest of the app.

So, in my FractalView class (which extends View) I have a changeColor method. I want to access that method from the settings activity screen. But when I press the button for a color change, which calls the changeColor method, my app does a force close.

I access the View like this:

 FractalView fractalView = (FractalView) findViewById(R.id.fractalViewCanvas);
 fractalView.changeColor('G');

It gives me no errors, but when I select the button, it all crashes.

The FractalDisplay activity screen loads the FractalView View just like it would load a button or TextView. So there's no XML file for the FractalView class.

Here's the method in the FractalView class:

public class FractalView extends View{

//variables... multiple constructors...

public FractalView(Context context, AttributeSet attrs) {
    super(context, attrs);
    thisContext = context;
    initializeVariables();

 //tonnes of methods... here's the one I want to access:

public void changeColor(char newColor){

    switch(newColor) {
       case 'G':
            paint.setColor(Color.GREEN);
            break;
        default:
            paint.setColor(Color.BLACK);
    }

Of course, I can't actually access any of the methods in this class. I tried several.

Here's the code from the XML file, DisplayFractal, which successfully loads the FractalView onto its screen:

<com.pattmayne.fractalapp.FractalView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:id="@+id/fractalViewCanvas" />

Honestly, I don't know what other information to provide. It's not a huge app and I'm sure there's some way to do this... seems like a simple thing. An object has clearly been created from the class, since the animation is showing on the screen. I just need to get a reference to that object, and access its methods, from wherever I want!

I'll provide more info if you need it.

EDIT : Thanks for responding, guys.

Okay, here is the crash information:

10-05 22:14:35.650  14597-14597/com.pattmayne.fractalapp W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41458450)
10-05 22:14:35.650  14597-14597/com.pattmayne.fractalapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
    at com.pattmayne.fractalapp.ChooseColorActivity$1.onItemClick(ChooseColorActivity.java:43)
    at android.widget.AdapterView.performItemClick(AdapterView.java:298)
   ...
    tem.NativeStart.main(Native Method)

So I guess it's a null pointer exception, like I'm referencing a class but no object. How can I get the object that's running?

EDIT #2 :

There is no "onClick." I do it like this in a .java file: (the method call is close to the bottom):

public class ChooseColorActivity extends ActionBarActivity {

    FractalView fractalView;

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

    String [] colors = {"Green", "Blue", "Red", "Black"};

    ListAdapter theAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, colors);

    ListView theListView = (ListView) findViewById(R.id.colorListView);

    theListView.setAdapter(theAdapter);
    theListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
            String colorPicked = "You selected " + String.valueOf(adapterView.getItemAtPosition(position));
            Toast.makeText(ChooseColorActivity.this, colorPicked, Toast.LENGTH_SHORT).show();
        fractalView = (FractalView) findViewById(R.id.fractalViewCanvas);
             fractalView.changeColor('G');
            loadFractalView();
        }
    });

}

EDIT #3 :

Oh, and here's the loadFractalView() method (which calls the activity which contains the FractalView view:

public void loadFractalView() {
    Intent getNameScreenIntent = new Intent(this, DisplayFractal.class);
    final int result = 1;
    startActivityForResult(getNameScreenIntent, result);
}
Matt Payne
  • 198
  • 4
  • 15
  • You need to provide more details about the error that is occuring. i.e is it a null reference exception – Abbath Oct 06 '14 at 02:07
  • possible duplicate of [Unfortunately MyApp has stopped. How can I solve this?](http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – Code-Apprentice Oct 06 '14 at 02:10
  • See the above link for instructions about how to view the error which causes your program to crash. – Code-Apprentice Oct 06 '14 at 02:10
  • It isn't the same problem, though there are similarities. I need to find out how to access that method, which isn't covered in the other thread. – Matt Payne Oct 06 '14 at 02:19
  • Show your `onItemClick` code. – takendarkk Oct 06 '14 at 02:26
  • Are you missing code or did you not actually set the fractalView to anything and then try to access it? – Mike Oct 06 '14 at 03:29
  • I did a: fractalView = (FractalView) findViewById(R.id.fractalViewCanvas); I just deleted it and forgot to put it back in. I moved it around to different places to see if scope was the problem :p – Matt Payne Oct 06 '14 at 03:37
  • aw man I really need this answered LoL. – Matt Payne Oct 06 '14 at 04:13

0 Answers0