Calling an Activity with Fragments from another Activity

1

I have tried to make a simple example for tablet.

What I do is: I create a Activity and by means of a button I call the Activity 2 . This Activity 2 contains two fragments, but when invoking it by means of the button, I get several errors and I do not know what is due.

I enclose the code that the logcat sends me:

AndroidRuntime: FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity    ComponentInfo{com.example.ivan.teto/com.example.ivan.teto.ActividadListaOpciones}: android.view.InflateException: Binary XML file line #38: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2070)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2095)
at android.app.ActivityThread.access$600(ActivityThread.java:134)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1203)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4823)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #38: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.ivan.teto.ActividadListaOpciones.onCreate(ActividadListaOpciones.java:11)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2034)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2095) 
at android.app.ActivityThread.access$600(ActivityThread.java:134) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1203) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:4823) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554) 
at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.IllegalStateException: Fragment com.example.ivan.teto.ListaOpciones did not create a view.
at android.app.Activity.onCreateView(Activity.java:4687)
at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:34)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:79)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280) 
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
at com.example.ivan.teto.ActividadListaOpciones.onCreate(ActividadListaOpciones.java:11) 
at android.app.Activity.performCreate(Activity.java:5008) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2034) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2095) 
at android.app.ActivityThread.access$600(ActivityThread.java:134) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1203) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:4823) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511)
atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554) 
at dalvik.system.NativeStart.main(Native Method) 

This is the source code of Activity that has conflict:

public class ActividadListaOpciones extends AppCompatActivity {

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

Layout of ActividadListaOpciones :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    tools:context=".ActividadListaOpciones"
    android:weightSum="100" >

    <fragment
        android:id="@+id/lista_opciones"
        android:name="com.example.ivan.teto.ListaOpciones"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="30"
        tools:layout="@layout/listaopciones"
        class="com.example.ivan.teto.ListaOpciones" />

    <fragment
        android:id="@+id/lista_estudiante"
        android:name="com.example.ivan.teto.ListaEstudiante"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="70"
        tools:layout="@layout/listaestudiante"
        class="com.example.ivan.teto.ListaEstudiante" />

</LinearLayout>

Activity ListaEstudiante (Fragment 1):

public class ListaEstudiante extends Fragment {
    View rootView;

    public View onCreaView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        rootView = inflater.inflate(R.layout.listaestudiante, container, false);

        return rootView;
    }
}

Layout of Fragment 1:

<?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="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="Hector, Jazmin, Santiago" />

</LinearLayout>

Activity ListaOpciones.java (Fragment 2):

public class ListaOpciones extends Fragment {
        View rootView;
    public View onCreaView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
            rootView =inflater.inflate(R.layout.listaopciones,container,false);
        return rootView;
    }
}

Layout Fragment 2:

<?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="horizontal">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hola Mundo"
        android:textSize="40sp"/>

</LinearLayout>

File Manifest.xml :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ivan.teto">

<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">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".ActividadListaOpciones"
        android:label="@string/menu_opciones">
    <intent-filter>
        <action android:name="android.intent.action.ACTIVIDADLISTAOPCIONES" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    </activity>

</application>

</manifest>
    
asked by Hector Valencia 09.06.2016 в 05:49
source

1 answer

1

Looking at your code, I realized that you have poorly written key methods and I suppose it would be the copy / paste when entering the code, but just in case, I want you to take it into account.

In your java code of the fragments, you call a method called onCreaView() and that method does not exist. You have cast yourself by typing the name of the method, because you type onCreateView() .

Fixing that little clue the app will work without problems.

I tried your code before answering because I thought the error was caused by another code, but changing that, it did not give me any problems when executing.

Look to see if it is that and if it is, calm, the confusions like these happen to us all, if not, leave me a comment to try to help you.

Good luck with your app !!

    
answered by 14.06.2016 / 12:17
source