FragmentActivity无法解析为一种类型。

29

我正在尝试来自这篇博客的应用程序。在扩展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>

检查您的导入并确保所有所需的都在那里。(如果您发布一些代码/错误日志/其他内容,将更容易帮助您)。 - kaspermoerch
4个回答

45

在等待了很长时间后,我已经整理好了事情,并且我认为许多其他人将需要一个简单的解决方案来回答此问题。

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

当您为2.2或更低版本的应用程序扩展FragmentActivity时,可以从这里下载android.supportv4.jar,或者您可以从您的android-sdk目录 ..\android-sdk\extras\android\support\v4 中获取并将其放入项目中。

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

从Eclipse项目工作区:选择您想要使用的project/application。右键单击Project并选择Properties选项。在此选择Java build path并选择选项卡Libraries

enter image description here

现在点击Add Jar。它将显示当前选择的当前项目的项目列表。

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

您可以通过将鼠标悬停在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的设备。


这对我很有帮助!不过我按了 添加外部JAR包... - ymerdrengene

37

如果你正在使用 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.

那么对于 WatchActivity 无法解析为类型应该怎么办? - Prasad

5

您需要将外部的Android支持Jar添加到您的类路径中,例如在我的情况下,我添加了android-support-v4.jar,通常位于{android_home}\extras\android\support\v4目录中。我使用的是Windows XP,在我的情况下,它位于C:\Program Files\Android\android-sdk\extras\android\support\v4

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


2

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

不确定为什么在这里提到它,可能是一个项目特定的类。

可以尝试查看随sdk一起发货的HCGallery示例。


那么这个需要用哪个库? - Pratik

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接