O aplicativo não está sendo executado. (Unable to start activity ComponentInfo{com.example.wesley.fragmentos/com.example.wesley.fragmentos.MainActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f07003e (com.example.wesley.fragmentos:id/layoutFrag) for fragment Fragment1{9f29d45 #0 id=0x7f07003e Fragment1)
MainActivity
package com.example.wesley.fragmentos; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends android.support.v4.app.FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if(savedInstanceState == null) { android.support.v4.app.FragmentManager fm = getSupportFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); Fragment1 frag1 = new Fragment1(); ft.add(R.id.layoutFrag, frag1, "Fragment1"); ft.commit(); } } }
Fragment1
package com.example.wesley.fragmentos; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class Fragment1 extends android.support.v4.app.Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_1, container,false); return view; } }
Fragment_1.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/layoutFrag"> <TextView android:id="@+id/text" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="Hello, World! Eu sou um fragmento."/> </FrameLayout>