【问题标题】:Adding Fragment to Android将片段添加到 Android
【发布时间】:2017-09-01 14:51:52
【问题描述】:

我正在尝试一个非常简单的事情。通过java代码添加片段。但它在 (fragmentTransaction.add(R.id.fragment_container, fragment);) 中显示错误。我无法理解这里出了什么问题。请帮忙。有两个类,主类和片段类。以及两种布局主要布局和片段布局。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.chetan.fragmentprtc.MainActivity">


<fragment
    android:id="@+id/fragment_container"
    android:name="android.app.DialogFragment"
    android:layout_width="368dp"
    android:layout_height="495dp"
    tools:layout_editor_absoluteX="8dp"
    tools:layout_editor_absoluteY="8dp" />

</android.support.constraint.ConstraintLayout>


package com.example.chetan.fragmentprtc;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = 
fragmentManager.beginTransaction();
    BlankFragment fragment = new BlankFragment();
    fragmentTransaction.add(R.id.fragment_container, fragment);
    fragmentTransaction.commit();
}
}




<FrameLayout 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"
tools:context="com.example.chetan.fragmentprtc.BlankFragment">

<!-- TODO: Update blank fragment layout -->
<TextView
    android:id="@+id/fragment1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="@string/hello_blank_fragment" />

</FrameLayout>

package com.example.chetan.fragmentprtc;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class BlankFragment extends Fragment {


public BlankFragment() {
    // Required empty public constructor
}


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

}

}

【问题讨论】:

    标签: java android fragment


    【解决方案1】:

    我不知道为什么上面的代码对我不起作用。但这确实:

     BlankFragment firstFragment = new BlankFragment();
     getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment_container, firstFragment).commit();
    

    这没有显示任何错误,它确实有效!谢谢大家。

    【讨论】:

    • 我打算建议这种方法,很高兴您发布了解决方案。我相信由于您的活动扩展了AppCompatActivity,因此您需要在适用时使用支持库来正确实现向后兼容性。
    【解决方案2】:

    注意!!

    处理片段时的一个重要规则是您的activity 布局必须包含一个容器视图,您可以在其中插入片段。

    Container 可以是 ViewGroup,例如 FrameLayoutLinearLayout。在您的情况下,您正在替换 Fragment 中的片段 - 这就是问题所在。

    使用下面的代码作为容器 -

    <FrameLayout
        android:id="@+id/fragment_container"
        ....
        ... />
    

    【讨论】:

    • 向导您好!我已经更改了 xlm 文件中的代码。但是一行有一个错误:transaction.replace(R.id.fragment_container, fragment);
    【解决方案3】:

    首先你必须记住一件事: 在 XML 中硬编码的片段,无法替换。 而且您的代码似乎不止一个问题。首先是 MainActivity 的布局,另一件事是根据您的需要替换而不是添加片段。好吧,我以简单的方式重构了您的代码,如下所示:

     <?xml version="1.0" encoding="utf-8"?>
    
    <FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_container"
    android:name="android.app.DialogFragment"
    android:layout_width="match_parent"
     android:layout_height="match_parent"
     tools:context="com.example.chetan.fragmentprtc.MainActivity"
    />
    

    您的 MainActivity 代码:

    package com.example.chetan.fragmentprtc;
    import android.app.FragmentManager;
    import android.app.FragmentTransaction;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    
    public class MainActivity extends AppCompatActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
      FragmentTransaction transaction = 
     getFragmentManager().beginTransaction();
    BlankFragment fragment = new BlankFragment();
    transaction.replace(R.id.fragment_container, fragment);
    transaction.addToBackStack(null);
    
    transaction.commit();
    
    
     }
    } 
    

    子片段的布局为:

    <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"
    tools:context="com.example.chetan.fragmentprtc.BlankFragment">
    
    <TextView
    android:id="@+id/fragment1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:text="@string/hello_blank_fragment" />
    
     </LinearLayout>
    

    希望重构这么多后它会起作用。

    【讨论】:

    • 您好 Krantiz,感谢您的帮助。我使用了你的代码,但它仍然显示错误:transaction.replace(R.id.fragment_container, fragment);
    猜你喜欢
    • 2017-11-29
    • 1970-01-01
    • 1970-01-01
    • 2012-11-18
    • 1970-01-01
    • 2013-02-03
    • 1970-01-01
    • 1970-01-01
    • 2017-04-14
    相关资源
    最近更新 更多