Fragment error "Unable to start activity"

-1

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

  

Unable to start activity   ComponentInfo {com.example.everton.horoscope / 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 following the code if someone 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();
        }
    }
}

o 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) 
    
asked by anonymous 09.10.2016 / 22:57

3 answers

2

I'm sorry the delay in answering is that you had no time to go. I 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();

Initially I passed the Bundle through main but later I created a method and I passed 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 the help.

    
22.10.2016 / 16:26
0

In your .xml this is R.id.fragmentDetail and in your class you call R.id.frag_detalhe . Try to correctly check and place the identifiers because they have different ids .

Another possible error is in the .xml in which you define the name of your fragment.

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

Try changing to class.

class="com.example.everton.horoscopo.fragments.HoroscopoListFragment"
    
10.10.2016 / 15:23
0

Hello, Everton

Let's look at the excerpt below where you inflate the fragment in your activity:

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

This wrap indicates that you are relating an instance of HoroscopoDetailFragment to the visual component that has the frag_detalhe id, 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 another with the id fragmentDetail , but I can not see someone with the id frag_detalhe . What is happening is that the code is trying to inflate a visual component with an id that does not exist.

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

Embrace

    
10.10.2016 / 15:33