如何覆盖一个函数?
Posted
技术标签:
【中文标题】如何覆盖一个函数?【英文标题】:How to override a function? 【发布时间】:2014-09-08 03:24:37 【问题描述】:您能告诉我如何正确覆盖“createViews”吗?如果我将 @Override 放在它前面,它会说:“WhatSnap 类型的方法 createViews() 必须覆盖或实现超类型方法” 如果我删除 @Override 它不会被执行。 这是第一堂课:https://github.com/apache/cordova-android/blob/master/framework/src/org/apache/cordova/CordovaActivity.java#L267
这是我要覆盖 createViews 的第二个:
package com.Snap.What;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.widget.RelativeLayout;
import org.apache.cordova.*;
import com.startapp.android.publish.StartAppAd;
import com.startapp.android.publish.StartAppSDK;
import com.startapp.android.publish.banner.Banner;
public class WhatSnap extends CordovaActivity
private CordovaWebView cordova_webview;
private StartAppAd startAppAd = new StartAppAd(this);
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
super.init();
// Set by <content src="index.html" /> in config.xml
super.loadUrl(Config.getStartUrl());
//super.loadUrl("file:///android_asset/www/index.html");
StartAppSDK.init(this, "xxx", "xxx", true);
Log.e("activity", "activity");
@Override
protected void createViews()
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT);
layout.setLayoutParams(lp);
appView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
Log.e("createViews", "createViews");
appView.setVisibility(View.INVISIBLE);
layout.addView((View) appView);
setContentView(layout);
Banner startAppBanner = new Banner(this);
RelativeLayout.LayoutParams bannerParameters =
new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
bannerParameters.addRule(RelativeLayout.CENTER_HORIZONTAL);
bannerParameters.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
// Add to main Layout
layout.addView(startAppBanner, bannerParameters);
@Override
protected CordovaWebView makeWebView ()
Log.e("CordovaWebView", "CordovaWebView");
return new CordovaWebView(WhatSnap.this);
【问题讨论】:
只需将createViews的签名改为protected.. @l0oky 我试过那个人但它不工作..我得到同样的错误! 【参考方案1】:您必须使用与超类方法相同的访问修饰符。将覆盖签名更改为,
@Override
protected void createViews()
//Your code here
【讨论】:
我试过那个人但它不工作..我得到同样的错误! 你是否也改变了 makeWebView() 的签名?您也以错误的方式覆盖了它。 我更改了 ti,但仍然收到错误 ar Override line that is above creatViews()以上是关于如何覆盖一个函数?的主要内容,如果未能解决你的问题,请参考以下文章