【发布时间】:2016-05-24 05:40:53
【问题描述】:
我已经尝试解决这个问题几天了,但仍然无法解决。我已经检查了许多关于此的类似主题,但没有得到我需要的东西。
如您所见,我正在动态添加片段:
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();
}
这是我无法弄清楚的部分。我正在按标签搜索片段,它似乎不起作用,因为我总是得到空指针异常或类异常。 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>
主活动:
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"/>
【问题讨论】:
-
请在您定义片段或其
Framelayout的位置显示您的布局XML - 我们需要确认您的框架布局的id属性 - 这是您将与findFragmentById(id)一起使用的id方法。 -
我使用的是默认的(android.R.id.content)
-
嗨,请通过包含布局 XML 文件更新您的问题,并向我们展示您的
Activity的onCreate方法 - 我们需要检查这些并为您的问题提出解决方案。 -
我更新了我的主题
-
@Nenco 尝试创建扩展
Fragment的活动,然后在FragmentManager manager = getSupportFragmentManager();之后调用FragmentTransaction。