0

I have a simple Android app with tabs. I want the second tab to open a google play store URL when clicked.

The tabs load but the app crashes when I click on tab2.

TabSpec spec = m_tabhost.newTabSpec("tab1")
            .setIndicator("tab1")
            .setContent(new Intent().setClass(this, GridActivity.class).putExtra("tabindex", 0));
            m_tabhost.addTab(spec);

   spec = m_tabhost.newTabSpec("tab2")
    .setIndicator("tab2")
    .setContent(new Intent(Intent.ACTION_VIEW).setData(Uri.parse("market://search?q=pub:Google Inc.")));
    m_tabhost.addTab(spec);   

Here's the error:

01-16 18:53:58.734: W/dalvikvm(10611): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
01-16 18:53:58.738: W/System.err(10611): java.lang.SecurityException: Requesting code from com.android.vending (with uid 10060) to be run in process soundboard.code.bhak (with uid 10130)
01-16 18:53:58.757: W/System.err(10611):    at android.app.ActivityThread.getPackageInfo(ActivityThread.java:1296)
01-16 18:53:58.757: W/System.err(10611):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1546)
01-16 18:53:58.757: W/System.err(10611):    at android.app.ActivityThread.startActivityNow(ActivityThread.java:1491)
01-16 18:53:58.757: W/System.err(10611):    at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
01-16 18:53:58.757: W/System.err(10611):    at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
01-16 18:53:58.757: W/System.err(10611):    at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:657)
01-16 18:53:58.757: W/System.err(10611):    at android.widget.TabHost.setCurrentTab(TabHost.java:329)
01-16 18:53:58.757: W/System.err(10611):    at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:133)
01-16 18:53:58.757: W/System.err(10611):    at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:456)
01-16 18:53:58.761: W/System.err(10611):    at android.view.View.performClick(View.java:2538)
01-16 18:53:58.761: W/System.err(10611):    at android.view.View$PerformClick.run(View.java:9152)
01-16 18:53:58.761: W/System.err(10611):    at android.os.Handler.handleCallback(Handler.java:587)
01-16 18:53:58.761: W/System.err(10611):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-16 18:53:58.765: W/System.err(10611):    at android.os.Looper.loop(Looper.java:130)
01-16 18:53:58.765: W/System.err(10611):    at android.app.ActivityThread.main(ActivityThread.java:3687)
01-16 18:53:58.765: W/System.err(10611):    at java.lang.reflect.Method.invokeNative(Native Method)
01-16 18:53:58.765: W/System.err(10611):    at java.lang.reflect.Method.invoke(Method.java:507)
01-16 18:53:58.769: W/System.err(10611):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
01-16 18:53:58.769: W/System.err(10611):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
01-16 18:53:58.769: W/System.err(10611):    at dalvik.system.NativeStart.main(Native Method)
01-16 18:53:58.792: D/BugSenseHandler(10611): Transmitting stack trace: java.lang.SecurityException: Requesting code from com.android.vending (with uid 10060) to be run in process soundboard.code.bhak (with uid 10130)

any suggestions?

DigCamara
  • 5,540
  • 4
  • 36
  • 47

1 Answers1

0

It is exactly as the exception states, you get a SecurityException because you're requesting an external intent to be launched into a container you own. Pretty much your only option to do what you want is load the URL version of that play store url in a WebView that you create locally.

Conversely, if you want it to load the actual play store app you can still do it by sending an intent, but you won't be able to load it into your container... Instead you'd have to make your code actually launch the external Intent for the play store url when the tab is clicked.

EDIT: You may be able to somehow get a refernce to the actual view for the tab you want a click on in order to put an OnClickListener on it by some trickery with TabHost, here is an example of how to do that with the current tab, althought it's not immediately obvious how to get a view for any arbitrary tab. If you really wanted to go that approach you could probably iterate over the child views of the TabHost once you've created it and find the right view.

Conversely you can use the typical TabHost.OnTabChangedListener, which gives you an event when the tab is clicked on. You can put some kind of dummy layout in the tab content(where you're setting the play intent atm) (maybe make it switch somewhere else on resume or something? up to you) ... And then in the OnTabChanged for this tab, launch your above intent. Ie :

m_tabhost.setOnTabChangedListener(new OnTabChangedListener(){

  @Override 
  public void onTabChanged(String tabId){
  if(tabId.equals(SOME_TAB_ID_FOR_THIS_TAB)){
    Intent i = new   Intent(Intent.ACTION_VIEW).setData(Uri.parse("market://search?q=pub:Google Inc."));
    startActivity(i);
    //more stuff if you need
   }}
});
Community
  • 1
  • 1
syklon
  • 187
  • 6
  • 9