Log in or register to post comments

How can i call 'Toast.makeText()' ?

June 2, 2011 - 2:07am #1

I override QCAR (Thanks, Kim~).
I Call Toast.makeText(). Application close.

my QCAR Override java source(.jar file) is

public class OverrideExample extends com.qualcomm.QCARUnityPlayer.PlayerActivity
{

//public static Activity currentActivity;
public static OverrideExample currentActivity;

protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
currentActivity = this;
}

public void toastMsg()
{
Context context = getApplicationContext();
String text = "Call by Unity";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}

and, My Unity C# code is

AndroidJavaClass jc = new AndroidJavaClass("com.QCAR.CAPTEST.OverrideExample");
AndroidJavaObject jo = jc.GetStatic("currentActivity");
jo.Call("toastMsg");

and, My AndroidManifest.xml

android:label="@string/app_name"
android:configChanges="keyboardHidden|orientation">





...

what problum?

Help~me~~

Re: How can i call 'Toast.makeText()' ?

June 3, 2011 - 7:56am #4

Ah, it looks like the Unity script is running in a different thread than the Android UI thread. You can only spawn a toast from the UI thread. You'll want to set up a Handler object on the main UI thread to take care of this. There's some sample code in this post (not Unity-specific, but the general idea holds):

http://ar.qualcomm.at/node/2000032

- Kim

DDMS Log

June 2, 2011 - 5:54pm #3

Thanks Kim~

DDMS ERROR log is

06-03 09:47:46.148: ERROR/AndroidRuntime(4729): FATAL EXCEPTION: GLThread 13
06-03 09:47:46.148: ERROR/AndroidRuntime(4729): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
06-03 09:47:46.148: ERROR/AndroidRuntime(4729): at android.os.Handler.(Handler.java:121)
06-03 09:47:46.148: ERROR/AndroidRuntime(4729): at android.widget.Toast.(Toast.java:68)
06-03 09:47:46.148: ERROR/AndroidRuntime(4729): at android.widget.Toast.makeText(Toast.java:231)
06-03 09:47:46.148: ERROR/AndroidRuntime(4729): at com.QCAR.CAPTEST.OverrideExample.toastMsg(OverrideExample.java:62) -> toast.show();
06-03 09:47:46.148: ERROR/AndroidRuntime(4729): at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
06-03 09:47:46.148: ERROR/AndroidRuntime(4729): at com.unity3d.player.UnityPlayer.onDrawFrame(Unknown Source)
06-03 09:47:46.148: ERROR/AndroidRuntime(4729): at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1388)
06-03 09:47:46.148: ERROR/AndroidRuntime(4729): at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1138)
06-03 09:47:46.152: ERROR/SensorManager(4729): unregisterListener:: all sensors, listener = com.qualcomm.QCARUnityPlayer.QCARUnityPlayer@405488c8
06-03 09:47:46.167: ERROR/libEGL(4729): call to OpenGL ES API with no current context (logged once per thread)
06-03 09:47:46.167: ERROR/(134): Dumpstate > /data/log/dumpstate_app_com_QCAR_CAPTEST_error
06-03 09:47:54.894: ERROR/(258): onResume() check 0
06-03 09:47:54.894: ERROR/(258): onResume() check 1
06-03 09:47:54.898: ERROR/Launcher(258): setWindowOpaque()

Re: How can i call 'Toast.makeText()' ?

June 2, 2011 - 11:43am #2

It looks correct to me, what error messages are you seeing in the logs? Go to the DDMS perspective in Eclipse to see the running log.

- Kim

Log in or register to post comments