# Fehlermeldung beim ändern der ViewGroup.



## AaronMaster (17. Sep 2014)

Hallo,
ich habe folgendes problem: Wenn ich meine app starte wird zuerst ein Fragment geöffnet. Dieses wird dann bei einem Click entfernt und die eigentliche Activity wird gestartet. Nun bekomme ich aber flgende Fehlermeldung im LogCat:

09-17 19:37:55.222  11131-11131/tapthefrog.graphitentertainment.de.tapthefrog E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at tapthefrog.graphitentertainment.de.tapthefrog.MyActivity.initRound(MyActivity.java:54)
            at tapthefrog.graphitentertainment.de.tapthefrog.MyActivity.newGame(MyActivity.java:36)
            at tapthefrog.graphitentertainment.de.tapthefrog.MyActivity.startGame(MyActivity.java:99)
            at tapthefrog.graphitentertainment.de.tapthefrog.MyActivity.onClick(MyActivity.java:89)
            at android.view.View.performClick(View.java:4091)
            at android.view.View$PerformClick.run(View.java:17072)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:153)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
            at dalvik.system.NativeStart.main(Native Method)


----------



## JavaMeister (17. Sep 2014)

In der Zeile MyActivity.java:54 wird ein Objekt aufgerufen, das jedoch nicht initialisiert worden ist.


----------



## AaronMaster (18. Sep 2014)

Hier der Java Code. Vielleicht kann mir jemend sagen was ich initialisieren soll:


```
public class MyActivity extends Activity implements View.OnClickListener{

    private int points;
    private int round;
    private int countdown;
    private ImageView frog;
    private Random rnd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
            showStartFragment();
    }


    private void newGame(){
        points =0;
        round = 1;
        initRound();
    }

    private void initRound() {
        countdown=10;
        ViewGroup container = (ViewGroup)findViewById(R.id.container);
        container.removeAllViews();
        WimmelView wv = new WimmelView(this);
        container.addView(wv, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        wv.setImageCount(8 * (10 + round));
        frog = new ImageView(this);

        frog.setImageResource(R.drawable.frog);
        frog.setScaleType(ImageView.ScaleType.CENTER);
        float scale = getResources().getDisplayMetrics().density;
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(Math.round(64*scale),Math.round(61*scale));
        lp.gravity = Gravity.TOP + Gravity.LEFT;
        lp.leftMargin = rnd.nextInt(container.getWidth()-64);
        lp.topMargin = rnd.nextInt(container.getHeight()-61);
        frog.setOnClickListener(this);
        container.addView(frog, lp);

        frog.setOnClickListener(this);
        container.addView(frog, lp);
        update();
    }

    private void fillTextView(int id, String text){
        TextView tv =(TextView)findViewById(id);
        tv.setText(text);
    }

    private void update(){
        fillTextView(R.id.points, Integer.toString(points));
        fillTextView(R.id.round, Integer.toString(round));
        fillTextView(R.id.countdown, Integer.toString(countdown*1000));
    }

    private void showStartFragment(){
        ViewGroup container = (ViewGroup)findViewById(R.id.container);
        container.removeAllViews();
        container.addView(getLayoutInflater().inflate(R.layout.fragment_start, null));
        container.findViewById(R.id.start).setOnClickListener(this);
    }

    private void showGameOverFragment(){
        ViewGroup container = (ViewGroup)findViewById(R.id.container);
        container.addView(getLayoutInflater().inflate(R.layout.fragment_gameover, null));
        container.findViewById(R.id.play_again).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.start){
            startGame();
        }
        else if(v.getId() == R.id.play_again) {
            showStartFragment();
        }
    }

    private void startGame(){

        newGame();
    }
```


----------



## dzim (18. Sep 2014)

Da dem Code dein Package und Import fehlt, wissen wir nicht, was jetzt die Zeile 54 ist..... Sollen wir raten???

Ich *vermute*, dass du die #initRound()-Methode irgendwo aufrufst, *bevor* die Activity fertig initilaisiert wurde, aber das ist nur eine Vermutung... Vielleicht geht es ja hier schief: 
	
	
	
	





```
float scale = getResources().getDisplayMetrics().density;
```

BTW: 
	
	
	
	





```
lp.gravity = Gravity.TOP + Gravity.LEFT;
```
 müsste doch IMHO 
	
	
	
	





```
lp.gravity = Gravity.TOP & Gravity.LEFT;
```
 sein...


----------



## AaronMaster (18. Sep 2014)

Okey hier nochmal Fehlermeldung und die Ganze Java Klasse:

09-18 20:16:56.460    9489-9489/tapthefrog.graphitentertainment.de.tapthefrog E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at tapthefrog.graphitentertainment.de.tapthefrog.MyActivity.initRound(MyActivity.java:51)
            at tapthefrog.graphitentertainment.de.tapthefrog.MyActivity.newGame(MyActivity.java:34)
            at tapthefrog.graphitentertainment.de.tapthefrog.MyActivity.startGame(MyActivity.java:95)
            at tapthefrog.graphitentertainment.de.tapthefrog.MyActivity.onClick(MyActivity.java:86)
            at android.view.View.performClick(View.java:4091)
            at android.view.View$PerformClick.run(View.java:17072)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:153)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
            at dalvik.system.NativeStart.main(Native Method)



```
package tapthefrog.graphitentertainment.de.tapthefrog;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.Random;


public class MyActivity extends Activity implements View.OnClickListener{

    private int points;
    private int round;
    private int countdown;
    private ImageView frog;
    private Random rnd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
            showStartFragment();
    }


    private void newGame(){
        points =0;
        round = 1;
        initRound();
    }

    private void initRound() {
        countdown=10;
        ViewGroup container = (ViewGroup)findViewById(R.id.container);
        container.removeAllViews();
        WimmelView wv = new WimmelView(this);
        container.addView(wv, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        wv.setImageCount(8 * (10 + round));
        frog = new ImageView(this);

        frog.setImageResource(R.drawable.frog);
        frog.setScaleType(ImageView.ScaleType.CENTER);
        float scale = getResources().getDisplayMetrics().density;
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(Math.round(64*scale),Math.round(61*scale));
        lp.gravity = Gravity.TOP & Gravity.LEFT;
        lp.leftMargin = rnd.nextInt(container.getWidth()-64);
        lp.topMargin = rnd.nextInt(container.getHeight()-61);
        container.addView(frog, lp);
        frog.setOnClickListener(this);
        container.addView(frog, lp);
        update();
    }

    private void fillTextView(int id, String text){
        TextView tv =(TextView)findViewById(id);
        tv.setText(text);
    }

    private void update(){
        fillTextView(R.id.points, Integer.toString(points));
        fillTextView(R.id.round, Integer.toString(round));
        fillTextView(R.id.countdown, Integer.toString(countdown*1000));
    }

    private void showStartFragment(){
        ViewGroup container = (ViewGroup)findViewById(R.id.container);
        container.removeAllViews();
        container.addView(getLayoutInflater().inflate(R.layout.fragment_start, null));
        container.findViewById(R.id.start).setOnClickListener(this);
    }

    private void showGameOverFragment(){
        ViewGroup container = (ViewGroup)findViewById(R.id.container);
        container.addView(getLayoutInflater().inflate(R.layout.fragment_gameover, null));
        container.findViewById(R.id.play_again).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.start){
            startGame();
        }
        else if(v.getId() == R.id.play_again) {
            showStartFragment();
        }
    }

    private void startGame(){

        newGame();
    }



}
```


----------



## JavaMeister (18. Sep 2014)

rnd ist null.


----------



## AaronMaster (18. Sep 2014)

Vielen Dank


----------

