Error with Fragments

Asked

Viewed 247 times

0

I am beginner in Android programming and I’m having an error that I can’t fix it.

I’m doing a project that uses Fragments. I have a small layout that works perfectly when I don’t put the Fragment component, whereas when I put it, the parameter savedInstanceState of onCreate() is void.

XML Main

<LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".Main"
    android:background="#ff25947a">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3">

        <ListView
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:id="@+id/listView"
            android:clickable="true" />
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginLeft="30dp"
        android:layout_weight="7"
        android:id="@+id/layout_direito">


        <fragment
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:name="com.br.estudar.Fragment1"
            android:id="@+id/fragment"
            android:layout_gravity="center_horizontal"
            tools:layout="@layout/layout_fragment_1" />
    </LinearLayout>
</LinearLayout>

XML layout of 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"
        android:background="#ff586abe">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/textView"
            android:textColor="#ffffff"
            android:textSize="30dp" />

    </LinearLayout>

JAVA MAIN

    package com.br.estudar;

    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.FragmentManager;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;

    public class Main extends FragmentActivity {

        FragmentManager fm = this.getSupportFragmentManager();
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            String [] lista = new String[]{"Fragment 1", "Altera Texto", "Fragment 2", "Fragment 3"};

            ArrayAdapter<String> adapter = new  ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lista);

            ListView lv = (ListView)findViewById(R.id.listView);
            lv.setAdapter(adapter);

            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    if(position == 1)
                    {
                        Fragment1 frag = (Fragment1) fm.findFragmentById(R.id.fragment);
                        frag.setText("Frag1 - texto alterado");
                    }
                }
            });


  }
}

JAVA Fragment1

 package com.br.estudar;

    import android.graphics.Color;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;


    public class Fragment1 extends Fragment {

        public View onCreateView(LayoutInflater inflater, ViewGroup conteiner, Bundle savedInstanceState)
        {
             View view = inflater.inflate(R.layout.layout_fragment_1, null);
             this.setText("Fragment 1");

            return view;
        }

        public void setText(String texto)
        {
            TextView tv = (TextView) this.getView().findViewById(R.id.textView);
            tv.setText(texto);

            this.getView().setBackgroundColor(Color.rgb(25, 61, 36));
        }
    }

MANIFEST

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".Main">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

The error that occurs is as follows:

12-30 16:27:08.517 15362-15362/com.br.estudar E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.br.estudar.Main.access$super
12-30 16:27:08.517 15362-15362/com.br.estudar E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.br.estudar.Main.access$super
12-30 16:27:08.517 15362-15362/com.br.estudar E/dalvikvm: Could not find class 'android.media.session.MediaController', referenced from method com.br.estudar.Main.access$super
12-30 16:27:08.517 15362-15362/com.br.estudar E/dalvikvm: Could not find class 'android.widget.Toolbar', referenced from method com.br.estudar.Main.access$super
12-30 16:27:08.517 15362-15362/com.br.estudar E/dalvikvm: Could not find class 'android.app.ActivityManager$TaskDescription', referenced from method com.br.estudar.Main.access$super
12-30 16:27:08.527 15362-15362/com.br.estudar E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method com.br.estudar.Main.access$super
12-30 16:27:08.527 15362-15362/com.br.estudar E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.br.estudar.Main.access$super
12-30 16:27:08.527 15362-15362/com.br.estudar E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method com.br.estudar.Main.access$super
12-30 16:27:08.537 15362-15362/com.br.estudar E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.br.estudar.Main.access$super
12-30 16:27:08.577 15362-15362/com.br.estudar E/AndroidRuntime: FATAL EXCEPTION: main
                                                                Process: com.br.estudar, PID: 15362
                                                                java.lang.RuntimeException: Unable to start activity ComponentInfo{com.br.estudar/com.br.estudar.Main}: android.view.InflateException: Binary XML file line #34: Error inflating class Fragment
                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2318)
                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)
                                                                    at android.app.ActivityThread.access$800(ActivityThread.java:139)
                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293)
                                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                    at android.os.Looper.loop(Looper.java:149)
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5257)
                                                                    at java.lang.reflect.Method.invokeNative(Native Method)
                                                                    at java.lang.reflect.Method.invoke(Method.java:515)
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
                                                                    at dalvik.system.NativeStart.main(Native Method)
                                                                 Caused by: android.view.InflateException: Binary XML file line #34: Error inflating class Fragment
                                                                    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:707)
                                                                    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
                                                                    at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
                                                                    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
                                                                    at android.app.Activity.setContentView(Activity.java:2002)
                                                                    at com.br.estudar.Main.onCreate(Main.java:18)
                                                                    at android.app.Activity.performCreate(Activity.java:5411)
                                                                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396) 
                                                                    at android.app.ActivityThread.access$800(ActivityThread.java:139) 
                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293) 
                                                                    at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                    at android.os.Looper.loop(Looper.java:149) 
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5257) 
                                                                    at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                    at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609) 
                                                                    at dalvik.system.NativeStart.main(Native Method) 
                                                                 Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.Fragment" on path: DexPathList[[zip file "/data/app/com.br.estudar-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.br.estudar-2, /vendor/lib, /system/lib, /system/lib/arm]]
                                                                    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                                                                    at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
                                                                    at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
                                                                    at android.view.LayoutInflater.createView(LayoutInflater.java:559)
                                                                    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:652)
                                                                    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
                                                                    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
                                                                    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
                                                                    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 
                                                                    at android.view.LayoutInflater.rInflate(LayoutInflater.java:758) 
                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
                                                                    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290) 
                                                                    at android.app.Activity.setContentView(Activity.java:2002) 
                                                                    at com.br.estudar.Main.onCreate(Main.java:18) 
                                                                    at android.app.Activity.performCreate(Activity.java:5411) 
                                                                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270) 
                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396) 
                                                                    at android.app.ActivityThread.access$800(ActivityThread.java:139) 
                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293) 
                                                                    at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                    at android.os.Looper.loop(Looper.java:149) 
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5257) 
                                                                    at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                    at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609) 
                                                                    at dalvik.system.NativeStart.main(Native Method) 

If you could give me a hand please, thank you in advance.

  • What is the error that occurs?

  • What do you mean by "savedInstanceState() from onCreate() is null"?. Note that in XML Main the attribute shall be Fragment and not fragment

  • Good afternoon ramaral, first sorry for the error, it is not savedInstanceState() (a method), but savedInstanceState (a parameter of the onCreate() method). I’ve changed in mine XML Main the fragment for Fragment and kept making mistakes. What happens is the following, when the application calls the method onCreate the parameter savedInstanceState is already null, which causes several errors. Why this is happening is that I don’t know, and detail that only occurs when I add the tag <Fragment>. Dener I changed the post by adding the error.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.