FragmentActivity 无法解析为类型

发布于 2024-12-16 00:34:34 字数 2142 浏览 1 评论 0原文

我正在尝试此博客中的应用程序。在扩展 FragmentActivity 时,我收到以下错误:

`FragmentActivity` was not able to resolve.

我是否缺少任何库或其他任何内容?

我的代码:

public class Testing_newActivity extends FragmentActivity { // here the FragmentActivity getting error package not found for import
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        if (getResources().getConfiguration().orientation
                == Configuration.ORIENTATION_LANDSCAPE) {
            // If the screen is now in landscape mode, we can show the
            // dialog in-line so we don't need this activity.
            finish();
            return;
        }

        if (savedInstanceState == null) {
            // During initial setup, plug in the details fragment.
            DetailsFragment details = new DetailsFragment();
            details.setArguments(getIntent().getExtras());

            getSupportFragmentManager().beginTransaction().add(
                    android.R.id.content, details).commit();
        }       
    }
}

android清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.searce.testingnew"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="11" />

    <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" >
        <activity android:label="@string/app_name" android:name=".Testing_newActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

I'm trying the application from this blog. While extending the FragmentActivity, I'm getting the following error:

`FragmentActivity` was not able to resolve.

Am I missing any library or anything else?

My code:

public class Testing_newActivity extends FragmentActivity { // here the FragmentActivity getting error package not found for import
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        if (getResources().getConfiguration().orientation
                == Configuration.ORIENTATION_LANDSCAPE) {
            // If the screen is now in landscape mode, we can show the
            // dialog in-line so we don't need this activity.
            finish();
            return;
        }

        if (savedInstanceState == null) {
            // During initial setup, plug in the details fragment.
            DetailsFragment details = new DetailsFragment();
            details.setArguments(getIntent().getExtras());

            getSupportFragmentManager().beginTransaction().add(
                    android.R.id.content, details).commit();
        }       
    }
}

android manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.searce.testingnew"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="11" />

    <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" >
        <activity android:label="@string/app_name" android:name=".Testing_newActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

秋风の叶未落 2024-12-23 00:34:34

等待这个答案很长时间后,我已经整理了一些东西,我认为许多其他人会需要一个简单的解决方案来解决这个问题。

警告:此解决方案适用于使用 Indigo 或更低版本的 eclipse 用户。仍在寻找使用其他 Android IDE 的解决方案用户。

当使用 2.2 或更低版本为应用程序扩展 FragmentActivity 时,您可以从 此处 或者您可以从您的android-sdk 目录 ..\android-sdk\extras\android\support\v4 并放入您的项目中。

如果您的项目中没有libs目录,请创建libs目录并将android.supportv4.jar文件粘贴到该目录中。

从 Eclipse 项目工作区:
选择您要使用的项目/应用程序。右键单击Project并选择Properties选项。在此选择 Java 构建路径 并选择选项卡 Libraries

现在单击 。它将显示当前项目列表以及选定的当前项目。

展开它并转到 libs 目录并选择 android.supportv4.jar 文件,然后单击“确定”。现在它将显示在列表框中。 记住将 jar 文件添加为相对路径而不是绝对路径,因此每当您更改项目目录或在另一台计算机中打开时,都会自动检测项目目录。现在单击“确定”即可关闭属性对话框。

您可以通过将鼠标放在 FragmentActivity 上或按 ctrl+shift+o 导入缺少的文件来导入 android.support.v4.app.FragmentActivity;

希望你现在可以得到FragmentActivity类。

如果您使用的是 eclipse juno,则无需担心下载支持 jar 文件。它会自动放入libs目录并自动添加到您的项目路径中。如果没有,请通过 Properties 选项并添加它。

另一件事如果您需要支持较低版本的应用程序,请使用较高版本进行构建,并将此行添加到 androidmanifest.xml 文件中,

<uses-sdk
    android:minSdkVersion="3"
    android:targetSdkVersion="15" />

现在将支持 1.5 到 4.0.1 版本的设备。

After waiting a long time for this answer, I have sorted things out, and I think that many other people will need a simple solution for this question.

Warning: This solution is for the eclipse user who uses Indigo or lower version. Still looking for solutions users who use other IDE's for android.

When extending the FragmentActivity for your application with version of 2.2 or lower, you can download the android.supportv4.jar from here or you can get this from your android-sdk directory ..\android-sdk\extras\android\support\v4 and put into your project.

If there are no directory libs in your project, create libs directory and paste the android.supportv4.jar file in this directory.

From eclipse project workspace:
Select project/application in which you want to use. Right click on Project and select Properties option. In this select Java build path and select the tab Libraries

enter image description here

Now click on Add Jar. It will display current list of projects with selected current project.

Expand this and go to the libs directory and select android.supportv4.jar file then click ok. It will display in list box now. Remember add the jar file as relative path path not as absolute path, so whenever if you change the directory of your project or open in another machine then will detect automatically for the project directory. Now click on Ok to close the Properties dialog.

You are able to import the android.support.v4.app.FragmentActivity; by placing mouse over the FragmentActivity or pressing the ctrl+shift+o to import missing files.

Hope you can got now FragmentActivity class.

If you are using the eclipse juno, then no need to worry for downloading the support jar file. It will automatically put into the libs directory and automatically added to your project path. If in case not then go through the Properties option and add it.

One more thing If you need to support your application for lower version then build with the higher version and add your this line into your androidmanifest.xml file

<uses-sdk
    android:minSdkVersion="3"
    android:targetSdkVersion="15" />

this will now support for 1.5 to 4.0.1 version devices.

不疑不惑不回忆 2024-12-23 00:34:34

如果您使用的是 Eclipse 和 Windows,可以通过以下方法消除此错误。

Right click on your project folder->Build path-> Configure build path-> 
Add External Jars -> select "android-support-v4.jar" file (It'll be located in Android "android-sdk-windows\extras\android\support")
then click OK.

If you are using eclipse and windows, here is how you can get rid of this error.

Right click on your project folder->Build path-> Configure build path-> 
Add External Jars -> select "android-support-v4.jar" file (It'll be located in Android "android-sdk-windows\extras\android\support")
then click OK.
再浓的妆也掩不了殇 2024-12-23 00:34:34

你必须将外部 android 支持 Jar 添加到你的类路径中,例如在我的例子中,我添加了 android-support-v4.jar 它通常位于 {android_home}\extras\android\support\v4 目录中。我使用的是 Windows XP,就我而言,它位于 C:\Program Files\Android\android-sdk\extras\android\support\v4

希望这对正在寻找答案的人有所帮助。

you have to add external android support Jar to your class path e.g. in my case I added android-support-v4.jar its generally located in the {android_home}\extras\android\support\v4 directory. I am using windows XP and in my case its located at C:\Program Files\Android\android-sdk\extras\android\support\v4

Hope this helps to whoever is looking for answer.

夏天碎花小短裙 2024-12-23 00:34:34

FragmentActivity 是兼容性库的一部分,而不是 Android 3.0 sdk。
除非您想要向后兼容,否则不需要使用它。

不知道为什么这里提到这一点可能是它的一个项目特定类。

尝试查看 sdk 附带的 HCGallery 示例

FragmentActivity is part of the compatibility library and not Android 3.0 sdk.
You don't need to use it unless you want backward compatibility.

Not sure why this is mentioned here may be its a project specific class.

Try looking at HCGallery Sample shipped with the sdk

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文