java - 意外的顶级异常

标签 java android

我在 Android Studio 中编译此应用程序时遇到问题。在 Eclipse 中我没有任何问题。我不知道是什么问题。我对 getActionBar() 方法的调用似乎存在问题。错误表明调用需要 API 14(当前最小值为 8)。我知道我下载了 SDK 的所有更新。我迷路了请帮忙。谢谢大家。

public class HomeActivity extends FragmentActivity implements ActionBar.TabListener {

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide fragments for each of the
     * three primary sections of the app. We use a {@link android.support.v4.app.FragmentPagerAdapter}
     * derivative, which will keep every loaded fragment in memory. If this becomes too memory
     * intensive, it may be best to switch to a {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
    AppSectionsPagerAdapter mAppSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will display the three primary sections of the app, one at a
     * time.
     */
    ViewPager mViewPager;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);


        // Create the adapter that will return a fragment for each of the three primary sections
        // of the app.
        mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());

        // Set up the action bar.
        final ActionBar actionBar = getActionBar();

        // Specify that the Home/Up button should not be enabled, since there is no hierarchical
        // parent.
        actionBar.setHomeButtonEnabled(false);

        // Specify that we will be displaying tabs in the action bar.
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Set up the ViewPager, attaching the adapter and setting up a listener for when the
        // user swipes between sections.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mAppSectionsPagerAdapter);
        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                // When swiping between different app sections, select the corresponding tab.
                // We can also use ActionBar.Tab#select() to do this if we have a reference to the
                // Tab.
                actionBar.setSelectedNavigationItem(position);
            }
        });

        // For each of the sections in the app, add a tab to the action bar.
        for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
            // Create a tab with text corresponding to the page title defined by the adapter.
            // Also specify this Activity object, which implements the TabListener interface, as the
            // listener for when this tab is selected.
            actionBar.addTab(
                    actionBar.newTab()
                            .setText(mAppSectionsPagerAdapter.getPageTitle(i))
                            .setTabListener(this));
        }
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        // When the given tab is selected, switch to the corresponding page in the ViewPager.
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to one of the primary
     * sections of the app.
     */
    public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {

        public AppSectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            switch (i) {
                case 0:
                    // The first section of the app is the most interesting -- it offers
                    // a launchpad into the other demonstrations in this example application.
                    return new LaunchpadSectionFragment();

                default:
                    // The other sections of the app are dummy placeholders.
                    Fragment fragment = new DummySectionFragment();
                    Bundle args = new Bundle();
                    args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
                    fragment.setArguments(args);
                    return fragment;
            }
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return "Section " + (position + 1);
        }
    }

    /**
     * A fragment that launches other parts of the demo application.
     */
    public static class LaunchpadSectionFragment extends Fragment {

        public MediaPlayer sound;
        public AudioManager audioM;
        public SoundPool spool;
        public int soundID;
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_section_launchpad, container, false);

            sound = MediaPlayer.create(getActivity(), R.raw._main_select);
            spool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
            soundID = spool.load(getActivity(), R.raw._main_select, 1);

            // Demonstration of a collection-browsing activity.
            rootView.findViewById(R.id.demo_collection_button)
                    .setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            sound.start();
                            //Sound();
                            Intent intent = new Intent(getActivity(), MenuActivity.class);
                            startActivity(intent);
                        }
                    });

            // Demonstration of navigating to external activities.
            rootView.findViewById(R.id.demo_external_activity)
                    .setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            // Create an intent that asks the user to pick a photo, but using
                            // FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET, ensures that relaunching
                            // the application from the device home screen does not return
                            // to the external activity.
                            Intent externalActivityIntent = new Intent(Intent.ACTION_PICK);
                            externalActivityIntent.setType("image/*");
                            externalActivityIntent.addFlags(
                                    Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
                            startActivity(externalActivityIntent);
                        }
                    });

            return rootView;
        }

        public void Sound(){
            AudioManager audioM = (AudioManager) getActivity().getSystemService(AUDIO_SERVICE);
            float volume = (float) audioM.getStreamVolume(AudioManager.STREAM_MUSIC);
            spool.play(soundID, volume, volume, 1, 0, 1f);
        }
    }

    /**
     * A dummy fragment representing a section of the app, but that simply displays dummy text.
     */
    public static class DummySectionFragment extends Fragment {

        public static final String ARG_SECTION_NUMBER = "section_number";

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_section_dummy, container, false);
            Bundle args = getArguments();
            ((TextView) rootView.findViewById(android.R.id.text1)).setText(
                    getString(R.string.dummy_section_text, args.getInt(ARG_SECTION_NUMBER)));
            return rootView;
        }
    }
}

日志:

Execution failed for task ':app:dexDebug'.
 com.android.ide.common.internal.LoggedErrorException: Failed to run command:
    /Users/randolphgordon/adt-bundle-mac-x86_64-20131030/sdk/build-tools/19.0.2/dx --dex --output /Users/randolphgordon/Dropbox/_workspace/NYCTLCHACKPREP/app/build/dex/debug /Users/randolphgordon/Dropbox/_workspace/NYCTLCHACKPREP/app/build/classes/debug /Users/randolphgordon/Dropbox/_workspace/NYCTLCHACKPREP/app/build/dependency-cache/debug /Users/randolphgordon/Dropbox/_workspace/NYCTLCHACKPREP/app/build/pre-dexed/debug/android-support-v4-46e47f42317d6d3bf8d7ae02b23fb005cc7afb47.jar /Users/randolphgordon/Dropbox/_workspace/NYCTLCHACKPREP/app/build/pre-dexed/debug/classes-4a5ca97410ee19b747521fb500fab3687b1a037c.jar /Users/randolphgordon/Dropbox/_workspace/NYCTLCHACKPREP/app/build/pre-dexed/debug/support-v4-19.0.1-0d7b819b7b894911a3c8ca1b4399345922bb3696.jar
  Error Code:
    2
  Output:
    UNEXPECTED TOP-LEVEL EXCEPTION:
    com.android.dex.DexException: Multiple dex files define Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat$AccessibilityServiceInfoVersionImpl;
        at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:594)
        at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:552)
        at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:533)
        at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:170)
        at com.android.dx.merge.DexMerger.merge(DexMerger.java:188)
        at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:439)
        at com.android.dx.command.dexer.Main.runMonoDex(Main.java:287)
        at com.android.dx.command.dexer.Main.run(Main.java:230)
        at com.android.dx.command.dexer.Main.main(Main.java:199)
        at com.android.dx.command.Main.main(Main.java:103)

最佳答案

您的项目中有两份 android-support-v4.jar 副本。删除一个并重新构建。

关于java - 意外的顶级异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22031711/

相关文章:

java - 在没有硬编码声音路径的情况下关闭应用程序时播放声音

android - flutter : How to Fix SuffixIcon at the bottom of the Multiline Enable TextField?

android - 如何在移动时删除从源到当前位置的多段线路径

Java lambda 表达式最佳实践

java - 使用 Android Studio 进行 GitHub 协作

java - 线程 "AWT-EventQueue-0"java.lang.NoSuchMethodError 中的异常

java - 如何使我的可视化 Java 应用程序将自身复制到自定义目录?

android - 有没有办法让 Eclipse 将您的自定义 View 检测为其父布局?

android - 无法在 ionic v2 中安装非原生 cordova 插件

java - 通过 IIS 通过 ISAPI 或 URL 重写公开 Java Web 应用程序