0

这里的下一个按钮是单击按钮时调用的方法。当我单击按钮时,它会给我一个非法状态异常。

代码:

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

     } 

这里的nextshape(i)是自定义视图类中定义的方法。以下是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

xml文件:

<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>

自定义视图:

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

}

4

3 回答 3

1

我猜你的形状对象是空的

你可能想做

          ShapeView shape = new ShapeView(Shapes.this);

初始化然后调用shapenext

还添加适当的构造函数

编辑:

作为在视图上刷新或更新视图调用或从线程(非 ui 线程)调用的附带invalidate()说明postInvalidate()

于 2013-08-19T08:05:01.940 回答
0

我认为您在运行 onCreate() 方法之前调用了 next() 方法。

试试这个,而不是你当前的 next() 方法:

public void next(View view)
{
    setContentView(R.layout.activity_shapes);
    shape=(ShapeView)findViewById(R.id.shape_view);
    shape.nextshape(i);
}
于 2013-08-19T06:58:18.503 回答
0

试试这个。消除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);


        }

或者请在 onCreate() 中调用 next()

于 2013-08-19T07:13:15.467 回答