2

For some reason, I am unable to access the ActivityChooserModel class in my Android activity. I am trying to access it as follows:

ActivityChooserModel dataModel = ActivityChooserModel.get(this, ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);

But when I try to build the project I get the following:

cannot find symbol class ActivityChooserModel

I have attempted using both of the following imports, but neither of them works:

import android.widget.ActivityChooserModel;
import android.support.v7.internal.widget.ActivityChooserModel;

This is especially frustrating because in Android Studio I can open ActivityChooserModel.java and I can see that it is a public class. My activity is using a ShareActionProvider and importing it with no problems, and when I browse through the SDK source I can clearly view both that class and ActivityChooserModel so I have no idea why I'm not able to access it.

Here is a snippet of my build.gradle, although I'm not sure if related:

android {
    compileSdkVersion 22
    buildToolsVersion '22.0.1'

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 22
    }
}

Thanks a lot!

yuval
  • 6,369
  • 3
  • 32
  • 44
  • It compiled for me. Make sure you have `compile "com.android.support:appcompat-v7:22.1.1"` in your Gradle dependencies. – Daniel Nugent May 27 '15 at 00:24
  • @DanielNugent ah thank you, that must be it! Unfortunately I can't add that right now due to some dependency conflicts, but once I do I will confirm whether that fixed the problem or not – yuval May 27 '15 at 01:38

2 Answers2

1

This compiled for me:

import android.support.v7.internal.widget.ActivityChooserModel;
import android.support.v7.widget.ShareActionProvider;


public class MainActivity extends AppCompatActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActivityChooserModel dataModel = ActivityChooserModel.get(this, ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);

    }

build.gradle:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile "com.android.support:appcompat-v7:22.1.1"

}
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
0

The accepted answer worked for me, but I had to include appcompat-v7 in my project in order to do so, and that's a pretty big library. Although very hacky, I discovered an alternative way to do it with reflection:

Class<?> activityChooserModel = Class.forName("android.widget.ActivityChooserModel");
Method activityChooserGetMethod = activityChooserModel.getMethod("get", Context.class, String.class);
Object activityChooserObject = activityChooserGetMethod.invoke(null, this, ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);

The accepted answer is probably better if you don't mind including appcompat-v7 but I just wanted to add this one as well

yuval
  • 6,369
  • 3
  • 32
  • 44