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);
}