Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 更新片段时getActivity()为什么起作用?_Android_Android Fragments - Fatal编程技术网

Android 更新片段时getActivity()为什么起作用?

Android 更新片段时getActivity()为什么起作用?,android,android-fragments,Android,Android Fragments,我有两个简单的xml文件,一个带有、和,另一个带有 对于我的两个类,我的MainActivity为按钮设置onClickListeners,以便通过bundle将输入从EditText发送到另一个类FragmentActivity 我的碎片活动如下所示: public class FragmentActivity extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup co

我有两个简单的xml文件,一个带有
,另一个带有

对于我的两个类,我的MainActivity为按钮设置onClickListeners,以便通过bundle将输入从EditText发送到另一个类FragmentActivity

我的碎片活动如下所示:

public class FragmentActivity extends Fragment {

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

    View V = inflater.inflate(R.layout.activity_fragment, container, false);


    Bundle input = this.getArguments();

    if(input != null) {

        String message = input.getString("msg");

        TextView fragText = (TextView) getActivity().findViewById(R.id.fragment_text);

        fragText.setText(message);
    }

    return V;
}
}

最初,我的fragText看起来是这样的:

TextView fragText = (TextView) V.findViewById(R.id.fragment_text);
结果是文本永远不会更新。这到底是怎么回事

另外,当您用代码中的视图替换片段时,这是否意味着您可以将main_layout.xml视为没有
,而是有其他内容(如

编辑:抱歉,不发布其余有问题的文件可能不明智

activity_fragment.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.test.android.testfragapp.FragmentActivity">

    <TextView
        android:id="@+id/fragment_text"
        android:text="This is a fragment."
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

由于您还没有发布xml文件,我需要猜测一下。R.layout.activity\u片段真的包含您的文本视图吗?从命名来看,我认为它包含您的片段标记,这是您活动的布局,因此完全是错误的。

我们需要查看您的XML文件才能更好地回答这个问题。我们还需要了解您的
片段如何添加到您的
活动中,但从您发布的内容来看,您似乎在夸大片段中的活动布局

这不是应该的工作方式。我猜你想要的是以下几点

  • 一个
    活动\u main.xml
    ,它只包含您的
    标记
  • 包含您的
    TextView
    s的
    my_fragment.xml
  • 一个
    活动
    在其
    onCreate()中执行
    setContentView(R.layout.Activity\u main.xml)
  • 一个
    片段
    ,它确实
    查看fragView=充气机。充气(R.layout.my_片段,容器,false)在其
    onCreateView()
  • 现在,您可以执行
    fragView.findviewbyd(R.id.fragment\u text)


    简言之,您似乎看到了奇怪的行为,因为您将片段布局和活动布局混淆了。

    使用所有源文件进行了更新。我希望我没有做得不对……是的,它确实包含TextView。包含片段的是activity_main.xml布局。我开始怀疑我做得不对。我明白了,问题是你试图在运行时替换一个片段。这不适用于片段标记。片段标记是静态的。如果要在运行时替换片段,则需要添加布局容器(例如FrameLayout)并用FragmentTransaction替换此容器:用getActivity()替换v.findviewbyid应该不起作用。还有,如何用视图替换片段。你想用它实现什么?我真的不知道我现在在做什么,我想起来了。我想做的是在上面的部分有一些文本,可以用EditText和按钮轻松更新,但我刚刚意识到我可以用一个简单的文本替换activity_main.xml,通过onClick方法可以用setText轻松修改。我真的无话可说。
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <fragment
            class="com.test.android.testfragapp.FragmentActivity"
            android:id="@+id/fragment_holder"
            android:layout_width="match_parent"
            android:layout_height="0px"
            android:layout_weight="4"
            tools:layout="@layout/activity_fragment"/>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0px"
            android:layout_weight="5"
            android:orientation="vertical"
            android:gravity="center_horizontal">
    
            <EditText
                android:id="@+id/input_text"
                android:layout_width="300dp"
                android:layout_height="wrap_content"
                android:layout_marginBottom="20dp"
                android:gravity="center" />
    
            <Button
                android:id="@+id/button_display_regular"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Click to display"/>
    
            <Button
                android:id="@+id/button_display_reverse"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Click to reverse"/>
    
        </LinearLayout>
    
    </LinearLayout>
    
    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // removes the shadow from the action bar for lollipop devices
            if(Integer.valueOf(Build.VERSION.SDK_INT) >= 21)
                getActionBar().setElevation(0);
    
            final EditText input_text = (EditText) findViewById(R.id.input_text);
    
            this.findViewById(R.id.button_display_regular).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Bundle bundle = new Bundle();
                    bundle.putString("msg", input_text.getText().toString());
                    placeFragment(bundle);
                }
            });
        }
    
        // Replace the fragment with the activity_fragment layout
        public void placeFragment(Bundle bundle){
            Fragment fr = new FragmentActivity();
    
            fr.setArguments(bundle);
    
            FragmentManager fm = getFragmentManager();
            FragmentTransaction fragmentTransaction = fm.beginTransaction();
            fragmentTransaction.replace(R.id.fragment_holder, fr);
            fragmentTransaction.commit();
        }
    }