0
まず、私はRoboguiceを使用しています。Roboguiceビューインジェクション:RecyclerViewアダプタでカスタムビューをインジェクトすると失敗します
とActivity
があります。 RecyclerView's
アダプター内にカスタムビューを展開しようとしています。私のプロジェクトでRoboguice
を使用しています。
MainActivity.java
@ContentView(R.layout.activity_main)
public class MainActivity extends AppCompatActivity {
@InjectView(R.id.recycler_view) RecyclerView recyclerView;
private UserAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<User> users = fetchUsers();
mAdapter = new UserAdapter(this,users);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(mAdapter);
}
UserAdapter.java
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.CustomViewHolder> {
@Inject CustomView.Provider myViewProvider;
private List<User> userList;
private Context mContext;
public UserAdapter(Context context, List<User> userList) {
this.userList = userList;
this.mContext = context;
}
@Override
public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
View view = myViewProvider.get(parent);
CustomViewHolder viewHolder = new CustomViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(CustomViewHolder customViewHolder, int position) {
((CustomViewHolder) holder).init(userList.get(position));
}
@Override
public int getItemCount() {
return (null != userList ? userList.size() : 0);
}
public class CustomViewHolder extends RecyclerView.ViewHolder {
private final CustomView myView;
public CustomViewHolder(View itemView) {
super(itemView);
myView = (CustomView) itemView;
}
public void init(final User user) {
myView.init(user);
}
}
}
CustomView.java
@ProvidedBy(CustomView.Provider.class)
public class CustomView extends RelativeLayout {
private View caret;
private TextView questionText;
public CustomView(Context context) {
super(context);
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
caret = findViewById(R.id.view_caret);
questionText = (TextView) findViewById(R.id.view_question);
}
public CustomView init(final User user) {
this.user = user;
//Set view
return this;
}
public static class Provider extends ViewProvider<CustomView> {
@Inject
public Provider(LayoutInflaterWithInjection<CustomView> inflater) {
super(R.layout.module_user, inflater);
}
}
}
私はこれを実行する際に、アプリがクラッシュします。 ログ - 私は私のCustomViewが正しく初期化されていないためだと思い
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View com.user.CustomView$Provider.get(android.view.ViewGroup)' on a null object reference
at com.user.UserAdapter.onCreateViewHolder(UserAdapter.java:356)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5228)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4453)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4363)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1961)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1370)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1333)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:562)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2900)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3071)
at android.view.View.layout(View.java:16678)
。誰かが助けることができますか?ありがとう!!
うわー!!それは完璧に働いた。どうもありがとう !! – user1001