从主Xml调用SurfaceView时出现类错误

时间:2017-05-05 09:19:16

标签: android xml

我一直在尝试将admob代码添加到我的应用中。我通常直接从mainactivity.java调用surfaceview。

因为我需要将admob代码添加到main xml中,所以我尝试使用标记从main xml调用我的surfaceview。

当我尝试这样做时,我在标签的开头出现了一个Inflatin Class错误。到目前为止,我在stackoverflow上找到的解决方案对我没有帮助。有人能帮我吗?

这是mainactivity.java

protected void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    /*mGLSurfaceView = new CCGLSurfaceView(this);*/

    setContentView(R.layout.activity_gold_miner);
     mGLSurfaceView = (CCGLSurfaceView) findViewById(R.id.VIEWID); 

这是Main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"

android:layout_width="fill_parent"
android:layout_height="fill_parent"

tools:context=".GoldMinerActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@string/hello_world" />

<com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    ads:adSize="BANNER"
    ads:adUnitId="@string/banner_ad_unit_id"></com.google.android.gms.ads.AdView>



<view class="org.cocos2d.opengl.CCGLSurfaceView"
    android:id="@+id/VIEWID"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>

这是LogCat错误

05-05 15:29:58.094: E/AndroidRuntime(8445): FATAL EXCEPTION: main
05-05 15:29:58.094: E/AndroidRuntime(8445): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.goldminer/org.goldminer.GoldMinerActivity}: android.view.InflateException: Binary XML file line #29: Error inflating class org.cocos2d.opengl.CCGLSurfaceView
05-05 15:29:58.094: E/AndroidRuntime(8445):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-05 15:29:58.094: E/AndroidRuntime(8445):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-05 15:29:58.094: E/AndroidRuntime(8445):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-05 15:29:58.094: E/AndroidRuntime(8445):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-05 15:29:58.094: E/AndroidRuntime(8445):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-05 15:29:58.094: E/AndroidRuntime(8445):     at android.os.Looper.loop(Looper.java:137)
05-05 15:29:58.094: E/AndroidRuntime(8445):     at android.app.ActivityThread.main(ActivityThread.java:5041)
05-05 15:29:58.094: E/AndroidRuntime(8445):     at java.lang.reflect.Method.invokeNative(Native Method)
05-05 15:29:58.094: E/AndroidRuntime(8445):     at java.lang.reflect.Method.invoke(Method.java:511)
05-05 15:29:58.094: E/AndroidRuntime(8445):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-05 15:29:58.094: E/AndroidRuntime(8445):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-05 15:29:58.094: E/AndroidRuntime(8445):     at dalvik.system.NativeStart.main(Native Method)
05-05 15:29:58.094: E/AndroidRuntime(8445): Caused by: android.view.InflateException: Binary XML file line #29: Error inflating class org.cocos2d.opengl.CCGLSurfaceView
05-05 15:29:58.094: E/AndroidRuntime(8445):     at android.view.LayoutInflater.createView(LayoutInflater.java:596)
05-05 15:29:58.094: E/AndroidRuntime(8445):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
05-05 15:29:58.094: E/AndroidRuntime(8445):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
05-05 15:29:58.094: E/AndroidRuntime(8445):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
05-05 15:29:58.094: E/AndroidRuntime(8445):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
05-05 15:29:58.094: E/AndroidRuntime(8445):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
05-05 15:29:58.094: E/AndroidRuntime(8445):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
05-05 15:29:58.094: E/AndroidRuntime(8445):     at android.app.Activity.setContentView(Activity.java:1899)
05-05 15:29:58.094: E/AndroidRuntime(8445):     at org.goldminer.GoldMinerActivity.onCreate(GoldMinerActivity.java:52)
05-05 15:29:58.094: E/AndroidRuntime(8445):     at android.app.Activity.performCreate(Activity.java:5122)
05-05 15:29:58.094: E/AndroidRuntime(8445):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-05 15:29:58.094: E/AndroidRuntime(8445):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-05 15:29:58.094: E/AndroidRuntime(8445):     ... 11 more
05-05 15:29:58.094: E/AndroidRuntime(8445): Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
05-05 15:29:58.094: E/AndroidRuntime(8445):     at java.lang.Class.getConstructorOrMethod(Class.java:460)
05-05 15:29:58.094: E/AndroidRuntime(8445):     at java.lang.Class.getConstructor(Class.java:431)
05-05 15:29:58.094: E/AndroidRuntime(8445):     at android.view.LayoutInflater.createView(LayoutInflater.java:561)
05-05 15:29:58.094: E/AndroidRuntime(8445):     ... 22 more
05-05 15:29:58.094: W/ActivityManager(2704):   Force finishing activity org.goldminer/.GoldMinerActivity

Inflating Class Error

提前谢谢

1 个答案:

答案 0 :(得分:0)

看起来CCGLSurfaceView无法从XML实例化,因为它缺少构造函数

CCGLSurfaceView(Context context, AttributeSet attrs);

您的选择

1)创建一个扩展CCGLSurfaceView的类来实现这样的构造函数

2)将其动态添加到布局

第二种方法是:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".GoldMinerActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id"/>
</RelativeLayout>

onCreate之后的setContentView

.
.
ViewGroup root = (ViewGroup)findViewById(R.id.root);

// create the surfaceview
CCGLSurfaceView cocos = new CCGLSurfaceView(this);

// create the layout params (tweak this to your needs)
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);

root.addView(cocos, 0, layoutParams);
.
.

addView中的“0”会导致您在其余视图后面创建SurfaceView,因此广告已覆盖它,但您可以使用垂直LinearLayout根布局,或创建更精细的LayoutParams,因此AdView低于surfaceView而不是它。