簡體   English   中英

Android Studio上的admob無法正常工作

[英]admob on android studio not working

我輸入以下代碼,並通過將其與手機連接並啟用USB調試進行測試,並且還生成了一個簽名的apk仍然無法正常工作,請幫幫我,我只是android studio和編寫android應用程序的新手。

在不是我真正的廣告單元ID的string.xml中,我檢查了一些數字

MainActivity.java

    package funaisraeljumbo.myapplication;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem; 
import android.view.View;
import android.widget.Button;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;



public class MainActivity extends ActionBarActivity {

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

    AdView mAdView = (AdView) findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);
}
public void onClick(View v){

    switch (v.getId()){
        case R.id.button3:
        Intent intent = new Intent(getBaseContext(), BasicDifferentiation.class);
        startActivity(intent);
        break;
    }

}


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.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();

         //noinspection SimplifiableIfStatement
         if (id == R.id.action_settings) {
              return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

activity_main.xml

<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textStyle="bold"
    android:textSize="20dp"
    android:id="@+id/textView"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="30dp"/>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/linearLayout"
    android:gravity="center"
    android:layout_above="@+id/adView">


    <Button
        android:layout_width="160dp"
        android:layout_height="wrap_content"
        android:text="@string/rules_for_differentiation"
        android:id="@+id/button3"
        android:onClick="onClick" />

    <Button
        android:layout_width="160dp"
        android:layout_height="wrap_content"
        android:text="@string/trigonometric_function"
        android:id="@+id/button"
        android:onClick="onClick" />

    <Button
        android:layout_width="160dp"
        android:layout_height="wrap_content"
        android:text="@string/log_and_expo"
        android:id="@+id/button2"
        android:onClick="onClick"
        android:layout_marginBottom="20dp"/>


</LinearLayout>

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

</LinearLayout>

build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "funaisraeljumbo.myapplication"
        minSdkVersion 16
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'),     '    proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.google.android.gms:play-services-ads:7.8.0'

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="funaisraeljumbo.myapplication" >


    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <meta-data android:name="com.google.gms.version"
            android:value="@integer/google_play_services_version"/>

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".BasicDifferentiation"
            android:label="@string/title_activity_basic_differentiation" >
        </activity>
        <activity android:name="com.google.android.gms.ads.AdActivity"
                     android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
            android:theme="@android:style/Theme.Translucent"/>

    </application>

</manifest>

string.xml

<resources>
    <string name="app_name">Differential Calculus Formula (Beta)</string>

    <string name="hello_world">Differential Calculus Formula</string>
    <string name="action_settings">Settings</string>
    <string name="rules_for_differentiation">Rules For Differentiation</string>
    <string name="trigonometric_function">Differentiation of Trigonometric Function</string>
    <string name="log_and_expo">Differentiation of Logarithmic and Exponential Function</string>
    <string name="title_activity_basic_differentiation">Basic Differentiation</string>
    <string name="home">ca-app-pub-1x104x92x9x2x52x/272xx4x6x2</string>
</resources>

打開SDK管理器並安裝Google Play服務。

然后將此行添加到Gradle(模塊)

  compile 'com.google.android.gms:play-services:9.4.0'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM