膨胀内部类视图时出错

Posted

技术标签:

【中文标题】膨胀内部类视图时出错【英文标题】:Error inflating inner class view 【发布时间】:2010-12-19 14:45:22 【问题描述】:

我正在尝试膨胀内部类视图并遇到“错误膨胀类”问题。我引用了 this post 这很有帮助,但我仍然不知道为什么我不能让它工作。

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

编辑:我还注意到,当我清楚地拥有它时,logcat 为“CupcakeMessageView(Context, AttributeSet) 声明了一个“没有此类方法异常”。

谢谢大家!

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_
    android:layout_ >

    <view class="com.cmllc.zcc.CupcakeMessage$CupcakeMessageView"
        android:id="@+id/cupcake_message_view"
        android:screenOrientation="portrait"
        android:layout_
        android:layout_ />
    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 ComponentInfocom.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

【问题讨论】:

尝试让你的内部类public static +1。那行得通。想解释一下原因吗? 声明公共静态会导致代码不可重入,因此仅在需要内部类的单个实例时才有效。请参阅(280Z28 的回答)***.com/questions/16986603/… 了解 Java 到 CSharp 内部类的转换 【参考方案1】:

你需要在内部类中添加public static

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

同样,我建议在单独的课程中使用View

【讨论】:

我最初确实将视图放在了一个单独的类中。我决定把它放在内部类中,因为视图基本上是整个活动减去几个按钮。对我来说,这个实现更简单,更容易理解。感谢您的帮助。

以上是关于膨胀内部类视图时出错的主要内容,如果未能解决你的问题,请参考以下文章

二进制 XML 文件第 28 行:二进制 XML 文件第 28 行:膨胀类 <unknown> 时出错

膨胀类 ImageView 时出错 - android.view.InflateException:二进制 XML 文件第 10 行:膨胀类 ImageView 时出错

膨胀类 CollapsingToolbarLayout 时出错

膨胀类片段时出错

在 RecyclerView 上膨胀类 ImageView 时出错

膨胀类 ImageButton 时出错