Fehler in Android Spiel Startet nicht bzw nur weißer Bildschirm

  • Antworten:31
  • Bentwortet
André
  • Forum-Beiträge: 53

27.02.2014, 22:15:56 via Website

Android Game App Entwicklung Tutorial – Little Dragons von Panjutorials

Habe die Tutorials 1-10 gemacht.

Irgendwo muss ein Fehler sein den ich nicht finde..Spiel startet nicht und bekomme nur einen weißen Bildschirm..8o

Wenn jemand helfen kann wäre nett..

Quellcodes stehen hier drunter als Antwort...:wacko:

— geändert am 28.02.2014, 01:46:09

Antworten
André
  • Forum-Beiträge: 53

27.02.2014, 22:30:42 via Website

Falls irgendjemand lust hat das durchzuschauen oder wenn jemand das Tutorial selber gemacht hat und mir die Dateien geben kann gerne melden :sleep:

GameActivity Quellcode:

1package com.panjutorials.lazypudding;
2
3import android.app.Activity;
4import android.os.Bundle;
5
6public class GameActivity extends Activity {
7 /** Called when the activity is first created. */
8 @Override
9 public void onCreate(Bundle savedInstanceState) {
10 super.onCreate(savedInstanceState);
11 setContentView(new GameView(this));
12 }
13}

GameView Quellcode:

1package com.panjutorials.lazypudding;
2
3import java.util.ArrayList;
4import java.util.List;
5import java.util.Random;
6
7import android.annotation.SuppressLint;
8import android.content.Context;
9import android.graphics.Bitmap;
10import android.graphics.BitmapFactory;
11import android.graphics.Canvas;
12import android.graphics.Color;
13import android.view.SurfaceHolder;
14import android.view.SurfaceView;
15
16public class GameView extends SurfaceView {
17 private List<Sprite> spriteList = new ArrayList<Sprite>();
18 private List<Integer> spriteListNum = new ArrayList<Integer>();
19 private SurfaceHolder surfaceHolder;
20 private Bitmap bmp;
21 private GameLoopThread theGameLoopThread;
22 private boolean createSprites=true;
23
24 public GameView(Context context) {
25 super(context);
26 theGameLoopThread = new GameLoopThread(this);
27 surfaceHolder = getHolder();
28 surfaceHolder.addCallback(new SurfaceHolder.Callback() {
29
30 public void surfaceDestroyed(SurfaceHolder holder) {
31 boolean retry = true;
32 theGameLoopThread.setRunning(false);
33 while (retry) {
34 try {
35 theGameLoopThread.join();
36 retry = false;
37 } catch (InterruptedException e) {
38
39 }
40 }
41
42 }
43
44 public void surfaceCreated(SurfaceHolder holder) {
45 theGameLoopThread.setRunning(true);
46 theGameLoopThread.start();
47 }
48
49 public void surfaceChanged(SurfaceHolder holder, int format,
50 int width, int height) {
51 // TODO Auto-generated method stub
52
53 }
54 });
55 }
56
57 @SuppressLint("WrongCall") @Override
58 protected void onDraw(Canvas canvas) {
59 canvas.drawColor(Color.DKGRAY);
60 if(createSprites==true){
61 initialSprites();
62 }
63 for (Sprite sprite : spriteList) {
64 sprite.onDraw(canvas);
65 }
66 }
67
68 private void createSprite(int index) {
69 Bitmap bmp = null;
70 switch (index) {
71 case 0:
72 bmp = BitmapFactory.decodeResource(getResources(),
73 R.drawable.alienspriteblue);
74 break;
75 case 1:
76 bmp = BitmapFactory.decodeResource(getResources(),
77 R.drawable.alienspritered);
78 break;
79 case 2:
80 bmp = BitmapFactory.decodeResource(getResources(),
81 R.drawable.alienspritegreen);
82 break;
83 case 3:
84 bmp = BitmapFactory.decodeResource(getResources(),
85 R.drawable.alienspriteyellow);
86 break;
87 }
88 Sprite sprite = new Sprite(this, bmp);
89 spriteList.add(sprite);
90 spriteListNum.add(index);
91 }
92 private void initialSprites() {
93 for (int i = 0; i < 4; i++) {
94 for (int j = 0; j < 3; j++)
95 createSprite(i);
96 }
97 createSprites=false;
98 }
99 private void rndCreateSprite() {
100 Random rnd = new Random(System.currentTimeMillis());
101 int i = rnd.nextInt(4);
102 createSprite(i);
103 }
104}

GameLoopThread Quellcode:

1package com.panjutorials.lazypudding;
2
3import com.panjutorials.lazypudding.GameView;
4
5import android.annotation.SuppressLint;
6import android.graphics.Canvas;
7
8public class GameLoopThread extends Thread {
9 static final long FPS = 20;
10 private GameView theView;
11 private boolean isRunning = false;
12
13 public GameLoopThread(GameView theView) {
14 this.theView = theView;
15 }
16
17 public void setRunning(boolean run) {
18 isRunning = run;
19 }
20// @SuppressLint("WrongCall") hinzugefügt am 20.02.14
21 @SuppressLint("WrongCall") @Override
22
23 public void run() {
24 long TPS = 1000 / FPS;
25 long startTime, sleepTime;
26 while (isRunning) {
27 Canvas theCanvas = null;
28 startTime = System.currentTimeMillis();
29 try {
30 theCanvas = theView.getHolder().lockCanvas();
31 synchronized (theView.getHolder()) {
32 theView.draw(theCanvas);
33 }
34 } finally {
35 if (theCanvas != null) {
36 theView.getHolder().unlockCanvasAndPost(theCanvas);
37 }
38 }
39 sleepTime = TPS - (System.currentTimeMillis() - startTime);
40 try {
41 if (sleepTime > 0)
42 sleep(sleepTime);
43 else
44 sleep(10); }
45 catch (Exception e) { }
46 }
47 }
48
49}

Sprite Quellcode:

1package com.panjutorials.lazypudding;
2
3import java.util.Random;
4
5import android.annotation.SuppressLint;
6import android.graphics.Bitmap;
7import android.graphics.Canvas;
8import android.graphics.Rect;
9
10@SuppressLint("DrawAllocation")
11 public class Sprite {
12 static final private int BMP_ROWS = 4;
13 static final private int BMP_COLUMNS = 4;
14 private int[] DIRECTION_TO_SPRITE_SHEET = { 2, 0, 3, 1 };
15 private int x;
16 private int y;
17 private int xSpeed;
18 private int ySpeed;
19 private int width;
20 private int height;
21 private Bitmap bmp;
22 private GameView theGameView;
23 private int currentFrame=0;
24
25
26 public Sprite(GameView theGameView, Bitmap bmp){
27 this.theGameView = theGameView;
28 this.bmp = bmp;
29 this.width = bmp.getWidth() / BMP_COLUMNS;
30 this.height = bmp.getHeight() / BMP_ROWS;
31 Random rnd = new Random();
32 x=rnd.nextInt(theGameView.getWidth()- width);
33 y=rnd.nextInt(theGameView.getHeight() - height);
34 ySpeed = rnd.nextInt(10) - 4;
35 xSpeed = rnd.nextInt(10) - 4;
36 }
37
38 private void bounceOff() {
39 if (x > theGameView.getWidth() - width - xSpeed || x + xSpeed < 0) {
40 xSpeed = -xSpeed;
41 }
42 x = x + xSpeed;
43 if (y > theGameView.getHeight() - height - ySpeed || y + ySpeed < 0) {
44 ySpeed = -ySpeed;
45 }
46 y = y + ySpeed;
47 currentFrame = ++currentFrame % BMP_COLUMNS;
48 }
49 @SuppressLint("DrawAllocation")
50 public void onDraw(Canvas canvas){
51 bounceOff();
52 int sourceX = currentFrame * width;
53 int sourceY = getAnimationRow() * height;
54 Rect source = new Rect(sourceX, sourceY, sourceX + width, sourceY + height);
55 Rect destine = new Rect(x, y, x + width, y + height);
56 canvas.drawBitmap(bmp, source, destine, null);
57
58 }
59
60 private int getAnimationRow() {
61 double directionDouble = (Math.atan2(xSpeed, ySpeed) / (Math.PI / 2)+2);
62 int spriteDir = (int) Math.round(directionDouble) % BMP_ROWS;
63 return DIRECTION_TO_SPRITE_SHEET[spriteDir];
64 }
65 }

— geändert am 28.02.2014, 01:44:21

Antworten
André
  • Forum-Beiträge: 53

28.02.2014, 13:36:31 via Website

Emulator:



LogCat:

102-28 12:25:35.532: D/AndroidRuntime(333): Shutting down VM
202-28 12:25:35.532: W/dalvikvm(333): threadid=1: thread exiting with uncaught exception (group=0x40015560)
302-28 12:25:35.612: E/AndroidRuntime(333): FATAL EXCEPTION: main
402-28 12:25:35.612: E/AndroidRuntime(333): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.panjutorials.lazypudding/com.panjutorials.lazypudding.MainActivity}: java.lang.ClassNotFoundException: com.panjutorials.lazypudding.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.panjutorials.lazypudding-2.apk]
502-28 12:25:35.612: E/AndroidRuntime(333): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
602-28 12:25:35.612: E/AndroidRuntime(333): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
702-28 12:25:35.612: E/AndroidRuntime(333): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
802-28 12:25:35.612: E/AndroidRuntime(333): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
902-28 12:25:35.612: E/AndroidRuntime(333): at android.os.Handler.dispatchMessage(Handler.java:99)
1002-28 12:25:35.612: E/AndroidRuntime(333): at android.os.Looper.loop(Looper.java:123)
1102-28 12:25:35.612: E/AndroidRuntime(333): at android.app.ActivityThread.main(ActivityThread.java:3683)
1202-28 12:25:35.612: E/AndroidRuntime(333): at java.lang.reflect.Method.invokeNative(Native Method)
1302-28 12:25:35.612: E/AndroidRuntime(333): at java.lang.reflect.Method.invoke(Method.java:507)
1402-28 12:25:35.612: E/AndroidRuntime(333): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
1502-28 12:25:35.612: E/AndroidRuntime(333): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
1602-28 12:25:35.612: E/AndroidRuntime(333): at dalvik.system.NativeStart.main(Native Method)
1702-28 12:25:35.612: E/AndroidRuntime(333): Caused by: java.lang.ClassNotFoundException: com.panjutorials.lazypudding.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.panjutorials.lazypudding-2.apk]
1802-28 12:25:35.612: E/AndroidRuntime(333): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
1902-28 12:25:35.612: E/AndroidRuntime(333): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
2002-28 12:25:35.612: E/AndroidRuntime(333): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
2102-28 12:25:35.612: E/AndroidRuntime(333): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
2202-28 12:25:35.612: E/AndroidRuntime(333): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
2302-28 12:25:35.612: E/AndroidRuntime(333): ... 11 more

hilft das?

— geändert am 28.02.2014, 14:03:11

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

28.02.2014, 13:38:13 via Website

LogCat kann man auch rauskopieren, dann kann man es erst lesen.

— geändert am 28.02.2014, 13:38:34

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

Antworten
André
  • Forum-Beiträge: 53

28.02.2014, 13:41:28 via Website

hab ich jetzt gemacht.)

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

28.02.2014, 13:46:45 via Website

thread exiting with uncaught exception //Schon mal schlecht, wenn die Exception nicht abgefangen wird

Und deine Main startet nicht weil irgendeine Klasse nicht gefunden wird. //Class not found Exception
irgendeine Lib oder externe Recourcen drinne?

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

Antworten
André
  • Forum-Beiträge: 53

28.02.2014, 13:52:31 via Website

ist eigentlich nichts externes drin..

— geändert am 28.02.2014, 13:55:22

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

28.02.2014, 13:54:17 via Website

Wieso immer bilder.
Den code immer Direkt in ["code](ohne") Tags posten, dann kann man es lesen

— geändert am 28.02.2014, 13:54:34

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

Antworten
André
  • Forum-Beiträge: 53

28.02.2014, 13:58:40 via Website

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

da sagt er The method rndCreateSprite() from the type GameView is never used locally

1private Bitmap bmp;

und da: The value of the field GameView.bmp is not used

res hab ich nur 4 Bilder eingefügt..

— geändert am 28.02.2014, 14:04:24

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

28.02.2014, 14:07:40 via Website

Das sind aber nur Warnungen und keine Errors oder?
"Is never use locally" meint, dass diese Funktion intern nie Aufgerufen wird.
Und das zweite meint, dass die Variable bmp nie genutzt wird.
Wo jetzt die Class not Foun exception entsteht ist mir unbekannt.
PS: ich sah, dass die MainActivity nicht gefunden wird, hast du überhaupt eine oder hast du die nachträglich umbenannt?

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

Antworten
André
  • Forum-Beiträge: 53

28.02.2014, 14:11:02 via Website

Ja sind nur Warnungen..

wenn du das Bild von ebend meinst das war ein anderes Projekt

1package com.panjutorials.lazypudding;
2
3import android.app.Activity;
4import android.os.Bundle;
5
6public class GameActivity extends Activity {
7 /** Called when the activity is first created. */
8 @Override
9 public void onCreate(Bundle savedInstanceState) {
10 super.onCreate(savedInstanceState);
11 setContentView(new GameView(this));
12 }
13}

das ist bei mir die "GameActivity"

— geändert am 28.02.2014, 14:12:34

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

28.02.2014, 14:15:27 via Website

War das mal eine Mainactivity und du hast diese umbenannt?
Hat diese App schon mal funktioniert?

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

Antworten
André
  • Forum-Beiträge: 53

28.02.2014, 14:18:31 via Website

Nein hat sie nicht

Ich weiß es grade nicht mehr. eine Mainactivity gibt es bei mir aufjedenfall nicht..

Bin am überlegen ob ich das Tutorial neu anfange..

— geändert am 28.02.2014, 14:20:37

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

28.02.2014, 14:24:59 via Website

Hast du nur Copy und paste gemacht oder auch den Code verstanden.
Normalerweise testet man die App zwischendurch einmal

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

Antworten
André
  • Forum-Beiträge: 53

28.02.2014, 14:26:43 via Website

Ich hab mir alle Videos angeguckt in denen er den Code erklärt hab auch eigentlich alles gemacht wie er es gemacht hat nur irgendwie klappt das bei mir nicht..

Antworten
André
  • Forum-Beiträge: 53

28.02.2014, 14:33:39 via Website

Ich beginn mit dem Tutorial nochmal von Anfang an..Jetzt im nachhinein weiß ich nicht mehr was das alles genau war,,

und den Fehler finde ich auch nicht

— geändert am 28.02.2014, 14:34:04

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

28.02.2014, 14:35:34 via Website

Das bringt doch nix. Als Programmierer ist man meist länger mit der Fehlerbehebung beschäftigt, als mit der app selbst.

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

Antworten
André
  • Forum-Beiträge: 53

28.02.2014, 14:36:19 via Website

Aber was soll ich denn noch machen?

Ich versteh einfach nicht warum die nicht startet..

Oder was ich machen könnte wenn da echt keine MainActivity ist..
Oder warum die Exception nicht abgefangen wird
Oder Warum die eine Class nicht gefunden wird wie du schon sagst..

— geändert am 28.02.2014, 14:38:59

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

28.02.2014, 14:37:41 via Website

Erst mal nen paar tage Fehler suchen oder neu anfangen.
Aber was machst du wenn der Fehler nach dem neu machen wieder ist?

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

Antworten
André
  • Forum-Beiträge: 53

28.02.2014, 14:40:08 via Website

Vielleicht hab ich irgendwas, was er im Tutorial macht ja doch übersehen dann wäre es ja kein wunder wieso die App nicht funktioniert..

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

28.02.2014, 14:40:56 via Website

Das kann natürlich sein, aber dafür musst du das tut noch mal anschauen

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

Antworten
André
  • Forum-Beiträge: 53

28.02.2014, 14:45:48 via Website

Was würdest du denn sagen was ich jetz am besten machen soll..? kenn mich mit Java einfach noch nicht so gut aus

Antworten
André
  • Forum-Beiträge: 53

28.02.2014, 15:01:31 via Website

Ok naja danke dir für die hilfe erstmal vielleicht bekomm ichs ja noch irgendwie hin ansonsten guck ich mir das tutorial nochmal komplett an und Java guck ich mir auch erstmal weiter online an vllt später auch noch mit Buch :)

Antworten
André
  • Forum-Beiträge: 53

28.02.2014, 15:31:37 via Website

Habe jetzt wieder rausgefunden das die GameActivity die MainActivity ist :)

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

28.02.2014, 15:50:28 via Website

Dann wird das von Eclipse nicht erkannt, ist diese umbenannt oder vom projekt aus so benannt worden?

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

Antworten
André
  • Forum-Beiträge: 53

28.02.2014, 16:07:30 via Website

Als ich das Android Prjojekt erstellt habe, hab ich die MainActivity einfach umbenannt

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

28.02.2014, 16:18:03 via Website

Das wird das Problem sein, dass Eclipse nicht weiss, deas die neuenActivity die Main ist.
Um diesen Fehler zu beheben musst du die Manifest XML ändern.
Poste diese mal bitte her.

— geändert am 28.02.2014, 16:18:13

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

Antworten
André
  • Forum-Beiträge: 53

28.02.2014, 16:27:21 via Website

1<?xml version="1.0" encoding="utf-8"?>
2<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3 package="com.panjutorials.lazypudding"
4 android:versionCode="1"
5 android:versionName="1.0" >
6
7 <uses-sdk
8 android:minSdkVersion="8"
9 android:targetSdkVersion="17" />
10
11 <application
12 android:allowBackup="true"
13 android:icon="@drawable/ic_launcher"
14 android:label="@string/app_name"
15 android:theme="@style/AppTheme" >
16 <activity
17 android:name="com.panjutorials.lazypudding.MainActivity"
18 android:label="@string/app_name" >
19 <intent-filter>
20 <action android:name="android.intent.action.MAIN" />
21
22 <category android:name="android.intent.category.LAUNCHER" />
23 </intent-filter>
24 </activity>
25 </application>
26
27</manifest>

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

28.02.2014, 16:56:45 via Website

1android:name="com.panjutorials.lazypudding.MainActivity"
2
3
4ändern in
5
6 android:name="com.panjutorials.lazypudding.GameActivity"

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

Antworten
André
  • Forum-Beiträge: 53

28.02.2014, 17:25:05 via Website

Danke dir, klappt jetzt:)

im anderen Projekt hab ich den Fehler wie auch viele andere:

1@Override
2 protected void onDraw(Canvas canvas) {
3 canvas.drawColor(Color.DKGRAY);
4 theSprite.onDraw(canvas);
5 theSprite2.onDraw(canvas);
6 }

Da sagt er immer statt
1theSprite.onDraw
soll ich nur
1theSprite.draw
nehmen der Fehler bleibt aber was kann man da tun?

hab jetzt einfach @SuppressLint("WrongCall") eingefügt funktioniert alles-_-

— geändert am 28.02.2014, 17:50:51

Antworten