问题描述:

I have created an andorid app using C# and xamarin and I am trying to have a method that when called simply loads the google play store app page for a different app, but I am not having much joy.

Here is what I have so far, but it doesnt work and I think I was maybe going down the wrong route. Any help would be greatly appreciated.

 void BuyGameMenuItemSelected(object sender, PlayerIndexEventArgs e)

{

String appPackageName = "com.innervisionsoftware.visionthegame";

AndroidGameActivity a = new AndroidGameActivity ();

try

{

a.StartActivity(new Intent("ACTION_VIEW", Android.Net.Uri.Parse("market://details?id=" + appPackageName)));

}

catch (Android.Content.ActivityNotFoundException anfe)

{

// a.StartActivity(new Intent("ACTION_VIEW", Android.Net.Uri.Parse("http://play.google.com/store/apps/details?id=" + appPackageName)));

}

}

================================

Edit:

I am still getting the following exception, no idea why.

Java.Lang.NullPointerException:

at at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (intptr,intptr,intptr,Android.Runtime.JValue[]) <IL 0x00085, 0x00318>

at at Android.Content.ContextWrapper.StartActivity (Android.Content.Intent) <IL 0x000a9, 0x003ff>

at Vision_Android.MainMenuScreen.BuyGameMenuItemSelected (object,Vision_Android.PlayerIndexEventArgs) [0x0000e] in c:\Users\calcof\Documents\Visual Studio 2012\Projects\Vision_Android_Lite\Vision\Screens\MainMenuScreen.cs:218

at Vision_Android.MenuItem.OnSelectEntry (Microsoft.Xna.Framework.PlayerIndex) [0x0000e] in c:\Users\calcof\Documents\Visual Studio 2012\Projects\Vision_Android_Lite\Vision\ScreenManager\MenuItem.cs:138

at Vision_Android.MenuItem.Update (Vision_Android.MenuScreen,bool,Microsoft.Xna.Framework.GameTime) [0x0009d] in c:\Users\calcof\Documents\Visual Studio 2012\Projects\Vision_Android_Lite\Vision\ScreenManager\MenuItem.cs:392

at Vision_Android.MenuScreen.Update (Microsoft.Xna.Framework.GameTime,bool,bool) [0x0006a] in c:\Users\calcof\Documents\Visual Studio 2012\Projects\Vision_Android_Lite\Vision\ScreenManager\MenuScreen.cs:251

at Vision_Android.MainMenuScreen.Update (Microsoft.Xna.Framework.GameTime,bool,bool) [0x00001] in c:\Users\calcof\Documents\Visual Studio 2012\Projects\Vision_Android_Lite\Vision\Screens\MainMenuScreen.cs:180

at Vision_Android.ScreenManager.Update (Microsoft.Xna.Framework.GameTime) [0x000a5] in c:\Users\calcof\Documents\Visual Studio 2012\Projects\Vision_Android_Lite\Vision\ScreenManager\ScreenManager.cs:214

at Microsoft.Xna.Framework.Game.<.cctor>b__19 (Microsoft.Xna.Framework.IUpdateable,Microsoft.Xna.Framework.GameTime) [0x00000] in c:\Users\calcof\Documents\GitHub\MonoGame\MonoGame.Framework\Game.cs:596

at at Microsoft.Xna.Framework.Game/SortingFilteringCollection`1<Microsoft.Xna.Framework.IUpdateable>.ForEachFilteredItem<Microsoft.Xna.Framework.GameTime> (System.Action`2<Microsoft.Xna.Framework.IUpdateable, Microsoft.Xna.Framework.GameTime>,Microsoft.Xna.Framework.GameTime) <0x003a7>

at Microsoft.Xna.Framework.Game.Update (Microsoft.Xna.Framework.GameTime) [0x00001] in c:\Users\calcof\Documents\GitHub\MonoGame\MonoGame.Framework\Game.cs:600

at Vision_Android.Game.Update (Microsoft.Xna.Framework.GameTime) [0x00001] in c:\Users\calcof\Documents\Visual Studio 2012\Projects\Vision_Android_Lite\Vision\Game.cs:182

at Microsoft.Xna.Framework.Game.DoUpdate (Microsoft.Xna.Framework.GameTime) [0x00022] in c:\Users\calcof\Documents\GitHub\MonoGame\MonoGame.Framework\Game.cs:690

at Microsoft.Xna.Framework.Game.Tick () [0x00110] in c:\Users\calcof\Documents\GitHub\MonoGame\MonoGame.Framework\Game.cs:500

at Microsoft.Xna.Framework.AndroidGameWindow.OnUpdateFrame (object,OpenTK.FrameEventArgs) [0x00065] in c:\Users\calcof\Documents\GitHub\MonoGame\MonoGame.Framework\Android\AndroidGameWindow.cs:90

at at OpenTK.GameViewBase.OnUpdateFrame (OpenTK.FrameEventArgs) <IL 0x00014, 0x0008f>

at at OpenTK.Platform.Android.AndroidGameView.UpdateFrameInternal (OpenTK.FrameEventArgs) <IL 0x00013, 0x000b3>

at at OpenTK.Platform.Android.AndroidGameView.RunIteration (System.Threading.CancellationToken) <IL 0x00096, 0x003d3>

at at OpenTK.Platform.Android.AndroidGameView/<StartThread>c__AnonStorey0.<>m__0 (object) <IL 0x0000d, 0x00077>

at at Android.App.SyncContext/<Send>c__AnonStorey1.<>m__0 () <IL 0x0000c, 0x0005b>

at at Java.Lang.Thread/RunnableImplementor.Run () <IL 0x00011, 0x00097>

at at Java.Lang.IRunnableInvoker.n_Run (intptr,intptr) <IL 0x0000a, 0x000a3>

at at (wrapper dynamic-method) object.efceb458-16eb-42a8-a5bd-ce69af2b2929 (intptr,intptr) <IL 0x00011, 0x0003b>

at --- End of managed exception stack trace ---

at java.lang.NullPointerException

at at android.app.Activity.startActivityForResult(Activity.java:3388)

at at android.app.Activity.startActivityForResult(Activity.java:3349)

at at android.app.Activity.startActivity(Activity.java:3584)

at at android.app.Activity.startActivity(Activity.java:3552)

at at mono.java.lang.RunnableImplementor.n_run(Native Method)

at at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:29)

at at android.os.Handler.handleCallback(Handler.java:725)

at at android.os.Handler.dispatchMessage(Handler.java:92)

at at android.os.Looper.loop(Looper.java:153)

at at android.app.ActivityThread.main(ActivityThread.java:5299)

at at java.lang.reflect.Method.invokeNative(Native Method)

at at java.lang.reflect.Method.invoke(Method.java:511)

at at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)

at at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)

at at dalvik.system.NativeStart.main(Native Method)

网友答案:

Use Intent.ActionView rather than the string "ACTION_VIEW".

Eg:

a.StartActivity(new Intent(Intent.ActionView, Android.Net.Uri.Parse("market://details?id=" + appPackageName)));
相关阅读:
Top