【问题标题】:Calling a Class Fragment which extends Fragment from a MainActivity Class which extends Activity从扩展 Activity 的 MainActivity 类调用扩展 Fragment 的 Class Fragment
【发布时间】:2015-04-14 10:56:07
【问题描述】:

如何从扩展活动的类中调用扩展片段的类,我尝试查看各种教程和堆栈问题,但是当我尝试实现 FragmentManager 等时。 我在 fragmentTransaction.add(R.id.LinearLayout1, fragment); 行收到错误陈述 '无法解析方法 add(int, com.example.angrywords.AccountDetailsFragment)'

我对 android 还很陌生,因此非常感谢任何帮助。 谢谢。

我的 MainAcivity.class

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarDrawerToggle;
import android.text.format.Time;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnRetrieveHttpData, View.OnClickListener {
    ActionBarDrawerToggle mDrawerToggle;
    Button logoutbtn;
    Button Optionsbtn;
    private String mUserName;
    private String mPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Hide the ActionBar
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        // Set Layout
        setContentView(R.layout.activity_main);

        SharedPreferencesWrapper.getFromPrefs(this, "username", mUserName);
        SharedPreferencesWrapper.getFromPrefs(this, "password", mPassword);

        logoutbtn = (Button) findViewById(R.id.logoutButton);
        logoutbtn.setOnClickListener(MainActivity.this);

        Optionsbtn = (Button) findViewById(R.id.optionsButton);

        View.OnClickListener listener = new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                AccountDetailsFragment fragment = new AccountDetailsFragment();
                fragmentTransaction.add(R.id.LinearLayout1, fragment);
                fragmentTransaction.commit();
            }
        };

        Optionsbtn.setOnClickListener(listener);
    }

    public void NewPuzzleButtonClicked(View v) {
        if (v.getId() == R.id.newPuzzleButton) {
            //handle the click here
            startNewPuzzleActivity();
        }
    }

    public void SavedPuzzleButtonClicked(View v) {
        if (v.getId() == R.id.savedPuzzlesButton) {
            //handle the click here
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

我的 AccountDetailsFragment.class

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class AccountDetailsFragment extends Fragment {
    Button changePasswordbtn;
    Button unregisterbtn;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

            View v;
            v = inflater.inflate(R.layout.activity_account, container, false);
            changePasswordbtn = (Button)v.findViewById(R.id.changePasswordbtn);
            unregisterbtn = (Button)v.findViewById(R.id.unregisterbtn);

            TextView mName = (TextView) v.findViewById(R.id.account_details_name_textview);
            String mFirstName = SharedPreferencesWrapper.getFromPrefs(getActivity(), "first_name", "First Name");
            String mLastName = SharedPreferencesWrapper.getFromPrefs(getActivity(), "last_name", "Last Name");
            mName.setText(mFirstName + " " + mLastName);

            TextView mUsername = (TextView) v.findViewById(R.id.account_details_username_textview);
            mUsername.setText(SharedPreferencesWrapper.getFromPrefs(getActivity(), "username", "Username"));

            TextView mPassword = (TextView) v.findViewById(R.id.account_details_password_textview);
            mPassword.setText(SharedPreferencesWrapper.getFromPrefs(getActivity(), "password", "Password"));

            changePasswordbtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DialogFragment changePasswordFragment = new ChangePasswordDialogFragment();
                    changePasswordFragment.show(getFragmentManager(), "DIALOG_CHANGE_PASSWORD");
                }
            });
            unregisterbtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DialogFragment unRegisterFragment = new UnregisterDialogFragment();
                    unRegisterFragment.show(getFragmentManager(), "DIALOG_UNREGISTER");
                }
            });
         return v;
        }

        @Override
        public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
            inflater.inflate(R.menu.menu_main, menu);
            super.onCreateOptionsMenu(menu, inflater);
        }

    }

还有我的 activity_main.xml(如果有帮助的话)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:gravity="bottom"
android:orientation="horizontal"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_marginTop="36dp"
    android:layout_weight="1"
    android:orientation="vertical"
    android:weightSum="1">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:gravity="center|bottom" >

        <TextView
            android:id="@+id/welcomeTextView"
            android:layout_width="wrap_content"
            android:layout_height="143dp"
            android:shadowDx="1"
            android:shadowDy="1"
            android:shadowRadius="2"
            android:text="label main welcome"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textSize="26sp"
            android:textStyle="bold"
            android:typeface="normal"
            android:layout_gravity="center_vertical" />

    </LinearLayout>

    <TextView
        android:id="@+id/currentUserTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:text="label_main_sub"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="14sp"
        android:layout_gravity="center_horizontal"
        android:layout_weight="0.22" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Log out"
        android:id="@+id/logoutButton"
        android:layout_gravity="center_horizontal" />

</LinearLayout>

<LinearLayout
    android:layout_width="316dp"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp" >

        <Button
            android:id="@+id/newPuzzleButton"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:onClick="NewPuzzleButtonClicked"
            android:text="New Puzzle"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp" >

        <Button
            android:id="@+id/savedPuzzlesButton"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Resume Saved Puzzle" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/optionsButton"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Options" />

    </LinearLayout>
    <FrameLayout
        android:id="@+id/FragmentContainer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

【问题讨论】:

  • 两个类中的活动和片段的导入语句是什么?
  • 我现在已经包含了导入。

标签: java android android-fragments


【解决方案1】:

确保您的MainAcivity 是从android.support.v4.app.FragmentActivityandroid.support.v7.app.ActionBarActivity 扩展的,并且您的AccountDetailsFragment 是从android.support.v4.app.Fragment 扩展的,并且您的FragmentManagergetSupportFragmentManager() 的结果

【讨论】:

  • 好的,我已经尝试实现你的两个想法并将扩展更改为FragmentActivity,然后将扩展更改为ActionBarActivity。这些都不起作用,但是当将android.support.v4.app.Fragment 更改为android.app.Fragment 时,它起作用了,但是我在AccountDetailsFragment 在线AccountDetailsFragment 中遇到了一个关于getFragmentManager() 的问题,@ 987654334@ 声明无法解析方法'show( android.app.FragmentManager , java.lang.String)' 是否有解决方法或通过不同的导入修复?
  • 我已经设法解决了这个问题,不是以最漂亮的方式,而是通过在我的 MainActivity 中的原始语句中实现 android.support.v4.app.FragmentManagerandroid.support.v4.app.FragmentTransaction。感谢您的意见和支持。非常感谢。
【解决方案2】:

问题在于您使用的 Fragment 类型。在活动中,您导入了处理标准 Fragment 的标准 FragmentManager,而您的 AccountDetailsFragment 是 v4 Fragment 类型。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多