从 XML 膨胀自定义视图会创建 Illegalstateexception(Android, Java)

Posted

技术标签:

【中文标题】从 XML 膨胀自定义视图会创建 Illegalstateexception(Android, Java)【英文标题】:Inflating custom view from XML creates Illegalstateexception(Android, Java) 【发布时间】:2011-05-22 14:05:56 【问题描述】:

我已经在一些恼人的运行时异常上停留了大约 12 个小时,当我尝试扩展我创建的自定义视图时,这种异常一直在发生。这是我的第一个 android(和 Java)程序,所以希望我没有做一些非常愚蠢的事情。

XML:

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_ 
    android:layout_
    android:padding="10px" >

<com.studentfees.GraphView/>

</RelativeLayout>

类:

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

        



日志:

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

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

编辑:

第一次回答后的新日志 - 感谢您的帮助 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)

【问题讨论】:

【参考方案1】:

从您的日志中:

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)

您必须指定layout_widthlayout_height

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_ 
    android:layout_
    android:padding="10px" >

   <com.studentfees.GraphView
         android:layout_ 
         android:layout_ />

</RelativeLayout>

通常您必须为布局内的所有项目指定layout_widthlayout_height。在某些情况下,这是可选的,但有据可查。


现在您的应用程序由于另一个原因而失败:NullPointerException。它发生在您的GraphSetUp 函数中:

switch(graphtype)

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

【讨论】:

已添加布局参数但仍然遇到同样的问题:( 你得到完全相同的日志吗?可以为您的问题添加新日志吗?

以上是关于从 XML 膨胀自定义视图会创建 Illegalstateexception(Android, Java)的主要内容,如果未能解决你的问题,请参考以下文章

在 Android 中创建复合视图

膨胀视图时找不到 xml id

将标签和侦听器添加到多次膨胀的 XML 的视图中

Android:自定义视图上的膨胀异常

在 Android 中膨胀视图后,自定义视图子项为空

Android:在片段内膨胀自定义视图