将片段附加到活动

时间:2014-04-07 14:22:32

标签: android android-fragments

我创建了两个活动,并使用Intent从第一个活动切换到第二个活动。我也在第二个活动中使用了片段。当我尝试运行应用程序时,应用程序启动并且不幸地停止。当我从我的应用程序中删除片段时,应用程序会运行。我该怎么做才能摆脱这个错误?这是我的代码......

First Activity code

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button b=(Button)findViewById(R.id.button1);
    b.setOnClickListener(new OnClickListener() {
          public void onClick(View v) {
         Intent i=new Intent(MainActivity.this,FirstActivity.class);
         startActivity(i);

          }
        });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

第二个活动代码

public class FirstActivity extends Activity {

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

public void selectFrag(View view) 
 {      
     Fragment fr;

     if(view == findViewById(R.id.button2)) 
     {
         fr = new FragmentOne();        


     }

     else if(view==findViewById(R.id.button3)){

         fr = new FragmentOne();         

     }
     else
     {
         fr=new FragmentOne();
     }

     FragmentManager fm = getFragmentManager();

     FragmentTransaction fragmentTransaction = fm.beginTransaction();

     fragmentTransaction.replace(R.id.fragment_first, fr);

     fragmentTransaction.commit();


 }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.first, menu);
    return true;
}

}

片段代码

 public class FragmentOne extends Fragment {
    public View onCreateView(LayoutInflater inflater,

              ViewGroup container, Bundle savedInstanceState) {        

               //Inflate the layout for this fragment           

              return inflater.inflate(

                      R.layout.fragment_one, container, false);
              }

}

我尝试使用片段的第二活动的清单

<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".FirstActivity" >
<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button2"
    android:layout_alignParentBottom="true"
    android:layout_alignRight="@+id/button2"
    android:layout_marginBottom="66dp"
    android:text="@string/IDE" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button3"
    android:layout_alignLeft="@+id/button1"
    android:layout_marginBottom="42dp"
    android:layout_toLeftOf="@+id/fragment_first"
    android:text="@string/Applications" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/fragment_first"
    android:layout_alignBottom="@+id/fragment_first"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="18dp"
    android:text="@string/Operating_Systems" />

<fragment
    android:id="@+id/fragment_first"
    android:name="com.example.test"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/button3"
    android:layout_alignParentTop="true"
    android:layout_marginTop="56dp"
    android:layout_toRightOf="@+id/button1" />

2 个答案:

答案 0 :(得分:0)

您正在尝试替换静态Fragment。静态Fragments(在xml中定义的那些)无法通过FragmentTransactions进行操作。

此外,您告诉Android在此处通知名为Fragment的{​​{1}}:

com.example.test

答案 1 :(得分:0)

  • 首先,在 FirstActivity 的布局中创建一个容器
  • 其次,使用片段管理器,将 FragmentOne 设置为
  • 这就是你需要的所有工作!
相关问题