无论是静态还是动态app都崩溃
import android.content.Intent; import android.os.Bundle; import android.app.Activity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.view.View; import android.widget.ImageView; public class MainActivity extends Activity implements View.OnClickListener{ private ImageView imageView1, imageView2, imageView3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.content_main); imageView1 = (ImageView) findViewById(R.id.btn1); imageView2 = (ImageView) findViewById(R.id.btn2); imageView3 = (ImageView) findViewById(R.id.btn3); imageView1.setOnClickListener(this); imageView2.setOnClickListener(this); imageView3.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.btn1:{ Intent intent = new Intent(); intent.setClassName("newzilllion.fragment", "newzillion.fragment.MainActivity2"); startActivity(intent); break; } case R.id.btn2:{ Fragment2 fragment2 = new Fragment2(); FragmentManager fragmentManager = fragment2.getFragmentManager(); FragmentTransaction beginTransaction = fragmentManager.beginTransaction(); beginTransaction.add(R.id.linear, fragment2); beginTransaction.commit(); break; } case R.id.btn3:{ break; } } } }
下面是第二个 MainActivity
package newzillion.fragment; /** * Created by HASEE on 2016/3/27. */ import android.app.Activity; import android.os.Bundle; public class MainActivity2 extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } }
静态加载的 fragment
import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * Created by HASEE on 2016/3/27. */ /* * 这个设置为静态的 fragment */ public class Fragment1 extends Fragment{ @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment1 , container , false); return view; } }
动态加载的fragment
import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * Created by HASEE on 2016/3/27. */ /* * 这个设置为动态的 fragment*/ public class Fragment2 extends Fragment{ @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment1 , container , false); return view; } }
manifest
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="newzillion.fragment"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:label="@string/app_name" android:theme="@style/AppTheme.NoActionBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".MainActivity2"/> </application> </manifest>
MainActivity 的布局文件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="newzillion.fragment.MainActivity" tools:showIn="@layout/activity_main"> <RadioGroup android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="clip_horizontal" android:layout_marginBottom="10dp"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/scissor" android:id="@+id/btn1"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn2" android:src="@drawable/stone"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn3" android:src="@drawable/paper"/> </RadioGroup> <LinearLayout android:layout_marginBottom="5dp" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/linear"></LinearLayout> </RelativeLayout>
第二个Activity的布局文件
<?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"> <fragment android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/fragment" android:name="newzillion.fragment.Fragment1"/> </LinearLayout>
fragment 连接的布局文件
<?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"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点击按钮吧兄弟" android:id="@+id/textView"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn" android:text="我是按钮"/> </LinearLayout>