从主要活动

时间:2016-05-18 19:48:44

标签: android android-fragments

我一直试图解决这个问题几天,仍然无法弄明白。我已经检查了很多关于这个的类似主题,但没有得到我需要的东西。

正如您所看到的,我正在动态添加片段:

  public void displayFragment1() {

    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.fragment_container, new Fragment_1(), "frag1");
    fragmentTransaction.commit();


}

public void displayFragment2() {

    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.fragment_container, new Fragment_2(), "frag2");
    fragmentTransaction.commit();


}

这是我无法弄清楚的部分。我正在通过标签搜索片段,似乎它不起作用,因为我总是得到nullpointer异常或classexception。 Toast只显示我输入的文本的值,以便将数据从片段发送到活动。这部分很好。任何想法的人???

 @Override
public void inputValue(String text) {

    if (text != null) {

        Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();

        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, new Fragment_2(), "frag2");
        fragmentTransaction.commit();

        FragmentManager fm = getSupportFragmentManager();
        Fragment_2 fragment = (Fragment_2) fm.findFragmentByTag("frag2");
        fragment.setChangeTo(text);


    } else {

        Toast.makeText(MainActivity.this, "didnt recieve the value", Toast.LENGTH_SHORT).show();

    }


}

活动主要xml

<RelativeLayout
    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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.neven.question_for_stackoverflow.MainActivity"
    android:background="@drawable/my_background">

<FrameLayout android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:id="@+id/fragment_container">

</FrameLayout>

MainActivity:

public class MainActivity extends AppCompatActivity implements Fragment_1.sendData {

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

片段1 xml:

<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.neven.question_for_stackoverflow.Fragment_1"
         android:background="@drawable/my_background">

<!-- TODO: Update blank fragment layout -->
<TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment"/>
<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="fragment 1"
        android:id="@+id/textView" android:layout_gravity="center"/>
<EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/etInputID" android:layout_gravity="center" android:layout_marginTop="50dp"
        android:hint="enter text here" android:textColorHint="#e3ca0a"/>
<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send"
        android:id="@+id/bSendID" android:layout_gravity="center_horizontal|bottom"
        android:layout_marginBottom="130dp"/>

片段2 xml

<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.neven.question_for_stackoverflow.Fragment_2"
         android:background="@drawable/my_background">

<!-- TODO: Update blank fragment layout -->
<TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment"/>
<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="fragment 2 - change me"
        android:id="@+id/tvChangeMeID" android:layout_gravity="center"/>

3 个答案:

答案 0 :(得分:2)

问题似乎是您在活动displayFragment1();中呼叫onCreate,但您希望找到Fragment_2,而您只在displayFragment2();设置/初始化onCreate {1}} - 你永远不会称这种方法。 因此,请使用displayFragment2()方法更改您的代码,以便像这样调用protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); displayFragment2(); }

getSupportFragmentManager().executePendingTransactions()

更新: 请在commit语句后添加调用Fragment,然后尝试通过TAG查找Fragment,看看这是否至少返回... FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); fragmentTransaction.replace(R.id.fragment_container, new Fragment_2(), "frag2"); fragmentTransaction.commit(); getSupportFragmentManager().executePendingTransactions() Fragment_2 fragment = (Fragment_2) getSupportFragmentManager().findFragmentByTag("frag2"); fragment.setChangeTo(text); 对象而不是null。您的代码现在看起来像:

errSSLNoRootCert—The peer’s certificate chain was not verifiable to a root certificate.

我希望这会有所帮助。

答案 1 :(得分:0)

您应该在Activity的布局xml中声明一个FrameLayout,其中您想要的片段将被夸大。然后在进行事务而不是android.R.id.content时使用该FrameLayout的id。

答案 2 :(得分:0)

处理此问题的方法是:

Fragment fragment = getActivity().getFragmentManager().findFragmentById(R.id.fragment_container); 
if (fragment instanceof    YourFragmentClass) {
fragment.setChangeTo(value);
}