# Unable to start Activity



## Scheyschen (19. Jul 2015)

Hallo Programmierer,

ich habe ein Problem mit meinem Androidspiel. Dabei startet die Main Activity einfach nicht
Code: MainActivity

```
import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {
   private GameView theGameView;@Override
   protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(theGameView);
     theGameView = new GameView(this);
   }
}

Code GameView: import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import de.nikita.bangster.MainActivity;

public class GameView extends SurfaceView {
   private List < Sprite > spriteList = new ArrayList < Sprite > ();
   private List < Integer > spriteListNum = new ArrayList < Integer > ();
   private SurfaceHolder surfaceHolder;
   private Bitmap bmp = null;
   private Bitmap livesPicture;
   private theGameLoopThread thegameLoopThread;
   private boolean createSprites = true;
   private long lastClick;
   private int currentColorNum;
   private int lives = 4;
   private Paint paintRed, paintBlue, paintGreen, paintYellow;
   private Paint currentColor;
   private String scoreString;
   private String livesString;
   private float density;
   private MainActivity theMainActivity = new MainActivity();

   public GameView(Context context) {
     super(context);
     theMainActivity = (MainActivity) context;
     thegameLoopThread = new theGameLoopThread(this);
     surfaceHolder = getHolder();
     surfaceHolder.addCallback(new SurfaceHolder.Callback() {

       @Override
       public void surfaceDestroyed(SurfaceHolder holder) {
         boolean retry = true;
         thegameLoopThread.setRunning(false);
         while (retry) {
           try {
             thegameLoopThread.join();
             retry = false;
           } catch (InterruptedException e) {

           }
         }

       }

       @Override
       public void surfaceCreated(SurfaceHolder holder) {
         thegameLoopThread.setRunning(true);
         thegameLoopThread.start();
       }

       @Override
       public void surfaceChanged(SurfaceHolder holder, int format,
       int width, int height) {
         // TODO Auto-generated method stub

       }
     });
   }

   protected void onDraw(Canvas canvas) {

   }

   private void createSprite(int index) {
     if (bmp == null);
     switch (index) {
       case 0:
         bmp = BitmapFactory.decodeResource(getResources(),
         R.drawable.schwarz);
         break;
       case 1:
         bmp = BitmapFactory.decodeResource(getResources(), R.drawable.rot);
         break;
       case 2:
         bmp = BitmapFactory.decodeResource(getResources(),
         R.drawable.arschlecken);
         break;

     }
     Sprite sprite = new Sprite(this, bmp);
     spriteList.add(sprite);
     spriteListNum.add(index);

   }

   private void initialSprites() {
     for (int i = 0; i < 4; i++) {
       for (int j = 0; j < 3; j++)
       createSprite(i);
     }
     createSprites = false;
   }

   private void rndCreateSprite() {
     Random rnd = new Random(System.currentTimeMillis());
     int i = rnd.nextInt(4);
     createSprite(i);
   }

   @SuppressLint("ClickableViewAccessibility")@Override
   public boolean onTouchEvent(MotionEvent event) {
     if (System.currentTimeMillis() - lastClick > 300) {
       lastClick = System.currentTimeMillis();
       synchronized(getHolder()) {
         for (int i = spriteList.size() - 1; i >= 0; i--) {
           Sprite sprite = spriteList.get(i);
           if (sprite.isTouched(event.getX(), event.getY())) {
             rndCreateSprite();
             removeSprite(i);
             break;
           }
         }
       }
     }
     return true;
   }

   private void removeSprite(int index) {
     spriteList.remove(index);
     spriteListNum.remove(index);
   }
}
```

und als letztes die Fehlermeldung:

```
07-19 10:47:05.638: E/AndroidRuntime(339): FATAL EXCEPTION: main
07-19 10:47:05.638: E/AndroidRuntime(339): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.nikita.bangster/de.nikita.bangster.MainActivity}: java.lang.NullPointerException
07-19 10:47:05.638: E/AndroidRuntime(339):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-19 10:47:05.638: E/AndroidRuntime(339):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-19 10:47:05.638: E/AndroidRuntime(339):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-19 10:47:05.638: E/AndroidRuntime(339):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-19 10:47:05.638: E/AndroidRuntime(339):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-19 10:47:05.638: E/AndroidRuntime(339):     at android.os.Looper.loop(Looper.java:123)
07-19 10:47:05.638: E/AndroidRuntime(339):     at android.app.ActivityThread.main(ActivityThread.java:4627)
07-19 10:47:05.638: E/AndroidRuntime(339):     at java.lang.reflect.Method.invokeNative(Native Method)
07-19 10:47:05.638: E/AndroidRuntime(339):     at java.lang.reflect.Method.invoke(Method.java:521)
07-19 10:47:05.638: E/AndroidRuntime(339):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-19 10:47:05.638: E/AndroidRuntime(339):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-19 10:47:05.638: E/AndroidRuntime(339):     at dalvik.system.NativeStart.main(Native Method)
07-19 10:47:05.638: E/AndroidRuntime(339): Caused by: java.lang.NullPointerException
07-19 10:47:05.638: E/AndroidRuntime(339):     at android.view.ViewGroup.addViewInner(ViewGroup.java:1969)
07-19 10:47:05.638: E/AndroidRuntime(339):     at android.view.ViewGroup.addView(ViewGroup.java:1865)
07-19 10:47:05.638: E/AndroidRuntime(339):     at android.view.ViewGroup.addView(ViewGroup.java:1845)
07-19 10:47:05.638: E/AndroidRuntime(339):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:217)
07-19 10:47:05.638: E/AndroidRuntime(339):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
07-19 10:47:05.638: E/AndroidRuntime(339):     at android.app.Activity.setContentView(Activity.java:1658)
07-19 10:47:05.638: E/AndroidRuntime(339):     at de.nikita.bangster.MainActivity.onCreate(MainActivity.java:11)
07-19 10:47:05.638: E/AndroidRuntime(339):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-19 10:47:05.638: E/AndroidRuntime(339):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-19 10:47:05.638: E/AndroidRuntime(339):     ... 11 more
```

Währe cool wenn jemand die Lösung wusste

Scheyschen


----------



## theo_retiker (19. Jul 2015)

Ein paar mehr Infos und ein formatierter Code wären schön.

Der Fehlermeldung nach zu urteilen, handelt es sich um eine NullPointerException, sprich du übergibst irgend ein Objekt, was null ist. 
Die restlichen 11 Fehlermeldungen, die hier leider nicht zu sehen sind, bieten vielleicht mehr Infos, wo der Fehler austritt und ob es wirklich eine N.P.Exception ist.

mfg


----------



## Flown (20. Jul 2015)

Könnte es sein, dass du die GameView vorher setzt und dann erst initialisierst? HIER?:

```
setContentView(theGameView);
theGameView = new GameView(this);
```


----------



## RalleYTN (12. Aug 2015)

Flown hat gesagt.:


> Könnte es sein, dass du die GameView vorher setzt und dann erst initialisierst? HIER?:
> 
> ```
> setContentView(theGameView);
> ...


Genau das wird der Fehler sein XD


----------

