java - 创建并显示新 Activity ,不能使用我的 fragment ?

标签 java android android-activity android-fragments

我创建了一个新的 activity 并使用 intent 显示它。在 onCreate 中,我试图用我的类替换占位符 fragment ,但它抛出错误 - 无法解析方法。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_edit_viewer);
    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new ViewerEditFragment())
                .commit();
    }

ViewerEditFragment 类:

public class ViewerEditFragment extends Fragment {

    public ViewerEditFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.edit_viewer, container, false);

        return rootView;
    }
}

activity_edit_viewer.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.lightfootsolutions.sfnviewer.app.EditViewerActivity"
    tools:ignore="MergeRootFrame" />

edit_viewer.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/edit_viewer"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <TextView
        style="?android:attr/listSeparatorTextViewStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="sfn Credentials"/>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/username"
            android:paddingLeft="10dp" />
        <EditText
            android:id="@+id/username"
            android:hint="Admin"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/password"
            android:paddingLeft="10dp" />
        <EditText
            android:id="@+id/entry"
            android:inputType="textPassword"
            android:hint="Password"
            android:minWidth="100dip"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>

    <TextView
        style="?android:attr/listSeparatorTextViewStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Host Settings"/>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/host_url"
            android:paddingLeft="10dp" />
        <EditText
            android:id="@+id/hostUrl"
            android:hint="http://www.mysfn.com"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/web_app"
            android:paddingLeft="10dp" />
        <EditText
            android:id="@+id/webApp"
            android:hint="sfn"
            android:minWidth="100dip"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/model_id"
            android:paddingLeft="10dp" />
        <EditText
            android:id="@+id/modelId"
            android:hint="aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/properties"
            android:paddingLeft="10dp" />
        <EditText
            android:id="@+id/properties"
            android:hint="dv=icon"
            android:minWidth="100dip"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>

    <Button android:id="@+id/close"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Save" />
</LinearLayout>

最佳答案

由于您要为 fragment 扩展 android.support.v4.app.Fragment,因此您需要使用 getSupportFragmentManager() 而不是 getFragmentManager()。在这种情况下,一定要extend FragmentActivity

关于java - 创建并显示新 Activity ,不能使用我的 fragment ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24937042/

相关文章:

Android 和通过 Bundle 传递嵌套的 ArrayList

java - 使用 fused Location Provider 获取当前位置

java - 将从字符串派生的唯一数字转换为良好的哈希码

java - 根据新屏幕尺寸重新计算 X 和 Y 坐标

android - johannilsson 如何部署下拉刷新 ListView

android - 当一种模式出现零次或多次时如何从字符串中提取模式

android - 如果服务正在运行,如何启动不同的 Activity (并恢复状态)?

java - Eclipse:java.lang.NoClassDefFoundError:javax/activation/DataHandler

java - 我正在运行一个使用外部 JAR 文件的 Java 项目

android - Retrofit2 中的查询