我在绕过Fragments时遇到了问题,但在我遇到更高级的问题之前(比如关于布局更改的推荐方法)我已经在基本级别失败了。
下面我有我想要完成的项目的示例代码两件事:
添加删除片段
让视图/ ViewGroups / Fragments在添加/删除内容时调整它们的大小(我认为这一点在我担心调整不同屏幕模式的布局时会很有用),像这样:
------------------
| -------------- |
| | | |
| | | |
| | | |
| | ViewGroup1 | |
| | | |
| | | |
| | | |
| | | |
| -------------- |
------------------
==> (by pressing a button, layout, event, ...)
------------------
| -------------- |
| | | |
| | ViewGroup1 | |
| | | |
| -------------- |
| -------------- |
| | | |
| | ViewGroup2 | |
| | | |
| -------------- |
------------------
现在我两个都失败了:
这是我的示例代码:
activity_main.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"
tools:context=".MainActivity"
android:id="@+id/iRootLinearLayoutContainer"
android:orientation="vertical"
>
<Button
android:id="@+id/iMainButton"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Add fragment..."
/>
</LinearLayout>
MainActivity.java:
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button fragmentButton = (Button)findViewById(R.id.iMainButton);
fragmentButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if(fragmentButton.getText().equals("Add fragment..."))
{
Log.d("MainActivity.java: ", "Clicking Button and hopefully adding fragment.");
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.iRootLinearLayoutContainer, new TestFragment());
ft.commit();
fragmentButton.setText("Remove fragment...");
}
else
{
Log.d("MainActivity.java: ", "Clicking Button and hopefully removing fragment.");
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Fragment frag = fm.findFragmentById(R.id.iTestFragment);
ft.remove(frag);
ft.commit();
fragmentButton.setText("Add fragment...");
}
}
});
}
}
ltestfragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/iTestFragment"
>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="This is my test fragment."
/>
</LinearLayout>
TestFragment.java
public class TestFragment extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.inflate(R.layout.ltestfragment, container, false);
}
}
根据请求,此处出现错误日志:
致命的例外:主要 显示java.lang.NullPointerException
at android.app.BackStackRecord.run(BackStackRecord.java:651) at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435) at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441) at android.os.Handler.handleCallback(Handler.java:730) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5103) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:525) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
答案 0 :(得分:0)
您正在使用Fragment frag = fm.findFragmentById(R.id.iTestFragment)
按ID查找片段。但是我没有看到你在哪里为那个片段设置id。
R.id.iTestFragment
是LinearLayout对象的id,而不是其容器Fragment对象的id。
如果片段嵌入带有<fragment>
标记的布局中,则片段必须具有ID。要从代码中查找片段,请在添加时设置“标记”字符串。
例如:
ft.add(R.id.iRootLinearLayoutContainer,new TestFragment(),“uniqueTag”);
以后,找到它:
Fragment frag = fm.findFragmentByTag(“UniqueTag”);