Tengo una actividad y dos fragmentos y al rotar la pantalla del dispositivo aparece el error de que "Unable to start activity"
. Indica que el problema está en la línea de la actividad donde se pone setContentView(R.layout.fragments
); en la actividad. ¿Cómo podría solucionarlo?
Fragmento1:
public class Fragment1 extends Fragment{
private Callbacks mCallback = null;
public static Fragment1 newInstance() {
Fragment1 fragment = new Fragment1();
return fragment;
}
public Fragment1(){
}
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View rootView;
rootView = inflater.inflate(R.layout.principal,
container, false);
return rootView;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
mCallback = (Callbacks) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() + " must implement Callbacks");
}
}
public interface Callbacks {
public void onBoton1Selected (View v);
public void onBoton2Selected (View v);
}
}
En el fragmento2:
public class Fragment2 extends Fragment {
private static String boton="";
public View rootView = null;
public static Fragment2 newInstance(String botonSelected) {
Fragment2 fragment = new Fragment2();
Bundle args = new Bundle();
args.putString("BOTON", botonSelected);
boton=botonSelected;
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (savedInstanceState != null) {
boton = savedInstanceState.getString("BOTON");
}
if(boton.compareTo("boton1")==0) {
rootView = inflater.inflate(R.layout.boton1,
container, false);
}else{
if(boton.compareTo("boton2")==0) {
rootView = inflater.inflate(R.layout.boton2,
container, false);
}else{
rootView = inflater.inflate(R.layout.vacio,
container, false);
}
}
return rootView;
}
public Fragment2(){
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("BOTON", boton);
}
}
En la actividad:
public class Principal extends AppCompatActivity implements Fragment1.Callbacks {
private boolean doble = false;
private String boton="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
boton = savedInstanceState.getString("BOTON");
}
setContentView(R.layout.fragments);
if (findViewById(R.id.fragment2_container) != null)
doble = true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
public void onBoton1Selected(View v) {
if (doble == false) {
Intent intent = new Intent(this, Activity.class);
intent.putExtra("BOTON","boton1");
startActivity(intent);
return;
}
Fragment2 fragment=Fragment2.newInstance("boton1");
getSupportFragmentManager().beginTransaction().replace(R.id.frag2, fragment).commit();
}
public void onBoton2Selected(View v) {
if (doble == false) {
Intent intent = new Intent(this, Activity.class);
intent.putExtra("BOTON","boton2");
startActivity(intent);
return;
}
Fragment2 fragment=Fragment2.newInstance("boton2");
getSupportFragmentManager().beginTransaction().replace(R.id.frag2, fragment).commit();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("BOTON", boton);
}
}
LogCat del error:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{es.user.prueba/es.user.Principal}:
android.view.InflateException: Binary XML file line #22:
Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
at
android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:2832)
at android.app.ActivityThread.access$1600(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #22: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:587)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at es.user.Principal.onCreate(Principal.java:20)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:2832)
at android.app.ActivityThread.access$1600(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: Fragment es.user.Fragment1 did not create a view.
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2314)
at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:111)
at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:314)
at android.support.v4.app.BaseFragmentActivityDonut.onCreateView(BaseFragmentActivityDonut.java:44)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:79)
at android.support.v7.app.AppCompatDelegateImplV7.callActivityOnCreateView(AppCompatDelegateImplV7.java:998)
at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:986)
at android.support.v4.view.LayoutInflaterCompatBase$FactoryWrapper.onCreateView(LayoutInflaterCompatBase.java:36)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at es.user.Principal.onCreate(Principal.java:20)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:2832)
at android.app.ActivityThread.access$1600(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)