2

I'm very new to Fragments, but I've searched this error for a long time and have no idea what could be going wrong. Tried many fixed I found online, none worked.

The start of the callstack of the error is below, but my understanding of the issue is when my FollowerListActivity links itself to its XML file. In the XML file, it complains at the fragment. Perhaps I'm not handling the fragments correctly? I've never worked with them before, but I followed online tutorials closely.

04-15 13:59:18.317 15059-15059/xyz.mydomain.socialmaps E/AndroidRuntime: FATAL EXCEPTION: main
Process: xyz.mydomain.socialmaps, PID: 15059
java.lang.RuntimeException: Unable to start activity ComponentInfo{xyz.mydomain.socialmaps/xyz.mydomain.socialmaps.activities.followersSystem.FollowingListActivity}: android.view.InflateException: Binary XML file line #18: Binary XML file line #18: 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 #18: Binary XML file line #18: 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:276)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:136)
at xyz.mydomain.socialmaps.activities.followersSystem.FollowingListActivity.onCreate(FollowingListActivity.java:41)

I'll now try to list all relevant files. Sorry for listing so much, but other than where I've pointed to with the error above, I have zero idea what the issue possibly could be.

activity_follower_list.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    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"
    tools:context=".activities.followersSystem.FollowingListActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Follower"
        android:id="@+id/follower_button"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"/>

    <fragment
        class ="xyz.mydomain.socialmaps.fragments.ListOfUsersFragment"
        android:id="@+id/list_of_follewrs_fragment"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:layout_below="@+id/follower_button"
        android:layout_alignParentStart="true"
        android:layout_marginTop="10dp"
        android:layout_alignEnd="@+id/follower_button" />

</RelativeLayout>

fragment_list_of_users.xml

<FrameLayout 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:theme="@style/LoginTheme"
    tools:context="xyz.jhughes.socialmaps.fragments.ListOfUsersFragment">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rvUsers"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>

FollowerListActivity.java

package xyz.mydomain.socialmaps.activities.followersSystem;

import android.app.FragmentTransaction;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import objects.Command;
import objects.Response;
import objects.User;
import xyz.mydomain.socialmaps.R;
import xyz.mydomain.socialmaps.enums.TypeOfUserList;
import xyz.mydomain.socialmaps.fragments.ListOfUsersFragment;
import xyz.mydomain.socialmaps.fragments.ListOfUsersFragment.OnFragmentInteractionListener;
import xyz.mydomain.socialmaps.helpers.Alert;
import xyz.mydomain.socialmaps.server.Server;

public class FollowingListActivity extends AppCompatActivity implements ListOfUsersFragment.OnFragmentInteractionListener {



    private ListView listOfFollowers;
    private ArrayAdapter listOfFollowersAdapter;
    private ArrayList<String> followersList = new ArrayList<>();

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

        Button follower_button = (Button) findViewById(R.id.follower_button);
        follower_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getApplicationContext(), AddNewFollowerActivity.class));
            }
        });

        Fragment newFragment = ListOfUsersFragment.newInstance(TypeOfUserList.FOLLOWING,
                getSharedPreferences("xyz.jhughes.socialmaps", MODE_PRIVATE).getString("username", null));
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.list_of_follewrs_fragment, newFragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }

    @Override
    public void onFragmentInteraction(String username) {
        // TODO
    }
}

ListOfUsersFragment.java

package xyz.mydomain.socialmaps.fragments;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Fragment;

// import android.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

import objects.Command;
import objects.Response;
import objects.User;
import xyz.mydomain.socialmaps.R;
import xyz.mydomain.socialmaps.adapters.UsersAdapter;
import xyz.mydomain.socialmaps.enums.TypeOfUserList;
import xyz.mydomain.socialmaps.helpers.Alert;
import xyz.mydomain.socialmaps.server.Server;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link ListOfUsersFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link ListOfUsersFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ListOfUsersFragment extends Fragment {

    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "typeOfUserList";
    private static final String ARG_PARAM2 = "primaryUsername";

    private TypeOfUserList type;
    private String primaryUsername;

    private OnFragmentInteractionListener mListener;
    private ArrayList<User> listOfUsers;

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

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param type The type of list that should be displayed.
     * @return A new instance of fragment ListOfUsersFragment.
     */
    public static ListOfUsersFragment newInstance(TypeOfUserList type, String username) {
        ListOfUsersFragment fragment = new ListOfUsersFragment();
        Bundle args = new Bundle();
        switch(type) {
            case FOLLOWERS:
                args.putInt(ARG_PARAM1, 0);
                break;
            case FOLLOWING:
                args.putInt(ARG_PARAM1, 1);
                break;
            case NEW_FOLLOWER:
                args.putInt(ARG_PARAM1, 2);
                break;
        }
        args.putString(ARG_PARAM2, username);
        fragment.setArguments(args);
        return fragment;
    }

    private TypeOfUserList intToType(int i) {
        switch(i) {
            case 0:
                return TypeOfUserList.FOLLOWERS;
            case 1:
                return TypeOfUserList.FOLLOWING;
            case 2:
                return TypeOfUserList.NEW_FOLLOWER;
            default:
                throw new RuntimeException("Improper conversion from int to type.");
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.type = intToType(getArguments().getInt(ARG_PARAM1)); // may throw null pointer, but idc
        this.primaryUsername = getArguments().getString(ARG_PARAM2);
        switch (this.type) {
            case FOLLOWERS:
                new GetListOfUsersTask().execute(Command.CommandType.GET_USERS_THAT_FOLLOW_USER);
                break;
            case FOLLOWING:
                new GetListOfUsersTask().execute(Command.CommandType.GET_USERS_THAT_USER_FOLLOWS);
                break;
            case NEW_FOLLOWER:
                new GetListOfUsersTask().execute(Command.CommandType.GET_USERS_THAT_BEGIN_WITH_SUBSTRING);
                break;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_list_of_users, container, false);
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void gotoUserActivity(String username) {
        if (mListener != null) {
            mListener.onFragmentInteraction(username);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    private void updateListOfUsers(ArrayList<User> listOfUsers) {
        RecyclerView rvUsers = (RecyclerView) this.getView().findViewById(R.id.rvUsers);
        UsersAdapter adapter = new UsersAdapter(listOfUsers);
        rvUsers.setAdapter(adapter);
        rvUsers.setLayoutManager(new LinearLayoutManager(this.getContext()));
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // originally Uri, change back if things are fucked up
        void onFragmentInteraction(String username);
    }

    private class GetListOfUsersTask extends AsyncTask<Command.CommandType, Void, Response> {

        protected Response doInBackground(Command.CommandType... commandType) {
            User user = new User(null, ListOfUsersFragment.this.primaryUsername, null, null, null, null, null);
            Command command = new Command(commandType[0], null, user, null, null);
            return Server.sendCommandToServer(command, getContext());
        }

        protected void onPostExecute(Response response) {
            try {
                if (response == null)
                    Alert.networkError(getContext());
                else if(!response.isBool())
                    Alert.generalError(getContext());
                else if (response.getListOfUsers().size() == 0)
                    Alert.createAlertDialog("Follows", "You don't follow anyone. Try following someone!", getContext());
                else {
                    ArrayList<User> users = response.getListOfUsers();
                    updateListOfUsers(users);
                }
            } catch (NullPointerException e) {
                Alert.generalError(getContext());
            }
        }
    }
}
USER9561
  • 1,084
  • 3
  • 15
  • 41
CaptainForge
  • 1,365
  • 7
  • 21
  • 46

1 Answers1

0

Try using the import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager;. check if this helps you

Shaan_B
  • 1,788
  • 2
  • 12
  • 16