Android MainActivity-admob类型中的复制方法onCreate(Bundle)

Android MainActivity-admob类型中的复制方法onCreate(Bundle),android,Android,发现了同样的问题,但没有得到答案,尽管再次 我试图将admob添加到MainActivity.java文件中,但收到“MainActivity类型中的复制方法onCreate(Bundle)”错误。我在编码方面是新手,请有人指导我如何修复它以及如何解决错误,我已经尝试过改变 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

发现了同样的问题,但没有得到答案,尽管再次

我试图将admob添加到MainActivity.java文件中,但收到“MainActivity类型中的复制方法onCreate(Bundle)”错误。我在编码方面是新手,请有人指导我如何修复它以及如何解决错误,我已经尝试过改变

protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

但它不起作用

package xxxxxx.xxxxxxx.xxxxxx.com;

import java.io.File;


import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {

    private AdView mAdView;

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MobileAds.initialize(this, "ca-app-pub-xxxxxxxxxxx/xxxxxxxxxxxx");

        mAdView = (AdView) findViewById(R.id.ad_View);


        AdRequest adRequest = new AdRequest.Builder()
                .build();

        mAdView.loadAd(adRequest);

    }



boolean firstResume = true;



private ViewPager viewPager;
private TabsAdapter mAdapter;
private ActionBar actionBar;
Handler handler1 = new Handler();
private Context context;
private int i = 0;
private boolean exitAds = false;



private String[] tabs = { "Account", "Downloads" };

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

context = this;
String mBaseFolderPath = android.os.Environment
        .getExternalStorageDirectory()
        + File.separator
        + "Videos2" + File.separator;
if (!new File(mBaseFolderPath).exists()) {
    new File(mBaseFolderPath).mkdir();
}

// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsAdapter(getSupportFragmentManager());

viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

// Adding Tabs
for (String tab_name : tabs) {
    actionBar.addTab(actionBar.newTab().setText(tab_name)
            .setTabListener(this));
}

/**
 * on swiping the viewpager make respective tab selected
 * */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

    @Override
    public void onPageSelected(int position) {
        // on changing the page
        // make respected tab selected
        actionBar.setSelectedNavigationItem(position);
    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
    }

    @Override
    public void onPageScrollStateChanged(int arg0) {
    }
});

}

@Override
protected void onResume() {
super.onResume();
if (!firstResume) {

} else {
    firstResume = false;
}
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
if (tab.getPosition() == 1) {
    Log.e("UPDATED", "DOWNLOADS SELECTED");
    final DownloadsFragment fragment = ((DownloadsFragment) getSupportFragmentManager()
            .findFragmentByTag("android:switcher:" + R.id.pager + ":1"));
    fragment.updateDownloads();

    // if (i == 0) {
    // i++;
    // } else if (i > 0) {
    // }

} // else
    // displayInterstitial();
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_share) {
    // create the send intent
    Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);

    // set the type
    shareIntent.setType("text/plain");

    // add a subject
    shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
            "Share App with Friends");

    // build the body of the message to be shared
    String shareMessage = " Download from any of the links below \n"
            + "market://details?id=xxxxxxx.xxxxxxxxxxx.xxxxxx \n"
            + "https://play.google.com/store/apps/details?id=xxxxxx.comxxxxxxx.xxx";

    // add the message
    shareIntent.putExtra(android.content.Intent.EXTRA_TEXT,
            shareMessage);

    // start the chooser for sharing
    startActivity(Intent.createChooser(shareIntent, "Share App"));
    return true;
}
if (id == R.id.action_rate) {

    Uri uri = Uri.parse("market://details?id="
            + context.getPackageName());
    Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
    goToMarket.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY
            | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET
            | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
    try {
        startActivity(goToMarket);
    } catch (ActivityNotFoundException e) {
        startActivity(new Intent(
                Intent.ACTION_VIEW,
                Uri.parse("http://play.google.com/store/apps/details?id="
                        + context.getPackageName())));
    }
    return true;

}
if (id == R.id.action_exit) {
    System.exit(0);
    this.finish();
}
return super.onOptionsItemSelected(item);

}

@Override
public void onBackPressed() {

super.onBackPressed();
finish();

}

}

如注释中所述,您只能有一个onCreate()方法。只需将代码组合成一个方法

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MobileAds.initialize(this, "ca-app-pub-xxxxxxxxxxx/xxxxxxxxxxxx");

    mAdView = (AdView) findViewById(R.id.ad_View);


    AdRequest adRequest = new AdRequest.Builder()
        .build();

    mAdView.loadAd(adRequest);

    context = this;
    String mBaseFolderPath = android.os.Environment
        .getExternalStorageDirectory() +
        File.separator +
        "Videos2" + File.separator;
    if (!new File(mBaseFolderPath).exists()) {
        new File(mBaseFolderPath).mkdir();
    }

    //                                    Initilization
    viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getActionBar();
    0
    mAdapter = new TabsAdapter(getSupportFragmentManager());

    viewPager.setAdapter(mAdapter);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    //                                    Adding Tabs
    for (String tab_name: tabs) {
        actionBar.addTab(actionBar.newTab().setText(tab_name)
            .setTabListener(this));
    }

    /**
     * on swiping the viewpager make respective tab selected
     * */
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            // on changing the page
            // make respected tab selected
            actionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {}

        @Override
        public void onPageScrollStateChanged(int arg0) {}
    });

}

你说的“它不起作用”到底是什么意思?这是您在
MainActivity
中的全部代码吗?正如错误告诉您的,在
MainActivity
中,您只能有一个
onCreate(Bundle)
方法。如果您只使用Android搜索Admob,您会发现您可以阅读的非常好的文章……我的意思是错误不会发生,这不是全部代码,我正在研究如何解决它?@maxx您能在
MainActivity
中显示其余的代码吗?或者至少是其中声明的方法?您提供的此代码片段不会给出该错误。显然有两种方法称为
onCreate
。在使用广告之前,你应该先从basis开始学习,这可能是最后要添加的内容。非常感谢兄弟。你节省了我的时间:)解决了!
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MobileAds.initialize(this, "ca-app-pub-xxxxxxxxxxx/xxxxxxxxxxxx");

    mAdView = (AdView) findViewById(R.id.ad_View);


    AdRequest adRequest = new AdRequest.Builder()
        .build();

    mAdView.loadAd(adRequest);

    context = this;
    String mBaseFolderPath = android.os.Environment
        .getExternalStorageDirectory() +
        File.separator +
        "Videos2" + File.separator;
    if (!new File(mBaseFolderPath).exists()) {
        new File(mBaseFolderPath).mkdir();
    }

    //                                    Initilization
    viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getActionBar();
    0
    mAdapter = new TabsAdapter(getSupportFragmentManager());

    viewPager.setAdapter(mAdapter);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    //                                    Adding Tabs
    for (String tab_name: tabs) {
        actionBar.addTab(actionBar.newTab().setText(tab_name)
            .setTabListener(this));
    }

    /**
     * on swiping the viewpager make respective tab selected
     * */
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            // on changing the page
            // make respected tab selected
            actionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {}

        @Override
        public void onPageScrollStateChanged(int arg0) {}
    });

}