0

Eigentlich versuche ich eine App mit kostenlosen und kostenpflichtigen Varianten zu erstellen . Wenn ich die findViewById-Methode zum Binden verwende, funktioniert es einwandfrei. Aber als ich Butterknife hinzufügen wollte, steckte ich verzweifelt mit ButterKnife fest.So lösen Sie NullPointerException void com.google.android.gms.ads.AdView.loadAd (com.google.android.gms.ads.AdRequest) mit Butterknife

Ich versuche Inhalt (wie AdMob OR Button) durch Butterknife binden in MainActivity.java Datei zu binden. aber es zeigt Nullpointer Ausnahme Fehler zuerst es auf AdMob-Objekt nullpointer Ausnahme zeigt. Ich laufe sauberes Projekt dann zeigt es auf button.onClickListener Nullpointer Ausnahme.

Bitte helfen Sie mir ...

Hier ist der Fehler: die zeigt AdMob Nullpointer:

09-08 19:04:30.860 19466-19466/? E/AndroidRuntime: FATAL EXCEPTION: main 
                Process: com.santossingh.jokeapp.free, PID: 19466 
                java.lang.RuntimeException: Unable to start activity ComponentInfo{com.santossingh.jokeapp.free/com.santossingh.jokeapp.free.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.ads.AdView.loadAd(com.google.android.gms.ads.AdRequest)' on a null object reference 
                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) 
                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                 at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                 at android.os.Looper.loop(Looper.java:148) 
                 at android.app.ActivityThread.main(ActivityThread.java:5417) 
                 at java.lang.reflect.Method.invoke(Native Method) 
                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.ads.AdView.loadAd(com.google.android.gms.ads.AdRequest)' on a null object reference 
                 at com.santossingh.jokeapp.free.MainActivity.onCreate(MainActivity.java:62) 
                 at android.app.Activity.performCreate(Activity.java:6237) 
                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  
                 at android.app.ActivityThread.-wrap11(ActivityThread.java)  
                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)  
                 at android.os.Handler.dispatchMessage(Handler.java:102)  
                 at android.os.Looper.loop(Looper.java:148)  
                 at android.app.ActivityThread.main(ActivityThread.java:5417)  
                 at java.lang.reflect.Method.invoke(Native Method)  
                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  
                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  
09-08 19:04:35.652 837-2192/system_process E/Surface: getSlotFromBufferLocked: unknown buffer: 0xf2cf6c90 
09-08 19:04:56.030 837-853/system_process E/BluetoothAdapter: Bluetooth binder is null 
09-08 19:06:20.986 837-853/system_process E/BluetoothAdapter: Bluetooth binder is null 
09-08 19:07:41.011 837-853/system_process E/BluetoothAdapter: Bluetooth binder is null 
09-08 19:09:01.042 837-853/system_process E/BluetoothAdapter: Bluetooth binder is null 
09-08 19:10:20.982 837-853/system_process E/BluetoothAdapter: Bluetooth binder is null 
09-08 19:11:46.019 837-853/system_process E/BluetoothAdapter: Bluetooth binder is null 
09-08 19:13:06.051 837-853/system_process E/BluetoothAdapter: Bluetooth binder is null 
09-08 19:14:41.018 837-853/system_process E/BluetoothAdapter: Bluetooth binder is null 
09-08 19:16:06.047 837-853/system_process E/BluetoothAdapter: Bluetooth binder is null 

2-MainActivity.java

public class MainActivity extends AppCompatActivity implements AsyncResponse{ 

    @BindView(R.id.avi) AVLoadingIndicatorView avLoadingIndicatorView; 
    @BindView(R.id.button_jokeTeller) Button button_JokeTeller; 
    @BindView(R.id.instruction_TextView) 
    TextView instruction; 
    @BindView(R.id.container) RelativeLayout relativeLayout; 
    @BindView(R.id.adView) AdView adView; 
    @BindView(R.id.progressBar) LinearLayout linearLayout; 

    private InterstitialAd mInterstitialAd; 
    EndpointsAsyncTask endpointsAsyncTask; 
    private static final String JOKE_TAG="joke"; 

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

     endpointsAsyncTask = new EndpointsAsyncTask(this); 

     AdRequest adRequestBanner = new AdRequest.Builder().build(); 
     adView.loadAd(adRequestBanner); 

     mInterstitialAd = new InterstitialAd(this); 
     mInterstitialAd.setAdUnitId(getString(R.string.interstitial_ad_unit_id)); 
     AdRequest adRequestInterstitial = new AdRequest.Builder().build(); 
     mInterstitialAd.loadAd(adRequestInterstitial); 

     button_JokeTeller.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       showInterstitial(); 
      } 
     }); 

     mInterstitialAd.setAdListener(new AdListener() { 
      @Override 
      public void onAdClosed() { 
       showProgressbar(true); 
       showJoke(); 
      } 

      @Override 
      public void onAdFailedToLoad(int errorCode) { 
       Toast.makeText(getApplicationContext(), "Ad failed to load! error code: " + errorCode, Toast.LENGTH_SHORT).show(); 
      } 

      @Override 
      public void onAdLeftApplication() { 
       showProgressbar(true); 
       Toast.makeText(getApplicationContext(), "Ad left application!", Toast.LENGTH_SHORT).show(); 
      } 

      @Override 
      public void onAdOpened() { 
       Toast.makeText(getApplicationContext(), "Ad is opened!", Toast.LENGTH_SHORT).show(); 
      } 
     }); 
    } 
    private void showInterstitial() { 
     if (mInterstitialAd.isLoaded()) { 
      mInterstitialAd.show(); 
     } 
    } 

    public void showJoke(){ 
     endpointsAsyncTask.execute(getString(R.string.keyword)); 
    } 

    @Override 
    public void processFinish(String result) { 
     Intent intent = new Intent(this, JokeActivity 
       .class) 
       .putExtra(JOKE_TAG, result); 
     showProgressbar(false); 
     startActivity(intent); 
    } 

    public void showProgressbar(boolean a){ 
     if(a==true){ 
      relativeLayout.setVisibility(View.GONE); 
      linearLayout.setVisibility(View.VISIBLE); 
     }else{ 
      linearLayout.setVisibility(View.GONE); 
      relativeLayout.setVisibility(View.VISIBLE); 
     } 
    } 
} 

3- activity_main.xml Datei

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    android:id="@+id/activity_main" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#0e73a6" 
    android:padding="10dp" 
    tools:context="com.santossingh.jokeapp.free.MainActivity" 
    xmlns:ads="http://schemas.android.com/apk/res-auto" 
    xmlns:app="http://schemas.android.com/apk/res-auto"> 
    <LinearLayout 
     android:id="@+id/progressBar" 
     android:layout_width="match_parent" 
     android:gravity="center" 
     android:layout_height="match_parent"> 

    <com.wang.avi.AVLoadingIndicatorView 
     android:id="@+id/avi" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     style="@style/AVLoadingIndicatorView" 
     android:visibility="visible" 
     app:indicatorName="BallPulseIndicator" 
    /> 
</LinearLayout> 
    <RelativeLayout android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:id="@+id/container"> 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/instructions" 
      android:textColor="#fff" 
      android:id="@+id/instruction_TextView" 
     /> 

     <Button android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/button_jokeTeller" 
       android:layout_below="@+id/instruction_TextView" 
       android:text="@string/button_text" 
     /> 

     <!-- view for AdMob Banner Ad --> 
     <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> 

</RelativeLayout> 

4- Manifest-Datei

<manifest xmlns:android="http://schemas.android.com/apk/res/android"> 

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

    <application> 
     <activity android:name="com.santossingh.jokeapp.free.MainActivity" 
     > 
      <!-- This meta-data tag is required to use Google Play Services. --> 
      <meta-data 
       android:name="com.google.android.gms.version" 
       android:value="@integer/google_play_services_version"/> 

      <intent-filter> 
       <action android:name="android.intent.action.MAIN"/> 
       <category android:name="android.intent.category.LAUNCHER"/> 
      </intent-filter> 
     </activity> 
    </application> 

    <activity 
     android:name="com.google.android.gms.ads.AdActivity" 
     android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" 
     android:theme="@android:style/Theme.Translucent"/> 
</manifest> 

Antwort

1

Sie

adView = (AdView)this.findViewById(R.id.adView); 

Code ändern, wie unten

AdRequest adRequestBanner = new AdRequest.Builder().build(); 
**adView = (AdView)this.findViewById(R.id.adView);** 
     adView.loadAd(adRequestBanner); 

Update-Abhängigkeit für

dependencies { compile 'com.jakewharton:butterknife:8.0.1' apt 'com.jakewharton:butterknife-compiler:8.0.1' } 
+0

Hallo Amod Gokhale, eigentlich habe ich Buttermesser verwendet, um AdMob zu binden. Meine Frage ist, wie man diesen Fehler mit Butterjnife API löst. – Santosh

+0

haben Sie die Abhängigkeit richtig eingestellt?Abhängigkeiten { kompilieren 'com.jakewharton: Buttermesser: 8.0.1' apt 'com.jakewharton: Buttermesser-Compiler: 8.0.1' } –

+0

besuchen Sie bitte diesen Link http://stackoverflow.com/questions/37013619/butterknife -8-0-1-not-working –

0

die richtige Antwort für sie fehlt, ist, dass Ich benutzte grandle 2.2.0 oder neuere Version. Also danke für JakeWarton github link [https://github.com/JakeWharton/butterknife]which] beschreiben deutlich die Integration von Butterknife mit neuer Version von Gradle. Als Folge der Umsetzung der in butter neuere Version von gradle Schritt für Schritt wie folgt:

1- Erstens Classpath 'com.neenbedankt.gradle.plugins: android-apt: 1.8' hinzufügen Abhängigkeiten:

dependencies { 
     .... 
     classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' 
} 

2- in build.gradle App Modul butter Zweitens fügen

dependencies { 
    .... 
    compile 'com.jakewharton:butterknife:8.4.0' 
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0' 
} 

So über alle Schluss, dass, wenn wir gradle 2.2.0 oder eine neuere Version benutzt, dann müssen wir über Änderungen hinzuzufügen. Das ist es, so einfach ... :)

0

Ich konfrontiert ähnliches Problem. Wenn ich genau in Ressource-Datei überprüft gab es zwei Ressource-Datei für meine Aktivität:

\res\layout\activity_main_lauch.xml

\res\layout-v21\activity_main_lauch.xml

ich einzelne Datei wurde modifiziert, damit Fehler war es zu werfen. Wenn ich die Änderung in beiden Dateien anwendete, fing es an zu arbeiten.

Verwandte Themen