2017-05-25 84 views
0

我知道这个问题已经被问过一百万次,但我已经尝试了所有可以找到的解决方案,并且仍然没有没有工作。我曾尝试调用“this”作为上下文,我尝试过getActivity,我尝试过getContext(),但是没有看到特别适用于这个片段。相同的代码在不同的片段中工作,这就是为什么我很困惑。任何帮助赞赏。无法解析构造函数ArrayAdapter(android.Content.Context,int,java.util.ArrayList <MyObject>)

我LoginFragment,我的问题可以在setReservations()中找到:

public class LoginFragment extends Fragment { 
    LoginButton loginButton; 
    TextView nameText; 
    ProfileTracker mProfileTracker; 
    DBHandler dbHandler; 
    Context context; 
    ArrayList<Reservation> reservations; 
    ListView lv; 
    Profile fbProfile; 

    CallbackManager callbackManager; 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.login, container, false); 
     callbackManager = CallbackManager.Factory.create(); 
     dbHandler = new DBHandler(getContext(), null, null, 1); 
     context = getActivity(); 
     loginButton = (LoginButton) view.findViewById(R.id.login_button); 
     nameText = (TextView) view.findViewById(R.id.users_name); 
     loginButton.setReadPermissions("email"); 
     setmProfileTracker(); 
     mProfileTracker.startTracking(); 
     // If using in a fragment 
     loginButton.setFragment(this); 
     // Other app specific specialization 

     // Callback registration 
     loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { 
      @Override 
      public void onSuccess(LoginResult loginResult) { 

      } 

      @Override 
      public void onCancel() { 
       // App code 
      } 

      @Override 
      public void onError(FacebookException exception) { 
       // App code 
      } 
     }); 

     return view; 
    } 

    @Override 
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 
     fbProfile = Profile.getCurrentProfile(); 
     if (fbProfile != null) 
     { 
      updateUI(); 
     } 
     getActivity().setTitle("My page"); 
    } 


    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     callbackManager.onActivityResult(requestCode,resultCode,data); 
    } 

    private void setmProfileTracker() 
    { 
     mProfileTracker = new ProfileTracker() { 
      @Override 
      protected void onCurrentProfileChanged(Profile profile, Profile profile2) { 
       updateUI(); //this is the third piece of code I will discuss below 
      } 
     }; 
    } 

    private void updateUI() { 

     boolean enableButtons = AccessToken.getCurrentAccessToken() != null; 
     if (fbProfile == null) { 
      fbProfile = Profile.getCurrentProfile(); 
      Log.e("Profile", "null"); 
     } 
     if (enableButtons && fbProfile != null) { 
      Log.e("Access Token", AccessToken.getCurrentAccessToken().toString()); 
      nameText.setText(fbProfile.getName()); 
     } 
    } 

    private void setReservations() 
    { 
     reservations = dbHandler.userReservationToArrayList(parseLong(fbProfile.getId())); 

     lv = (ListView) getView().findViewById(R.id.reservations); 

     ArrayAdapter<Review> arrayAdapter = new ArrayAdapter<Review>(
      context, 
      android.R.layout.simple_list_item_1, 
      reservations); 

     lv.setAdapter(arrayAdapter); 
    } 
} 

编辑:代码格式

回答

1

你或许应该更多地了解泛型和类型安全。

您的reservationsArrayList<Reservation>,但您尝试创建ArrayAdapter<Review>这是错误的。将其更改为ArrayAdapter<Reservation>

+0

感谢,将尽快接受的答案。 –

2

你的数组适配器类型审查

ArrayAdapter<Review> arrayAdapter 

但你的传球:

ArrayList<Reservation> reservations 
Change it to ArrayAdapter<Reservation> 
0

你应该通过Review不是Reservation数组的数组。因此请将您的适配器更改为ArrayAdapter<Reservation>

此外,在您拨打onCreateView()方法中的getActivity()方法时,您的Activity尚未创建,这可能会导致错误。 为了解决这个问题,你应该给这条线移动到onActivityCreated()

@Override 
    public void onActivityCreated(@Nullable Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     context = getActivity(); 
    } 
相关问题