FragmentActivity无法解析为某种类型

Pra*_*tik 29 android android-fragmentactivity

我正在尝试从这个博客的应用程序.在扩展时FragmentActivity,我收到以下错误:

`FragmentActivity` was not able to resolve.
Run Code Online (Sandbox Code Playgroud)

我错过了任何图书馆还是其他什么?

我的代码:

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();
        }       
    }
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

Pra*_*tik 45

在等了很长时间才得到这个答案之后,我已经把事情搞定了,我认为很多其他人需要一个简单的解决方案来解决这个问题.

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

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

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

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

在此输入图像描述

现在点击Add Jar.它将显示具有所选当前项目的当前项目列表.

展开它并转到libs目录并选择android.supportv4.jar文件,然后单击确定.它现在将显示在列表框中.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.现在单击"确定"关闭"属性"对话框.

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

希望你现在可以获得FragmentActivity类.

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

还有一件事如果您需要支持更低版本的应用程序,那么使用更高版本构建并将此行添加到androidmanifest.xml文件中

<uses-sdk
    android:minSdkVersion="3"
    android:targetSdkVersion="15" />
Run Code Online (Sandbox Code Playgroud)

这将支持1.5到4.0.1版本的设备.


Shi*_*rur 38

如果你正在使用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.
Run Code Online (Sandbox Code Playgroud)


小智 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

希望这对任何寻找答案的人都有帮助.