2017-05-25 83 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(); 
    } 
相關問題