错误:错误导致类片段

时间:2016-04-16 02:28:05

标签: java android xml android-fragments android-studio

我对Fragments很新,但我已经搜索了这个错误了很长时间,并且不知道会出现什么问题。试过很多固定的我在网上发现,都没有用。

错误的callstack的开始如下,但我对该问题的理解是当我的FollowerListActivity将自己链接到其XML文件时。在XML文件中,它会在片段中抱怨。也许我没有正确处理碎片?我之前从未与他们合作,但我密切关注了在线教程。

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)

我现在尝试列出所有相关文件。很抱歉列出这么多,但除了我上面提到的错误之外,我不知道问题可能是什么。

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());
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

尝试使用_id。检查这是否有助于你