Reputation: 93
here the next button is a method called upon clicking a button.When i click on the button it gives me an illegal state exception.
code:
public class Shapes extends Activity {
int i=3;
ShapeView shape;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shapes);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.shapes, menu);
return true;
}
public void next(View view)
{
i++;
shape=(ShapeView)findViewById(R.id.shape_view);
shape.nextshape(i);
}
}
here nextshape(i) is a method defined in a custom view class.And the following is the message in logcat
Logcat :
08-19 12:09:15.348: E/AndroidRuntime(812): FATAL EXCEPTION: main
08-19 12:09:15.348: E/AndroidRuntime(812): java.lang.IllegalStateException: Could not execute method of the activity
08-19 12:09:15.348: E/AndroidRuntime(812): at android.view.View$1.onClick(View.java:2072)
08-19 12:09:15.348: E/AndroidRuntime(812): at android.view.View.performClick(View.java:2408)
08-19 12:09:15.348: E/AndroidRuntime(812): at android.view.View$PerformClick.run(View.java:8816)
08-19 12:09:15.348: E/AndroidRuntime(812): at android.os.Handler.handleCallback(Handler.java:587)
08-19 12:09:15.348: E/AndroidRuntime(812): at android.os.Handler.dispatchMessage(Handler.java:92)
08-19 12:09:15.348: E/AndroidRuntime(812): at android.os.Looper.loop(Looper.java:123)
08-19 12:09:15.348: E/AndroidRuntime(812): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-19 12:09:15.348: E/AndroidRuntime(812): at java.lang.reflect.Method.invokeNative(Native Method)
08-19 12:09:15.348: E/AndroidRuntime(812): at java.lang.reflect.Method.invoke(Method.java:521)
08-19 12:09:15.348: E/AndroidRuntime(812): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-19 12:09:15.348: E/AndroidRuntime(812): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-19 12:09:15.348: E/AndroidRuntime(812): at dalvik.system.NativeStart.main(Native Method)
08-19 12:09:15.348: E/AndroidRuntime(812): Caused by: java.lang.reflect.InvocationTargetException
08-19 12:09:15.348: E/AndroidRuntime(812): at org.example.m_pustika.Shapes.next(Shapes.java:29)
08-19 12:09:15.348: E/AndroidRuntime(812): at java.lang.reflect.Method.invokeNative(Native Method)
08-19 12:09:15.348: E/AndroidRuntime(812): at java.lang.reflect.Method.invoke(Method.java:521)
08-19 12:09:15.348: E/AndroidRuntime(812): at android.view.View$1.onClick(View.java:2067)
08-19 12:09:15.348: E/AndroidRuntime(812): ... 11 more
08-19 12:09:15.348: E/AndroidRuntime(812): Caused by: java.lang.NullPointerException
xmlfile:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Shapes" >
<org.example.m_pustika.ShapeView
android:id="@+id/shape_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/next_button" />
<Button
android:id="@+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="@string/next_button"
android:onClick="next" />
</RelativeLayout>
CustomView:
public class ShapeView extends View {
Paint cPaint;
Random r=new Random();
int n=3;
public ShapeView(Context context,AttributeSet attrs) {
super(context);
// TODO Auto-generated constructor stub
cPaint = new Paint();
cPaint.setColor(Color.BLACK);
}
public void nextshape(int n)
{
this.n=n;
}
public void onDraw(Canvas canvas)
{
Log.d("n","n= " +n);
for(int i=1;i<=n;i++)
{
int x=r.nextInt(canvas.getWidth()-50);
int y=r.nextInt(canvas.getHeight()-200);
canvas.drawCircle(x, y, 10, cPaint);
}
}
}
Upvotes: 0
Views: 468
Reputation: 133560
I guess you are shape object is null
You probably want to do
ShapeView shape = new ShapeView(Shapes.this);
to initalize and then call shapenext
Add appropriate constructors also
Edit:
As a side note to refresh or update view call invalidate()
on your view or call postInvalidate()
from a thread (non ui thread).
Upvotes: 1
Reputation: 3426
Try this once. remove shape=(ShapeView)findViewById(R.id.shape_view); from next()
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shapes);
shape=(ShapeView)findViewById(R.id.shape_view);
}
or please make a call of next() in onCreate()
Upvotes: 0
Reputation: 851
I think you called the method next() before running the onCreate() method.
Try this instead of you current next() method:
public void next(View view)
{
setContentView(R.layout.activity_shapes);
shape=(ShapeView)findViewById(R.id.shape_view);
shape.nextshape(i);
}
Upvotes: 0