2016-04-16 61 views
2

我很新Fragments,但我已经搜索了很长一段时间的错误,不知道可能会出错。尝试了很多修复我在网上找到,没有工作。错误:错误充气课程片段

错误的调用堆栈的开始位置在下方,但我对这个问题的理解是当我的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()); 
      } 
     } 
    } 
} 
+0

[在此](http://stackoverflow.com/questions/4512981/android-view-inflateexception-binary-xml-file-line-12-error-inflating-class)出现在我的首先谷歌搜索。 – randominstanceOfLivingThing

回答

0

尝试使用import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager;。检查,如果这可以帮助你