Answer the question
In order to leave comments, you need to log in
Android: Activity::onCreate() crashes every 2nd run on setContentView()?
I'm learning programming for Android, and in the process of writing a test program I came across an incomprehensible problem. If you run the program from ADT, then everything works great, but if you directly click on the shortcut on the device, then every second launch will crash with an error.
public class MainActivity extends Activity implements SensorEventListener
{
private static MainActivity instance;
public static MainActivity getInstance(){ return instance; }
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
instance = this;
setContentView(GameMainView.getInstance());
}
...
}
public class GameMainView extends SurfaceView implements SurfaceHolder.Callback, OnTouchListener
{
private static GameMainView instance;
public static GameMainView getInstance()
{
if(instance==null) instance = new GameMainView(MainActivity.getInstance());
return instance;
};
private GameMainView(Context context)
{
super(context);
SHolder = getHolder();
SHolder.addCallback(this);
setOnTouchListener(this);
}
...
}
Answer the question
In order to leave comments, you need to log in
If you run the program from ADT, then everything works great, but if you directly click on the shortcut on the device, then every second launch will crash with an error.
Yes, onepavel is right, the problem was indeed static. For some reason, static variables are saved in memory after the application is restarted. Perhaps dr_yand You are right and I really did not take into account something when completing the application, but that's another question. Thanks for answers.
static context is the worst of evils. A static context is not only an activity and, in fact, the context itself, but also a view and everything else that refers to the context.
android-developers.blogspot.co.il/2009/01/avoiding...
For some reason, static variables are saved in memory after the application is restarted.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question