膨胀内部类视图时出错

发布于 2024-10-08 12:11:40 字数 5518 浏览 2 评论 0原文

我试图膨胀内部类视图并遇到“错误膨胀类”问题。我引用了这篇文章有帮助,但我仍然不知道为什么我无法让它发挥作用。

这是代码摘录、我的 XML 和 logcat。

编辑:我还注意到,当我明确拥有“CupcakeMessageView(Context, AttributeSet)”时​​,logcat 声明“没有此类方法异常”。

谢谢大家!

public class CupcakeMessage extends Activity {

    class CupcakeMessageView extends View {
        private static final int TEXT_SIZE = 12;
        private static final int X_OFFSET = 15;

        private Paint mPaint;   

        public CupcakeMessageView(Context context) {
            super(context);
        }

        public CupcakeMessageView(Context context, AttributeSet attrs) {
            super(context, attrs);
            mContext = context;
            ...
        }

        more class stuff... 
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        setContentView(R.layout.cupcake_message); 

        ...      
    }
}

这是 XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <view class="com.cmllc.zcc.CupcakeMessage$CupcakeMessageView"
        android:id="@+id/cupcake_message_view"
        android:screenOrientation="portrait"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    more views...

Logcat:

12-19 09:31:24.249: ERROR/AndroidRuntime(5435): FATAL EXCEPTION: main
12-19 09:31:24.249: ERROR/AndroidRuntime(5435): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cmllc.zcc/com.cmllc.zcc.CupcakeMessage}: android.view.InflateException: Binary XML file line #7: Error inflating class com.cmllc.zcc.CupcakeMessage$CupcakeMessageView
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at android.os.Looper.loop(Looper.java:123)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at android.app.ActivityThread.main(ActivityThread.java:4627)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at java.lang.reflect.Method.invokeNative(Native Method)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at java.lang.reflect.Method.invoke(Method.java:521)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at dalvik.system.NativeStart.main(Native Method)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class com.cmllc.zcc.CupcakeMessage$CupcakeMessageView
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at android.view.LayoutInflater.createView(LayoutInflater.java:503)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at android.app.Activity.setContentView(Activity.java:1647)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at com.cmllc.zcc.CupcakeMessage.onCreate(CupcakeMessage.java:119)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     ... 11 more
12-19 09:31:24.249: ERROR/AndroidRuntime(5435): Caused by: java.lang.NoSuchMethodException: CupcakeMessageView(Context,AttributeSet)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at java.lang.Class.getMatchingConstructor(Class.java:660)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at java.lang.Class.getConstructor(Class.java:477)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at android.view.LayoutInflater.createView(LayoutInflater.java:475)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     ... 21 more

I'm trying to inflate an inner class view and getting the "error inflating class" problem. I've referenced this post which was helpful, but I'm still lost as to why I can't get this to work.

Here is a code excerpt, my XML, and logcat.

Edit: Also I noticed that logcat states a "no such method exception" for "CupcakeMessageView(Context, AttributeSet) when I clearly have it.

THANKS ALL!

public class CupcakeMessage extends Activity {

    class CupcakeMessageView extends View {
        private static final int TEXT_SIZE = 12;
        private static final int X_OFFSET = 15;

        private Paint mPaint;   

        public CupcakeMessageView(Context context) {
            super(context);
        }

        public CupcakeMessageView(Context context, AttributeSet attrs) {
            super(context, attrs);
            mContext = context;
            ...
        }

        more class stuff... 
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        setContentView(R.layout.cupcake_message); 

        ...      
    }
}

Here is the XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <view class="com.cmllc.zcc.CupcakeMessage$CupcakeMessageView"
        android:id="@+id/cupcake_message_view"
        android:screenOrientation="portrait"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    more views...

Logcat:

12-19 09:31:24.249: ERROR/AndroidRuntime(5435): FATAL EXCEPTION: main
12-19 09:31:24.249: ERROR/AndroidRuntime(5435): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cmllc.zcc/com.cmllc.zcc.CupcakeMessage}: android.view.InflateException: Binary XML file line #7: Error inflating class com.cmllc.zcc.CupcakeMessage$CupcakeMessageView
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at android.os.Looper.loop(Looper.java:123)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at android.app.ActivityThread.main(ActivityThread.java:4627)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at java.lang.reflect.Method.invokeNative(Native Method)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at java.lang.reflect.Method.invoke(Method.java:521)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at dalvik.system.NativeStart.main(Native Method)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class com.cmllc.zcc.CupcakeMessage$CupcakeMessageView
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at android.view.LayoutInflater.createView(LayoutInflater.java:503)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at android.app.Activity.setContentView(Activity.java:1647)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at com.cmllc.zcc.CupcakeMessage.onCreate(CupcakeMessage.java:119)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     ... 11 more
12-19 09:31:24.249: ERROR/AndroidRuntime(5435): Caused by: java.lang.NoSuchMethodException: CupcakeMessageView(Context,AttributeSet)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at java.lang.Class.getMatchingConstructor(Class.java:660)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at java.lang.Class.getConstructor(Class.java:477)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     at android.view.LayoutInflater.createView(LayoutInflater.java:475)
12-19 09:31:24.249: ERROR/AndroidRuntime(5435):     ... 21 more

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑饮青盏花 2024-10-15 12:11:40

您需要将 public static 添加到内部类。

  • public 因为它需要从外部可见。
  • static 因为如果不是,您将需要 CupcakeMessage 来实例化它。

尽管如此,我还是建议在单独的类中执行 View 操作。

You need to add public static to the inner class.

  • public because it needs to be visible from outside.
  • static because if not you will need to have CupcakeMessage to instantiate it.

All the same I would recommend doing the View in a separated class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文