片段活动未找到类异常 android

2024-01-06

我正在研究用于姜饼操作系统的片段活动。当我尝试在姜饼模拟器上运行应用程序时,由于以下原因,应用程序被强制关闭ClassNotFound错误。我在下面提供我的主要片段活动代码

和平的帮助将是值得赞赏的。

  package com.example.android.effectivenavigation;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
public class MainActivity 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;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // 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() {
        @SuppressLint("NewApi")
        @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));
    }
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@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 {
    ArrayAdapter<String> adapter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_section_launchpad, container, false);
        ListView listView = (ListView)rootView.findViewById(R.id.listview);
        String[] values = new String[] {"Akshay Borgave","Pramod Mahake","Vishal Lokhande","Vivek Chaudhari","Rahul Borole","Neha Gadekar"};


      adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_2, android.R.id.text1, values);


        listView.setAdapter(adapter);
        // Demonstration of a collection-browsing activity.
        /*rootView.findViewById(R.id.demo_collection_button)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(getActivity(), CollectionDemoActivity.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;
    }
}

/**
 * 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;
    }
}
}

清单看起来像这样

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.effectivenavigation"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="9" 
    android:targetSdkVersion="10"/>

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.WithActionBar" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".CollectionDemoActivity"
        android:label="@string/demo_collection" />
</application>

日志猫是

06-12 16:37:17.099: E/AndroidRuntime(378): FATAL EXCEPTION: main
06-12 16:37:17.099: E/AndroidRuntime(378): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.android.effectivenavigation/com.example.android.effectivenavigation.MainActivity}: java.lang.ClassNotFoundException: com.example.android.effectivenavigation.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.android.effectivenavigation-1.apk]
06-12 16:37:17.099: E/AndroidRuntime(378):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
06-12 16:37:17.099: E/AndroidRuntime(378):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-12 16:37:17.099: E/AndroidRuntime(378):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-12 16:37:17.099: E/AndroidRuntime(378):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-12 16:37:17.099: E/AndroidRuntime(378):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-12 16:37:17.099: E/AndroidRuntime(378):  at android.os.Looper.loop(Looper.java:123)
06-12 16:37:17.099: E/AndroidRuntime(378):  at android.app.ActivityThread.main(ActivityThread.java:3683)
06-12 16:37:17.099: E/AndroidRuntime(378):  at java.lang.reflect.Method.invokeNative(Native Method)
06-12 16:37:17.099: E/AndroidRuntime(378):  at java.lang.reflect.Method.invoke(Method.java:507)
06-12 16:37:17.099: E/AndroidRuntime(378):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-12 16:37:17.099: E/AndroidRuntime(378):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-12 16:37:17.099: E/AndroidRuntime(378):  at dalvik.system.NativeStart.main(Native Method)
06-12 16:37:17.099: E/AndroidRuntime(378): Caused by: java.lang.ClassNotFoundException: com.example.android.effectivenavigation.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.android.effectivenavigation-1.apk]
06-12 16:37:17.099: E/AndroidRuntime(378):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
06-12 16:37:17.099: E/AndroidRuntime(378):  at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
06-12 16:37:17.099: E/AndroidRuntime(378):  at  java.lang.ClassLoader.loadClass(ClassLoader.java:511)
06-12 16:37:17.099: E/AndroidRuntime(378):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
06-12 16:37:17.099: E/AndroidRuntime(378):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
06-12 16:37:17.099: E/AndroidRuntime(378):  ... 11 more

Fragment Activity的Xml布局文件如下

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />

再次提前致谢


在你的manifest.xml中你有

<uses-sdk android:minSdkVersion="9" 
    android:targetSdkVersion="10"/>

碎片 http://developer.android.com/guide/components/fragments.html被介绍于API 级别 11,即Android 3.0。我认为你正在得到ClassNotFoundException因为系统不了解较低版本中的 Fragment 类。

所以你需要使用Android 支持库 http://developer.android.com/tools/extras/support-library.html在较低的 API 级别中使用 Fragment。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

片段活动未找到类异常 android 的相关文章

随机推荐

  • Spring Batch - 并非所有记录都通过 MQ 检索进行处理

    我对 Spring 和 Spring Batch 相当陌生 所以如果您有任何疑问 请随时提出任何澄清问题 我发现 Spring Batch 存在问题 无法在测试或本地环境中重新创建 我们的日常工作是通过 JMS 连接到 Websphere
  • Java - Checkstyle - 冗余抛出

    我正在使用 STS 并安装了 checkstyle 插件 使用安装新软件与此网址http eclipse cs sourceforge net update http eclipse cs sourceforge net update 我的
  • 事件监听器如何工作?

    他们是否反复检查条件并在满足条件时执行 例如 操作系统如何准确知道 USB 设备何时插入 或者 MSN 如何准确知道您何时收到电子邮件 这是如何运作的 Thanks 在底层 操作系统内核 知道 何时发生某些事情 因为相关设备向 CPU 发送
  • 不处理从这里抛出的 Swift 错误

    我的代码在 Xcode 6 中工作 但自从我使用 Xcode 7 以来 我不知道如何解决这个问题 let jsonresult 行有一个错误 指出未处理从此处抛出的错误 代码如下 func connectionDidFinishLoadin
  • Hibernate / SQLException:字段没有默认值

    使用以下命令生成 mySQL 表 CREATE TABLE actors actorID INT 11 NOT NULL actorName VARCHAR 255 NOT NULL PRIMARY KEY AUTO INCREMENT a
  • 类型错误:$.datepicker 未定义

    我的 javascript 有代码 适用于我网站上的其中一个页面 nmdt1 datetimepicker dateFormat datepicker ATOM minDate nmsdt 当加载 id nmdt1 的页面时 这运行正常 我
  • 自定义键盘中handleInputModeList的正确实现

    iOS 10 中添加了一个新的 API 用于显示用户可以切换到的其他键盘列表 与用户在系统键盘上长按地球仪时出现的列表相同 函数的声明如下 func handleInputModeList from view UIView with eve
  • 如何在 Python 中将折叠标量转储到 YAML(使用 ruamel?)

    我一直在 stackoverflow 上搜索 寻找一种使用 Python 以 YAML 格式转储折叠标量的方法 普通的answer https stackoverflow com a 35406862 3615411来自用户Anthon h
  • 无法远程启动WebLogic进行调试

    我使用以下选项设置远程 WebLogic 服务器进行调试 Xdebug Xnoagent Xrunjdwp transport dt socket address DEBUG PORT server y suspend n Djava co
  • SVN 到 GitHub 迁移

    我必须将多个目录从 SVN trunk 迁移到one单个 GitHub 存储库 我可以使用以下命令一次克隆单个目录 git svn clone https svn repo url 如何使用 svn trunk 中的单个命令克隆多个目录 进
  • 错误:无法找到类 R.java 没有这样的文件或目录

    当我尝试开始一个新项目时 我不断收到此消息 ERROR Unable to open class file C Users Levi Desktop Android workspace Droid1 gen com androidbook
  • rbenv:权限被拒绝

    我正在关注 Ryan 的 RailsCast Episode 339 我已经安装了 rbenv 并且可以运行ruby v 我退出了会话 当我尝试返回时 通过su deployer from root 我收到这个错误 home deploye
  • 如何在不停止管道的情况下在多个 rtsp 视频流之间切换 [无缝流媒体]

    我使用 5 个 ip 摄像机 每个摄像机为我提供 5 个 RTSP 流 我选择这些 RTSP 视频流中的任何一个 并将它们与我的麦克风 音频 RTSP 流源 混合并将其广播到我的 RTMP 服务器 我尝试进行无缝流传输 这意味着当相机到相机
  • Node.js 应用程序中出现“EACCES”错误

    今天我通过卸载旧版本来更新节点版本 我最近安装的版本是 4 5 0 LTS 安装后 当我尝试安装新的 npm 时 它不起作用并给出以下错误 C Users myuser npm install g yo npm ERR Windows NT
  • 通过 VBA 传递 Python 参数

    我写了一个python脚本 需要使用VBA调用 我编写了一个 python 脚本 可以解析 pdf 将我需要的数据存储在变量中 并写入预制的 Excel 工作表以用这些值填充单元格 我已经成功地能够调用该脚本并使用以下代码运行它 Sub R
  • Chrome 和 Android 中的 Web SQL 存储限制?

    因此 我正在编写一个 Web 应用程序 需要在离线 Web SQL 数据库中存储约 40MB 的离线数据 它需要在 Chrome 桌面 Safari 桌面和移动 和 Android 浏览器中工作 现在我知道这些浏览器支持 Web SQL 并
  • 如何在 RHEL Linux 服务器上安装 Cargo?

    我尝试在 RHEL 服务器上安装 Cargo curl https sh rustup rs sSf sh 但完成后 我得到回复 cargo bash cargo command not found 有其他安装方式吗 首先启用rhel 7
  • 如何在共享框架中为 XCTest 创建公共扩展?

    例如 我从不使用以下描述XCTestCase expectation 所以我想使用一个函数来为其提供默认值 并通过命名清楚地表明我正在初始化期望 因为您不能真正使用初始化程序XCTestExpectation 但如果扩展不在测试目标中 则无
  • 在 jboss 独立运行时事务无法继续 STATUS_MARKED_ROLLBACK

    有人遇到以下问题吗 我能够使用 jboss 工具在 eclipse 内的 jboss 中成功构建 部署和运行我的 javaee6 应用程序 但是当我们将其部署到另一台独立运行的服务器上时 我们遇到了错误 我在 eclipse 所在的同一台机
  • 片段活动未找到类异常 android

    我正在研究用于姜饼操作系统的片段活动 当我尝试在姜饼模拟器上运行应用程序时 由于以下原因 应用程序被强制关闭ClassNotFound错误 我在下面提供我的主要片段活动代码 和平的帮助将是值得赞赏的 package com example