开发者

Inflating custom view from XML creates Illegalstateexception(Android, Java)

I've stuck for about 12 hours now on some irritating runtime exception that keeps happening when I try to inflate a custom view that I have created. This is my first Android (and Java) program so hopefully I am not doing something insanely stupid.

XML:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:padding="10px" >

<com.studentfees.GraphView/>

</RelativeLayout>

Class:

public class GraphView extends View {

    Paint paint             = new Paint();
    Boolean IsTouched       = false;
    float TouchCoords[]     = new float[2];
    public enum GraphType {Earnings, Payments, TotalPaid, NoneSet};
    GraphType graphtype = GraphType.NoneSet;
    Graph graph = new Graph();
    Button ButtonLeft;
    Button ButtonCentre;
    Button ButtonRight;

    public GraphView(Context context,  AttributeSet attrs) 
    {
            super(context, attrs);

        graphtype = GraphType.Earnings;  
        paint.setColor(Color.BLACK);
        paint.setAntiAlias(true);
        paint.setStrokeWidth(5);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) 
    {   
        IsTouched = true;
        TouchCoords[0] = event.getX();
        TouchCoords[1] = event.getY();
        this.invalidate();
        return super.onTouchEvent(event);

    }

    @Override
    public void onDraw(Canvas canvas) {

        GraphSetUp(canvas);

        TouchHandler(canvas);

        ButtonSetUp();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    private void ButtonSetUp() 
    {

    }

    private void TouchHandler(Canvas canvas) 
    {
       if(IsTouched)
       {
           IsTouched = false;
            canvas.drawCircle(TouchCoords[0], TouchCoords[1],10, paint);
       }
    }

    private void GraphSetUp(Canvas canvas)
    {   
        graph.DrawAxis(canvas, this);

        switch(graphtype)
        {
        case Earnings:  graph.LineCoords =     Coordinate.ToCoordArray(studentfeesproject.student.ListofEarnings); break;
        case Payments:  graph.LineCoords = Coordinate.ToCoordArray(studentfeesproject.student.ListofPayments); break;
        case TotalPaid: graph.LineCoords = graph.ToHistogram(Coordinate.ToCoordArray(studentfeesproject.student.ListofPayments)); break;
        default: break;
        }

        graph.LineCoords = graph.CoordsToGraph(graph.LineCoords);
        graph.DrawLineOnGraph(canvas);

    }    


}

Log:

05-22 13:57:04.028: ERROR/AndroidRuntime(10594): FATAL EXCEPTION: main
05-22 13:57:04.028: ERROR/AndroidRuntime(10594): java.lang.IllegalStateException: Could not execute method of the activity
05-22 13:57:04.028: ERROR/AndroidRuntime(10594):     at android.view.View$1.onClick(View.java:2082)
05-22 13:57:04.028: ERROR/AndroidRuntime(10594):     at android.view.View.performClick(View.java:2461)
05-22 13:57:04.028: ERROR/AndroidRuntime(10594):     at android.view.View$PerformClick.run(View.java:8888)
05-22 13:57:04.028: ERROR/AndroidRuntime(10594):     at android.os.Handler.handleCallback(Handler.java:587)
05-22 13:57:04.028: ERROR/AndroidRuntime(10594):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-22 13:57:04.028: ERROR/AndroidRuntime(10594):     at android.os.Looper.loop(Looper.java:123)
05-22 13:57:04.028: ERROR/AndroidRuntime(10594):     at android.app.ActivityThread.main(ActivityThread.java:4627)
05-22 13:57:04.028: ERROR/AndroidRuntime(10594):     at java.lang.reflect.Method.invokeNative(Native Method)
05-22 13:57:04.028: ERROR/AndroidRuntime(10594):     at java.lang.reflect.Method.invoke(Method.java:521)
05-22 13:57:04.028: ERROR/AndroidRuntime(10594):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
05-22 13:57:04.028: ERROR/AndroidRuntime(10594):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
05-22 13:57:04.028:开发者_运维问答 ERROR/AndroidRuntime(10594):     at dalvik.system.NativeStart.main(Native Method)
05-22 13:57:04.028: ERROR/AndroidRuntime(10594): Caused by: java.lang.reflect.InvocationTargetException
05-22 13:57:04.028: ERROR/AndroidRuntime(10594):     at com.studentfees.studentfeesproject.GetResults(studentfeesproject.java:51)
05-22 13:57:04.028: ERROR/AndroidRuntime(10594):     at java.lang.reflect.Method.invokeNative(Native Method)
05-22 13:57:04.028: ERROR/AndroidRuntime(10594):     at java.lang.reflect.Method.invoke(Method.java:521)
05-22 13:57:04.028: ERROR/AndroidRuntime(10594):     at android.view.View$1.onClick(View.java:2077)
05-22 13:57:04.028: ERROR/AndroidRuntime(10594):     ... 11 more
05-22 13:57:04.028: ERROR/AndroidRuntime(10594): Caused by: java.lang.RuntimeException: Binary XML file line #10: You must supply a layout_width attribute.
05-22 13:57:04.028: ERROR/AndroidRuntime(10594):     at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:491)
05-22 13:57:04.028: ERROR/AndroidRuntime(10594):     at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:3592)
05-22 13:57:04.028: ERROR/AndroidRuntime(10594):     at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:3672)
05-22 13:57:04.028: ERROR/AndroidRuntime(10594):     at android.widget.RelativeLayout$LayoutParams.<init>(RelativeLayout.java:1047)
05-22 13:57:04.028: ERROR/AndroidRuntime(10594):     at android.widget.RelativeLayout.generateLayoutParams(RelativeLayout.java:917)
05-22 13:57:04.028: ERROR/AndroidRuntime(10594):     at android.widget.RelativeLayout.generateLayoutParams(RelativeLayout.java:66)
05-22 13:57:04.028: ERROR/AndroidRuntime(10594):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:620)
05-22 13:57:04.028: ERROR/AndroidRuntime(10594):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
05-22 13:57:04.028: ERROR/AndroidRuntime(10594):     at android.view.LayoutInflater.inflate(LayoutInflater.java:382)
05-22 13:57:04.028: ERROR/AndroidRuntime(10594):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
05-22 13:57:04.028: ERROR/AndroidRuntime(10594):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
05-22 13:57:04.028: ERROR/AndroidRuntime(10594):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:200)
05-22 13:57:04.028: ERROR/AndroidRuntime(10594):     at android.app.Activity.setContentView(Activity.java:1647)
05-22 13:57:04.028: ERROR/AndroidRuntime(10594):     ... 15 more

+++++++++++ +++++++++++ +++++++++++

Edit:

New log as a result of first answer - thanks for your help inazaruk:

05-22 16:02:23.055: WARN/dalvikvm(13186): threadid=1: thread exiting with uncaught exception (group=0x4001d7d0)
05-22 16:02:23.071: ERROR/AndroidRuntime(13186): FATAL EXCEPTION: main
05-22 16:02:23.071: ERROR/AndroidRuntime(13186): java.lang.NullPointerException
05-22 16:02:23.071: ERROR/AndroidRuntime(13186):     at com.studentfees.Graph.CoordsToGraph(Graph.java:81)
05-22 16:02:23.071: ERROR/AndroidRuntime(13186):     at com.studentfees.GraphView.GraphSetUp(GraphView.java:93)
05-22 16:02:23.071: ERROR/AndroidRuntime(13186):     at com.studentfees.GraphView.onDraw(GraphView.java:55)
05-22 16:02:23.071: ERROR/AndroidRuntime(13186):     at android.view.View.draw(View.java:6793)
05-22 16:02:23.071: ERROR/AndroidRuntime(13186):     at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
05-22 16:02:23.071: ERROR/AndroidRuntime(13186):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
05-22 16:02:23.071: ERROR/AndroidRuntime(13186):     at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
05-22 16:02:23.071: ERROR/AndroidRuntime(13186):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
05-22 16:02:23.071: ERROR/AndroidRuntime(13186):     at android.view.View.draw(View.java:6796)
05-22 16:02:23.071: ERROR/AndroidRuntime(13186):     at android.widget.FrameLayout.draw(FrameLayout.java:354)
05-22 16:02:23.071: ERROR/AndroidRuntime(13186):     at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
05-22 16:02:23.071: ERROR/AndroidRuntime(13186):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
05-22 16:02:23.071: ERROR/AndroidRuntime(13186):     at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
05-22 16:02:23.071: ERROR/AndroidRuntime(13186):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
05-22 16:02:23.071: ERROR/AndroidRuntime(13186):     at android.view.View.draw(View.java:6796)
05-22 16:02:23.071: ERROR/AndroidRuntime(13186):     at android.widget.FrameLayout.draw(FrameLayout.java:354)
05-22 16:02:23.071: ERROR/AndroidRuntime(13186):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1894)
05-22 16:02:23.071: ERROR/AndroidRuntime(13186):     at android.view.ViewRoot.draw(ViewRoot.java:1432)
05-22 16:02:23.071: ERROR/AndroidRuntime(13186):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1174)
05-22 16:02:23.071: ERROR/AndroidRuntime(13186):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1752)
05-22 16:02:23.071: ERROR/AndroidRuntime(13186):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-22 16:02:23.071: ERROR/AndroidRuntime(13186):     at android.os.Looper.loop(Looper.java:123)
05-22 16:02:23.071: ERROR/AndroidRuntime(13186):     at android.app.ActivityThread.main(ActivityThread.java:4627)
05-22 16:02:23.071: ERROR/AndroidRuntime(13186):     at java.lang.reflect.Method.invokeNative(Native Method)
05-22 16:02:23.071: ERROR/AndroidRuntime(13186):     at java.lang.reflect.Method.invoke(Method.java:521)
05-22 16:02:23.071: ERROR/AndroidRuntime(13186):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
05-22 16:02:23.071: ERROR/AndroidRuntime(13186):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
05-22 16:02:23.071: ERROR/AndroidRuntime(13186):     at dalvik.system.NativeStart.main(Native Method)


From your logs:

05-22 13:57:04.028: ERROR/AndroidRuntime(10594): Caused by: java.lang.RuntimeException: Binary XML file line #10: You must supply a layout_width attribute.
05-22 13:57:04.028: ERROR/AndroidRuntime(10594):     at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:491)

You must specify layout_width and layout_height:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:padding="10px" >

   <com.studentfees.GraphView
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content" />

</RelativeLayout>

Normally you must specify layout_width and layout_height for all items inside layout. There are some cases where this is optional, but they are well documented.


Now your application fails for another reason: NullPointerException. It happens in your GraphSetUp function here:

switch(graphtype)
{
    case Earnings:  graph.LineCoords =     Coordinate.ToCoordArray(studentfeesproject.student.ListofEarnings); break;
    case Payments:  graph.LineCoords = Coordinate.ToCoordArray(studentfeesproject.student.ListofPayments); break;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜