Absturz beim Einbinden von Admob

  • Antworten:8
  • OffenNicht stickyNicht beantwortet
  • Forum-Beiträge: 19

15.05.2016, 12:05:13 via Website

Hi,

wenn ich versuch Werbung über Admob in Android-Studio einzubinden, bekomme ich folgende Fehlermeldung:

05-15 08:47:55.338 2776-2776/com.us.aspirationindex E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.us.aspirationindex, PID: 2776
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.us.aspirationindex/com.us.aspirationindex.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.us.aspirationindex.MainActivity.onCreate(MainActivity.java:25)
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) 

Könnt ihr mir weiterhelfen.

VG

Antworten
Pascal P.
  • Mod
  • Blogger
  • Forum-Beiträge: 10.174

15.05.2016, 12:38:48 via App

Scheint so als wäre dein Objekt der adView null deswegen der Fehler

Zeig mal den Code der Initialisierung

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten
  • Forum-Beiträge: 19

15.05.2016, 16:38:26 via Website

Hi,

05-15 14:37:34.393 2890-2890/com.us.aspirationindex I/art: Not late-enabling -Xcheck:jni (already on)
05-15 14:37:34.575 2890-2890/com.us.aspirationindex W/System: ClassLoader referenced unknown path: /data/app/com.us.aspirationindex-2/lib/x86
05-15 14:37:35.541 2890-2896/com.us.aspirationindex W/art: Suspending all threads took: 9.427ms
05-15 14:37:42.166 2890-2890/com.us.aspirationindex W/System: ClassLoader referenced unknown path: /data/app/com.us.aspirationindex-2/lib/x86
05-15 14:37:42.511 2890-2900/com.us.aspirationindex I/art: Background sticky concurrent mark sweep GC freed 16142(1591KB) AllocSpace objects, 8(188KB) LOS objects, 69% free, 1144KB/3MB, paused 4.131ms total 129.764ms
05-15 14:37:42.523 2890-2900/com.us.aspirationindex W/art: Suspending all threads took: 11.751ms
05-15 14:37:42.564 2890-2896/com.us.aspirationindex W/art: Suspending all threads took: 6.410ms
05-15 14:37:43.070 2890-2890/com.us.aspirationindex D/AndroidRuntime: Shutting down VM

VG

Antworten
Pascal P.
  • Mod
  • Blogger
  • Forum-Beiträge: 10.174

15.05.2016, 16:49:04 via Website

Das ist der error Log nicht dein Java Code ;)
Der Code ist wichtiger denn der Log sagt nichts

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten
  • Forum-Beiträge: 19

15.05.2016, 21:28:45 via Website

public class MainActivity extends AppCompatActivity {
Button btneng,btndue;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    AdView mAdView = (AdView) findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);

    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle("");
    btneng= (Button) findViewById(R.id.buttonEng);
    btndue= (Button) findViewById(R.id.buttonDeu);
    btneng.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Constants.langkey=Constants.English;
            InitializeTrails();
        }
    });
    btndue.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Constants.langkey=Constants.Duetsch;
            InitializeTrails();
        }
    });
}

private void InitializeTrails() {
    ArrayList<String> temp=new ArrayList<String>();
    if(Constants.langkey.equals(Constants.English)) {

        //dimentions
        Constants.dimentionsArrayList.clear();
        Constants.dimentionsArrayList.addAll(Arrays.asList(getResources().getStringArray(R.array.engDimentions)));
        //dimentions

        //traits

        Constants.traitsArrayList.clear();

        temp.clear();
        temp.addAll(Arrays.asList(getResources().getStringArray(R.array.eng0)));
        for (String trait:temp) {
            Constants.traitsArrayList.add(new Trait(1,trait));
        }

        temp.clear();
        temp.addAll(Arrays.asList(getResources().getStringArray(R.array.eng1)));
        for (String trait:temp) {
            Constants.traitsArrayList.add(new Trait(2,trait));
        }

        temp.clear();
        temp.addAll(Arrays.asList(getResources().getStringArray(R.array.eng2)));
        for (String trait:temp) {
            Constants.traitsArrayList.add(new Trait(3,trait));
        }

        temp.clear();
        temp.addAll(Arrays.asList(getResources().getStringArray(R.array.eng3)));
        for (String trait:temp) {
            Constants.traitsArrayList.add(new Trait(4,trait));
        }

        temp.clear();
        temp.addAll(Arrays.asList(getResources().getStringArray(R.array.eng4)));
        for (String trait:temp) {
            Constants.traitsArrayList.add(new Trait(5,trait));
        }

        temp.clear();
        temp.addAll(Arrays.asList(getResources().getStringArray(R.array.eng5)));
        for (String trait:temp) {
            Constants.traitsArrayList.add(new Trait(6,trait));
        }

        temp.clear();
        temp.addAll(Arrays.asList(getResources().getStringArray(R.array.eng6)));
        for (String trait:temp) {
            Constants.traitsArrayList.add(new Trait(7,trait));
        }

        //traits
    }
    else
    {

        //dimentions
        Constants.dimentionsArrayList.clear();
        Constants.dimentionsArrayList.addAll(Arrays.asList(getResources().getStringArray(R.array.deuDimentions)));
        //dimentions

        //traits

        Constants.traitsArrayList.clear();

        temp.clear();
        temp.addAll(Arrays.asList(getResources().getStringArray(R.array.deu0)));
        for (String trait:temp) {
            Constants.traitsArrayList.add(new Trait(1,trait));
        }

        temp.clear();
        temp.addAll(Arrays.asList(getResources().getStringArray(R.array.deu1)));
        for (String trait:temp) {
            Constants.traitsArrayList.add(new Trait(2,trait));
        }

        temp.clear();
        temp.addAll(Arrays.asList(getResources().getStringArray(R.array.deu2)));
        for (String trait:temp) {
            Constants.traitsArrayList.add(new Trait(3,trait));
        }

        temp.clear();
        temp.addAll(Arrays.asList(getResources().getStringArray(R.array.deu3)));
        for (String trait:temp) {
            Constants.traitsArrayList.add(new Trait(4,trait));
        }

        temp.clear();
        temp.addAll(Arrays.asList(getResources().getStringArray(R.array.deu4)));
        for (String trait:temp) {
            Constants.traitsArrayList.add(new Trait(5,trait));
        }

        temp.clear();
        temp.addAll(Arrays.asList(getResources().getStringArray(R.array.deu5)));
        for (String trait:temp) {
            Constants.traitsArrayList.add(new Trait(6,trait));
        }

        temp.clear();
        temp.addAll(Arrays.asList(getResources().getStringArray(R.array.deu6)));
        for (String trait:temp) {
            Constants.traitsArrayList.add(new Trait(7,trait));
        }

        //traits
    }
    Collections.shuffle(Constants.traitsArrayList);
    Constants.Limit=Constants.traitsArrayList.size();
    startActivity(new Intent(this,ExplanationActivity.class));
}

}

Antworten
Pascal P.
  • Mod
  • Blogger
  • Forum-Beiträge: 10.174

15.05.2016, 21:35:30 via Website

set contentView muss vor allem anderen stehen:

    setContentView(R.layout.activity_main);
  AdView mAdView = (AdView) findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten
  • Forum-Beiträge: 19

16.05.2016, 09:16:48 via Website

Danke, hat funktioniert, d.h. die App stürzt nicht mehr ab.

Das Banner wird aber dennoch nicht angezeigt, oder soll ich dafür nen neuen Thread aufmachen.

— geändert am 16.05.2016, 09:16:59

Antworten
Pascal P.
  • Mod
  • Blogger
  • Forum-Beiträge: 10.174

16.05.2016, 10:44:08 via App

Nein brauchst du nicht hat ja noch in gewisser weise damit zutun.
Aber leider kann ich dir da nicht helfen habe ABMob noch nie selber benutzt.
Hast du dir mal ein Tut im inet durchgelesen?

— geändert am 18.05.2016, 11:13:04

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten

Empfohlene Artikel