I don’t need the code for the creation Dialog box on an activity.
I use opengles and i am drawing with the renderer class i create . I think the execution at the android activity window stays at main activity.
I have states of my drawing and when a draw reach a state i want to post a dialog box. So there is a problem with that because the dialogbox builder wants a context and the renderer class isnt an activity object.
I am new at opengles and firstly all the work i do exists at
method
public void onDrawFrame(GL10 gl)
{ }
so i have 2 classes 1st the ui class
mainactivity extends activity
and second the renderer class
class mainrenderer implements GLSurfaceView.Renderer
i want from the second class to use activity operations such as create dialog box .
Can you give me a solution to this ?
Thanks.
In other words i want from a class (renderer) that isn’t ui class to make a dialogbox .
edited
i pass the context of my activity class
as myrender = new Renderer1(this);
at constructor of Renderer i have
class Renderer1 implements GLSurfaceView.Renderer
/* initializations */
public Renderer(Context context) {
super();
mcontext = context;
}
and after that i have implement the on drawFrame method and when i reach a state i call the method alertdialogbox()
given below
public void alertdialogbox() /* some code */ AlertDialog.Builder
builder = new AlertDialog.Builder(mcontext);
but it keeps erroring and application crashes when reach the state that the alertdialogbox called
the error begins with
java.lang.RuntimeException : Can’t create handler inside thread that
has not called Looper.prepare()
Edit 2 solved
i initialize a handler at main activity as :
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
alertdialogbox();
}
};
alertdialogbox is a method that i declare inside main activity class
and constructs the dialogbox
again inside the main activity where i instantiate the GlsurfaceView and the Glrenderer
i pass the handler that i initialize before so :
Renderer = new Renderer1(handler);
after that at the class Renderer1
class Renderer1 implements GLSurfaceView.Renderer
Handler mhandler;
public Renderer( Handler handler) {
super();
mhandler = handler;
}
*
*
public void onDrawFrame(){
*
*
if (state)
{
alertdialogbox();
}
}
*
*
public void alertdialogbox()
{
mhandler.sendEmptyMessage(1);
}
and finally i have my dialogbox viewed.
thanks for the suggetions .
When creating a
AlertDialog(or any other UI widget) from another class, you must have a reference to your activity class and with that object, you can callactivity.runOnUIThread()to execute any code related to your AlertDialog.