I have a problem with understanding the following events, please help.
I have an application with a menu Activity. In the menu there is a button, that when pressed starts a timer, that when finished tells the menu Activity that it has fininshed. It does that by using an interface I have made. The Activity recives the callback from the button and starts a new Activity via an Intent. The new Activity contains several views and one of them being an AdView. This used to run just fine before, but…
Now I have made some changes to the layout and I need to know when the AdView changes its size. To do this I added an OnLayoutChangeListener to the AdView, and here’s the problem. When the new Activity containing the AdView starts I recive a NoClassDefFoundError regarding the Activity class I tried to create.
I have also noticed that the error happens even if I actually don’t add the listener to the AdView, it seems to occur only because the Activity implements OnLayoutChangeListener.
If it is any help, the Activity also implements Runnable and OnClickListener, and also uses multiple Threads. Now, I don’t know why any of that would be important, but there it is.
And just for the record, I have both restarted Eclipse and cleaned the project.
Please help anyone, I realy don’t understand this.
Edit
Starts the Game activity:
public void onSlideEnd(View v) {
if (v == game){
Intent i = new Intent();
i.setClass(this, Game.class);
startActivity(i);
finish();
}
}
OnCreate method of the Game activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_game);
setRequestedOrientation(1);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
layoutBase = (LinearLayout) findViewById(R.id.layoutBase);
Drawable d = getResources().getDrawable(R.drawable.wood);
layoutBase.setBackgroundDrawable(d);
adView = (AdView) findViewById(R.id.adViewGame);
adView.addOnLayoutChangeListener(this);
layoutTop = (LinearLayout) findViewById(R.id.layoutTop);
brickBox = new BrickBox(this);
layoutBrick = (LinearLayout) findViewById(R.id.layoutBox);
layoutBrick.addView(brickBox);
brickBox.addOnLayoutChangeListener(this);
goal = new GoalBox(this);
LinearLayout layoutGoal = (LinearLayout) findViewById(R.id.layoutGoal);
layoutGoal.addView(goal);
goal.addOnLayoutChangeListener(this);
restart = (Button) findViewById(R.id.buttonRestart);
restart.setOnClickListener(this);
start = (Button) findViewById(R.id.buttonStart);
start.setOnClickListener(this);
menu = (Button) findViewById(R.id.buttonMenu);
menu.setOnClickListener(this);
TextView time = (TextView) findViewById(R.id.textViewTime);
TextView click = (TextView) findViewById(R.id.textViewClick);
scoreBoard = new ScoreBoard(this, time, click);
soundManager = new SoundManager(this);
resizeLayouts();
}
Crash code:
01-01 13:27:05.537: E/AndroidRuntime(3781): FATAL EXCEPTION: Thread-9
01-01 13:27:05.537: E/AndroidRuntime(3781): java.lang.NoClassDefFoundError: com.martin.colorPuzzleFree.Game
01-01 13:27:05.537: E/AndroidRuntime(3781): at com.martin.colorPuzzleFree.Menu.onSlideEnd(Menu.java:65)
01-01 13:27:05.537: E/AndroidRuntime(3781): at com.martin.colorPuzzleFree.BrickButton.allertListenersEnd(BrickButton.java:258)
01-01 13:27:05.537: E/AndroidRuntime(3781): at com.martin.colorPuzzleFree.BrickButton.run(BrickButton.java:243)
01-01 13:27:05.537: E/AndroidRuntime(3781): at java.lang.Thread.run(Thread.java:1102)
I also noticed that this is shown in the Log upon application start:
01-01 13:27:03.247: W/dalvikvm(3781): Link of class 'Lcom/martin/colorPuzzleFree/Game;' failed
01-01 13:27:03.257: E/dalvikvm(3781): Could not find class 'com.martin.colorPuzzleFree.Game', referenced from method com.martin.colorPuzzleFree.Menu.onSlideEnd
01-01 13:27:03.267: W/dalvikvm(3781): VFY: unable to resolve const-class 162 (Lcom/martin/colorPuzzleFree/Game;) in Lcom/martin/colorPuzzleFree/Menu;
I have found the answer myself. It was quite obvious actually, the phone I am testing on is running Android 2.2, but OnLayoutChangeListener was not implemented until Android 3.0. Eclipse didn’t see the problem because the AdView requires the app to target Android 3.2, which included OnLayoutChangeListener, but when the Activity was started on my phone it didn’t know what to do with the interface and crashed. I tested the app on an emulator running Android 3.2 and it worked.
Conclusion, check your version!