无法启动活动ComponentInfo ... java.lang.NullPointerException

时间:2014-08-12 06:16:53

标签: java android admob

我尝试在this教程的帮助下创建的简单游戏中使用AdMob广告,但我收到的NullPointerException错误似乎无法解决。

以下是我修改的AndroidGame类,其中包含一个线性布局(我相信这是AdMob广告所必需的)

package com.kilobolt.framework.implementation;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.view.Window;
import android.view.WindowManager;

import com.harryhochwarter.frugalfarminggame.R;
import com.kilobolt.framework.Audio;
import com.kilobolt.framework.FileIO;
import com.kilobolt.framework.Game;
import com.kilobolt.framework.Graphics;
import com.kilobolt.framework.Input;
import com.kilobolt.framework.Screen;

public abstract class AndroidGame extends Activity implements Game {
    AndroidFastRenderView renderView;
    Graphics graphics;
    Audio audio;
    Input input;
    FileIO fileIO;
    Screen screen;
    WakeLock wakeLock;

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

        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.main);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        boolean isPortrait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
        int frameBufferWidth = isPortrait ? 480: 800;
        int frameBufferHeight = isPortrait ? 800: 480;
        Bitmap frameBuffer = Bitmap.createBitmap(frameBufferWidth,
                frameBufferHeight, Config.RGB_565);

        float scaleX = (float) frameBufferWidth
                / getWindowManager().getDefaultDisplay().getWidth();
        float scaleY = (float) frameBufferHeight
                / getWindowManager().getDefaultDisplay().getHeight();

        renderView = new AndroidFastRenderView(this, frameBuffer);
        graphics = new AndroidGraphics(getAssets(), frameBuffer);
        fileIO = new AndroidFileIO(this);
        audio = new AndroidAudio(this);
        input = new AndroidInput(this, renderView, scaleX, scaleY);
        screen = getInitScreen();
        setContentView(renderView);

        PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
        wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "MyGame");
    }

    @Override
    public void onResume() {
        super.onResume();
        wakeLock.acquire();
        screen.resume();
        renderView.resume();
    }

    @Override
    public void onPause() {
        super.onPause();
        wakeLock.release();
        renderView.pause();
        screen.pause();

        if (isFinishing())
            screen.dispose();
    }

    @Override
    public Input getInput() {
        return input;
    }

    @Override
    public FileIO getFileIO() {
        return fileIO;
    }

    @Override
    public Graphics getGraphics() {
        return graphics;
    }

    @Override
    public Audio getAudio() {
        return audio;
    }

    @Override
    public void setScreen(Screen screen) {
        if (screen == null)
            throw new IllegalArgumentException("Screen must not be null");

        this.screen.pause();
        this.screen.dispose();
        screen.resume();
        screen.update(0);
        this.screen = screen;
    }

    public Screen getCurrentScreen() {

        return screen;
    }
}

以下是应用程序启动时运行的SampleGame类(我也不确定我的测试设备代码是否正确,因为我无法弄清楚如何检查它。

package com.harryhochwarter.frugalfarminggame;

import android.os.Bundle;

import com.google.android.gms.ads.*;

import com.kilobolt.framework.Screen;
import com.kilobolt.framework.implementation.AndroidGame;

public class SampleGame extends AndroidGame
{
    public static String map;
    boolean firstTimeCreate = true;
    //
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        AdView adView = (AdView)findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder()
        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
        .addTestDevice("AC98C820A50B4AD8A2106EDE96FB87D4")
        .build();
        adView.loadAd(adRequest);
    }

    @Override
    public Screen getInitScreen()
    {
        if(firstTimeCreate)
        {
            Assets.load(this);
            firstTimeCreate = false;
        }

        return new SplashLoadingScreen(this);
    }

    @Override
    public void onBackPressed()
    {
        getCurrentScreen().backButton();
    }

    @Override
    public void onResume()
    {
        super.onResume();
        //Assets.theme.play();
    }

    @Override
    public void onPause()
    {
        super.onPause();
        //Assets.theme.pause();
    }
}

最后,这是我的logcat输出

08-12 06:03:13.890: D/dalvikvm(1317): GC_FOR_ALLOC freed 72K, 9% free 2179K/2392K, paused 85ms, total 91ms
08-12 06:03:13.890: I/dalvikvm-heap(1317): Grow heap (frag case) to 2.993MB for 768016-byte allocation
08-12 06:03:13.900: D/dalvikvm(1317): GC_FOR_ALLOC freed <1K, 7% free 2928K/3144K, paused 12ms, total 12ms
08-12 06:03:14.150: D/AndroidRuntime(1317): Shutting down VM
08-12 06:03:14.150: W/dalvikvm(1317): threadid=1: thread exiting with uncaught exception (group=0xb1aeed70)
08-12 06:03:14.160: E/AndroidRuntime(1317): FATAL EXCEPTION: main
08-12 06:03:14.160: E/AndroidRuntime(1317): Process: com.harryhochwarter.frugalfarminggame, PID: 1317
08-12 06:03:14.160: E/AndroidRuntime(1317): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.harryhochwarter.frugalfarminggame/com.harryhochwarter.frugalfarminggame.SampleGame}: java.lang.NullPointerException
08-12 06:03:14.160: E/AndroidRuntime(1317):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2197)
08-12 06:03:14.160: E/AndroidRuntime(1317):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2258)
08-12 06:03:14.160: E/AndroidRuntime(1317):     at android.app.ActivityThread.access$800(ActivityThread.java:138)
08-12 06:03:14.160: E/AndroidRuntime(1317):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1209)
08-12 06:03:14.160: E/AndroidRuntime(1317):     at android.os.Handler.dispatchMessage(Handler.java:102)
08-12 06:03:14.160: E/AndroidRuntime(1317):     at android.os.Looper.loop(Looper.java:136)
08-12 06:03:14.160: E/AndroidRuntime(1317):     at android.app.ActivityThread.main(ActivityThread.java:5026)
08-12 06:03:14.160: E/AndroidRuntime(1317):     at java.lang.reflect.Method.invokeNative(Native Method)
08-12 06:03:14.160: E/AndroidRuntime(1317):     at java.lang.reflect.Method.invoke(Method.java:515)
08-12 06:03:14.160: E/AndroidRuntime(1317):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
08-12 06:03:14.160: E/AndroidRuntime(1317):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
08-12 06:03:14.160: E/AndroidRuntime(1317):     at dalvik.system.NativeStart.main(Native Method)
08-12 06:03:14.160: E/AndroidRuntime(1317): Caused by: java.lang.NullPointerException
08-12 06:03:14.160: E/AndroidRuntime(1317):     at com.harryhochwarter.frugalfarminggame.SampleGame.onCreate(SampleGame.java:25)
08-12 06:03:14.160: E/AndroidRuntime(1317):     at android.app.Activity.performCreate(Activity.java:5242)
08-12 06:03:14.160: E/AndroidRuntime(1317):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-12 06:03:14.160: E/AndroidRuntime(1317):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)
08-12 06:03:14.160: E/AndroidRuntime(1317):     ... 11 more
08-12 06:08:15.060: I/Process(1317): Sending signal. PID: 1317 SIG: 9

如果我需要发布任何其他内容或格式化不同的内容,请告知我们。我是stackoverflow和android开发的新手。

根据mohan的要求,这是main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:ads="http://schemas.android.com/apk/res-auto"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
  <com.google.android.gms.ads.AdView android:id="@+id/adView"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         ads:adUnitId="ca-app-pub-9472185097153282/1248179452"
                         ads:adSize="BANNER"/>
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

您的代码AdView adView = (AdView)findViewById(R.id.adView);无法在布局中找到该视图。这就是为什么adView对象为null并在您尝试访问该对象的方法时给出nullpointerexception。

检查布局文件夹中的main.xml文件中是否有标识为"@+id/adView"的Admob广告视图。