错误夸大内部类视图部类、视图、错误

2023-09-04 06:27:09 作者:不爱几条狗怎能货比三家

我试图夸大一个内部类视图,并得到错误充气类的问题。我引用this帖子这是有帮助的,但我还是输了,为什么我不能得到这个工作。

下面是一个code摘录,我的XML和logcat中。

编辑:我也注意到,logcat中规定了没有这样的方法异常的CupcakeMessageView(背景下,AttributeSet中)时,我清楚地把它

谢谢大家!

 公共类CupcakeMessage延伸活动{

    类CupcakeMessageView扩展视图{
        私有静态最终诠释TEXT_SIZE = 12;
        私有静态最终诠释X_OFFSET = 15;

        私人油漆mPaint;

        公共CupcakeMessageView(上下文的背景下){
            超(上下文);
        }

        公共CupcakeMessageView(上下文的背景下,ATTRS的AttributeSet){
            超(背景下,ATTRS);
            mContext =背景;
            ...
        }

        多个类的东西...
    }

    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.cupcake_message);

        ...
    }
}
 

下面是XML

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>

    <视图类=com.cmllc.zcc.CupcakeMessage $ CupcakeMessageView
        机器人:ID =@ + ID / cupcake_message_view
        机器人:screenOrientation =画像
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT/>
    更多的意见...
 
擦亮双眼 警惕13种常见中高考诈骗手段

logcat的:

 九月12号至19号:31:24.249:ERROR / AndroidRuntime(5435):致命异常:主要
9月12号至19号:31:24.249:ERROR / AndroidRuntime(5435):java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.cmllc.zcc / com.cmllc.zcc.CupcakeMessage}:android.view.InflateException:二进制XML文件行#7:错误充气类com.cmllc.zcc.CupcakeMessage $ CupcakeMessageView
9月12号至19号:31:24.249:ERROR / AndroidRuntime(5435):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
9月12号至19号:31:24.249:ERROR / AndroidRuntime(5435):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
9月12号至19号:31:24.249:ERROR / AndroidRuntime(5435):在android.app.ActivityThread.access $ 2300(ActivityThread.java:125)
9月12号至19号:31:24.249:ERROR / AndroidRuntime(5435):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033)
9月12号至19号:31:24.249:ERROR / AndroidRuntime(5435):在android.os.Handler.dispatchMessage(Handler.java:99)
9月12号至19号:31:24.249:ERROR / AndroidRuntime(5435):在android.os.Looper.loop(Looper.java:123)
9月12号至19号:31:24.249:ERROR / AndroidRuntime(5435):在android.app.ActivityThread.main(ActivityThread.java:4627)
9月12号至19号:31:24.249:ERROR / AndroidRuntime(5435):在java.lang.reflect.Method.invokeNative(本机方法)
9月12号至19号:31:24.249:ERROR / AndroidRuntime(5435):在java.lang.reflect.Method.invoke(Method.java:521)
9月12号至19号:31:24.249:ERROR / AndroidRuntime(5435):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
9月12号至19号:31:24.249:ERROR / AndroidRuntime(5435):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
9月12号至19号:31:24.249:ERROR / AndroidRuntime(5435):在dalvik.system.NativeStart.main(本机方法)
9月12号至19号:31:24.249:ERROR / AndroidRuntime(5435):android.view.InflateException:二进制XML文件中的行#7:错误充气类com.cmllc.zcc.CupcakeMessage $ CupcakeMessageView所致
9月12号至19号:31:24.249:ERROR / AndroidRuntime(5435):在android.view.LayoutInflater.createView(LayoutInflater.java:503)
9月12号至19号:31:24.249:ERROR / AndroidRuntime(5435):在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
9月12号至19号:31:24.249:ERROR / AndroidRuntime(5435):在android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
9月12号至19号:31:24.249:ERROR / AndroidRuntime(5435):在android.view.LayoutInflater.inflate(LayoutInflater.java:407)
9月12号至19号:31:24.249:ERROR / AndroidRuntime(5435):在android.view.LayoutInflater.inflate(LayoutInflater.java:320)
9月12号至19号:31:24.249:ERROR / AndroidRuntime(5435):在android.view.LayoutInflater.inflate(LayoutInflater.java:276)
9月12号至19号:31:24.249:ERROR / AndroidRuntime(5435):在com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
9月12号至19号:31:24.249:ERROR / AndroidRuntime(5435):在android.app.Activity.setContentView(Activity.java:1647)
9月12号至19号:31:24.249:ERROR / AndroidRuntime(5435):在com.cmllc.zcc.CupcakeMessage.onCreate(CupcakeMessage.java:119)
9月12号至19号:31:24.249:ERROR / AndroidRuntime(5435):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
9月12号至19号:31:24.249:ERROR / AndroidRuntime(5435):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
9月12号至19号:31:24.249:ERROR / AndroidRuntime(5435):11 ...更多
9月12号至19号:31:24.249:ERROR / AndroidRuntime(5435):java.lang.NoSuchMethodException:产生的原因CupcakeMessageView(背景下,AttributeSet中)
9月12号至19号:31:24.249:ERROR / AndroidRuntime(5435):在java.lang.Class.getMatchingConstructor(Class.java:660)
9月12号至19号:31:24.249:ERROR / AndroidRuntime(5435):在java.lang.Class.getConstructor(Class.java:477)
9月12号至19号:31:24.249:ERROR / AndroidRuntime(5435):在android.view.LayoutInflater.createView(LayoutInflater.java:475)
9月12号至19号:31:24.249:ERROR / AndroidRuntime(5435):21 ...更多
 

解决方案

您需要公共静态添加到内部类。

公开,因为它需要能够从外部观察。 静态,因为如果没有你需要有 CupcakeMessage 实例吧。

都一样,我建议做查看的分离类。

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

解决方案

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.