Fragment error "Unable to start Activity"

Asked

Viewed 886 times

-1

My error application when opening on a tablet and I can’t identify the error it informs that I have to enable:

Unable to start Activity Componentinfo{com.example.Everton.horoscopo/com.example.Everton.horoscopo.Mainactivity}: android.view.Inflateexception: Binary XML file line #15: Binary XML file line #15: Error inflating class Fragment.

I do not understand the error follow the code if anyone can help me thank you. My main Activity

package com.example.everton.horoscopo;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import com.example.everton.horoscopo.fragments.HoroscopoDetailFragment;

public class MainActivity extends AppCompatActivity implements OnHoroscopoClickLlistener {

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



    private boolean istablet() {
        return getResources().getBoolean(R.bool.tablet);

    }

    private boolean isSmartphone() {
        return getResources().getBoolean(R.bool.smartphone);
    }

    @Override
    public void onMovieClick (long id){
        if (isSmartphone()) {
            Toast.makeText(this, "bbb " + id, Toast.LENGTH_SHORT).show();
            Intent it = new Intent(MainActivity.this, HoroscopoDetailActivity.class);
            it.putExtra("Horosc", String.valueOf(id));
            startActivity(it);

        } else {
            HoroscopoDetailFragment horoscopoDetailFragment = new HoroscopoDetailFragment();
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.frag_detalhe, horoscopoDetailFragment)
                    .commit();
        }
    }
}

the xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.everton.horoscopo.MainActivity">

    <fragment
        android:layout_width="390dp"
        android:layout_height="match_parent"
        android:name="com.example.everton.horoscopo.fragments.HoroscopoListFragment"
        android:id="@+id/fragmentLista"/>

    <fragment
        android:layout_width="409dp"
        android:layout_height="match_parent"
        android:name="com.example.everton.horoscopo.fragments.HoroscopoDetailFragment"
        android:id="@+id/fragmentDetail"/>

</FrameLayout>

and error

10-09 16:50:34.702 23809-23815/? E/art: Failed sending reply to debugger: Broken pipe
10-09 16:50:35.111 23809-23809/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.everton.horoscopo, PID: 23809 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.everton.horoscopo/com.example.everton.horoscopo.MainActivity}: android.view.InflateException: Binary XML file line #15: Binary XML file line #15: Error inflating class fragment
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
   at android.app.ActivityThread.-wrap11(ActivityThread.java)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:148)
   at android.app.ActivityThread.main(ActivityThread.java:5417)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.view.InflateException: Binary XML file line #15: Binary XML file line #15: Error inflating class fragment
   at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
   at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
   at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
   at com.example.everton.horoscopo.MainActivity.onCreate(MainActivity.java:17)
   at android.app.Activity.performCreate(Activity.java:6237)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
   at android.app.ActivityThread.-wrap11(ActivityThread.java) 
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
   at android.os.Handler.dispatchMessage(Handler.java:102) 
   at android.os.Looper.loop(Looper.java:148) 
   at android.app.ActivityThread.main(ActivityThread.java:5417) 
   at java.lang.reflect.Method.invoke(Native Method) 
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: android.view.InflateException: Binary XML file line #15: Error inflating class fragment
   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
   at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
   at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
   at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
   at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280) 
   at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
   at com.example.everton.horoscopo.MainActivity.onCreate(MainActivity.java:17) 
   at android.app.Activity.performCreate(Activity.java:6237) 
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
   at android.app.ActivityThread.-wrap11(ActivityThread.java) 
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
   at android.os.Handler.dispatchMessage(Handler.java:102) 
   at android.os.Looper.loop(Looper.java:148) 
   at android.app.ActivityThread.main(ActivityThread.java:5417) 
   at java.lang.reflect.Method.invoke(Native Method) 
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
   at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
   at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
   at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
   at android.database.CursorWrapper.getString(CursorWrapper.java:137)
   at com.example.everton.horoscopo.fragments.HoroscopoDetailFragment.onCreateView(HoroscopoDetailFragment.java:50)
   at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1036)
   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1230)
   at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1332)
   at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2288)
   at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
   at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:356)
   at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:31)
   at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:79)
   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:754)
   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
   at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
   at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
   at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
   at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
   at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
   at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280) 
   at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
   at com.example.everton.horoscopo.MainActivity.onCreate(MainActivity.java:17) 
   at android.app.Activity.performCreate(Activity.java:6237) 
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
   at android.app.ActivityThread.-wrap11(ActivityThread.java) 
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
   at android.os.Handler.dispatchMessage(Handler.java:102) 
   at android.os.Looper.loop(Looper.java:148) 
   at android.app.ActivityThread.main(ActivityThread.java:5417) 
   at java.lang.reflect.Method.invoke(Native Method) 
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

3 answers

2

Guys, I’m sorry it took me so long to respond. I just solved the problem. The problem was that in main Activity was not passing the Bundle to the class of the Detail Fragment follows the solution:

Main Activity

 frag = findViewById(R.id.fragmentDetail);
           HoroscopoDetailFragment horoscopoDetailFragment= HoroscopoDetailFragment.newInstance(id);
            horoscopoDetailFragment.teste(true);
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.fragmentDetail, horoscopoDetailFragment)
                    .show(horoscopoDetailFragment)
                    .commit();

I initially passed the Bundle by main but then created a method and went through it.

Detailfragment

 public static HoroscopoDetailFragment newInstance(long id) {
    Bundle args = new Bundle();
    args.putString("Horosc", String.valueOf(id));
    HoroscopoDetailFragment horoscopoDetailFragment= new HoroscopoDetailFragment();
    horoscopoDetailFragment.setArguments(args);
    return horoscopoDetailFragment;
}

Thank you all for your help.

0

In his .xml appears in this way R.id.fragmentDetail and in your class you call R.id.frag_detalhe. Try to check and correctly put the identifiers because they are with ids different.

Another possible error is in . xml in which you define your Fragment name.

android:name="com.example.everton.horoscopo.fragments.HoroscopoListFragment"

Try changing to class.

class="com.example.everton.horoscopo.fragments.HoroscopoListFragment"

0

Hello, Everton

Let’s look at the section below where you inflate the Fragment in your Activity:

HoroscopoDetailFragment horoscopoDetailFragment = new HoroscopoDetailFragment();
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.frag_detalhe, horoscopoDetailFragment)
                    .commit();

This indicates that you are relating an instance of HoroscopoDetailFragment on the visual component that has id frag_detalhe, right? Let’s now look at your layout to see where to find this visual component with this id:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.everton.horoscopo.MainActivity">

    <fragment
        android:layout_width="390dp"
        android:layout_height="match_parent"
        android:name="com.example.everton.horoscopo.fragments.HoroscopoListFragment"
        android:id="@+id/fragmentLista"/>

    <fragment
        android:layout_width="409dp"
        android:layout_height="match_parent"
        android:name="com.example.everton.horoscopo.fragments.HoroscopoDetailFragment"
        android:id="@+id/fragmentDetail"/>

</FrameLayout>

I can see two components fragment, one with the id fragmentLista and the other with the id fragmentDetail, but I can’t see anyone with the id frag_detalhe. What’s happening is that the code is trying to inflate a visual component with an id that doesn’t exist.

What you need to do is just adjust the ids, either by modifying in the layout or by modifying in Activity, so that the two refer to the same component with the same id.

Hug

Browser other questions tagged

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