0
votes

I am following the steps of an example in a book and I get several errors when debugging with the emulator:

E/AndroidRuntime: FATAL EXCEPTION: main Process: activity.tasks.dummies.com.tasklistactivity, PID: 2995 java.lang.RuntimeException: Unable to start activity ComponentInfo{activity.tasks.dummies.com.tasklistactivity/activity.tasks.dummies.com.tasklistactivity.TaskListActivity}: android.view.InflateException: Binary XML file line #15: Error inflating class fragment at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) at android.app.ActivityThread.access$800(ActivityThread.java:144) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) Caused by: android.view.InflateException: Binary XML file line #15: Error inflating class fragment at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763) at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) at android.view.LayoutInflater.inflate(LayoutInflater.java:504) at android.view.LayoutInflater.inflate(LayoutInflater.java:414) at android.view.LayoutInflater.inflate(LayoutInflater.java:365) at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377) at android.app.Activity.setContentView(Activity.java:2144) at activity.tasks.dummies.com.tasklistactivity.TaskListActivity.onCreate(TaskListActivity.java:25) at android.app.Activity.performCreate(Activity.java:5937) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)  at android.app.ActivityThread.access$800(ActivityThread.java:144)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:135)  at android.app.ActivityThread.main(ActivityThread.java:5221)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)  Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class activity.tasks.dummies.com.tasklistactivity.TaskListFragment that is not a Fragment at android.app.Fragment.instantiate(Fragment.java:606) at android.app.Fragment.instantiate(Fragment.java:582) at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2108) at android.app.Activity.onCreateView(Activity.java:5286) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733) at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)  at android.view.LayoutInflater.inflate(LayoutInflater.java:504)  at android.view.LayoutInflater.inflate(LayoutInflater.java:414)  at android.view.LayoutInflater.inflate(LayoutInflater.java:365)  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377)  at android.app.Activity.setContentView(Activity.java:2144)  at activity.tasks.dummies.com.tasklistactivity.TaskListActivity.onCreate(TaskListActivity.java:25)  at android.app.Activity.performCreate(Activity.java:5937)  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)  at android.app.ActivityThread.access$800(ActivityThread.java:144)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:135)  at android.app.ActivityThread.main(ActivityThread.java:5221)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)  Caused by: java.lang.ClassCastException at android.app.Fragment.instantiate(Fragment.java:606)  at android.app.Fragment.instantiate(Fragment.java:582)  at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2108)  at android.app.Activity.onCreateView(Activity.java:5286)  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)  at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)  at android.view.LayoutInflater.inflate(LayoutInflater.java:504)  at android.view.LayoutInflater.inflate(LayoutInflater.java:414)  at android.view.LayoutInflater.inflate(LayoutInflater.java:365)  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377)  at android.app.Activity.setContentView(Activity.java:2144)  at activity.tasks.dummies.com.tasklistactivity.TaskListActivity.onCreate(TaskListActivity.java:25)  at android.app.Activity.performCreate(Activity.java:5937)  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)  at android.app.ActivityThread.access$800(ActivityThread.java:144)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:135)  at android.app.ActivityThread.main(ActivityThread.java:5221)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

The related java file is (TaskListFragment.java):

package activity.tasks.dummies.com.tasklistactivity;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView; 


import  activity.tasks.dummies.com.adapter.TaskListAdapter;


public class TaskListFragment extends Fragment
    {

        RecyclerView recyclerView;
        TaskListAdapter adapter;

        public TaskListFragment()
        {
            // Required empty public constructor
        }

        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            adapter = new TaskListAdapter();
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            // Inflate the layout for this fragment
            //return inflater.inflate(R.layout.fragment_task_list, container, false);
            final View v = inflater.inflate(R.layout.fragment_task_list, container, false);
            recyclerView = (RecyclerView) v.findViewById(R.id.recycler);

            recyclerView.setAdapter(adapter);

            recyclerView.setHasFixedSize(true);
            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            return v;
        }

    }

And my XML layout (activity_task_list.xml):

<?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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Toolbar
        style="?android:actionBarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:title="@string/app_name"
        android:id="@+id/toolbar"/>

    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/fragment"
        android:name="activity.tasks.dummies.com.tasklistactivity.TaskListFragment"
        tools:layout="@layout/fragment_task_list"/>
</LinearLayout>

The line 15 that is mentioned in the error is in the xml in < fragment

I'm beginner on Android and I do not know how to fix it.

Can anybody tell me what is wrong?

Thanks

Also there is another class (TaskListActivity.java):

package activity.tasks.dummies.com.tasklistactivity;


import android.app.Activity;
import android.os.Bundle;
import android.widget.Toolbar;

    public class TaskListActivity extends Activity
    {

        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_task_list);
            setActionBar((Toolbar) findViewById(R.id.toolbar));
        }
    }
1
what is your package name?rafsanahmad007
I think you pasted your XML twice, not your Java and XML.PurkkaKoodari
The java file is TaskListFragment.java and xml is fragment_task_list.xmlFabián Romo
XML inflation errors are notoriously nondescript, but a common mistake is mixing up default and support fragments: stackoverflow.com/questions/6424853/…Timo Ohr
I fix the error of pasting xml 2 times in this threadFabián Romo

1 Answers

0
votes

try this;

<fragment
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/fragment"
    class ="activity.tasks.dummies.com.tasklistactivity.TaskListFragment"
    tools:layout="@layout/fragment_task_list"/>

in you Activity class:

    public class TaskListActivity extends FragmentActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task_list);
        setActionBar((Toolbar) findViewById(R.id.toolbar));
    }
}

and import android.support.v4.app.FragmentActivity;