当我尝试运行应用程序时,我在LogCat中收到此错误。
java.lang.IllegalArgumentException:Context不能为null。
当我在班级中添加毕加索代码时会发生这种情况。
这里是adpater.Java
它说Context是null,我已阅读其他帖子但我找不到解决方案。
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder>{
private List<App> mApps;
private boolean mHorizontal;
private boolean mPager;
private Context mContext;
public Adapter(Context ctx, boolean horizontal, boolean pager, List<App> apps) {
mHorizontal = horizontal;
mApps = apps;
mPager = pager;
this.mContext=ctx;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (mPager) {
return new ViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.adaper_pager, parent, false));
} else {
return mHorizontal ? new ViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.adapter, parent, false)) :
new ViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.adapter_vertical, parent, false));
}
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
App app = mApps.get(position);
//holder.imageView.setImageResource(app.getDrawable());
Picasso.with(mContext).load(app.getLink()).into(holder.imageView);
holder.nameTextView.setText(app.getName());
holder.ratingTextView.setText(String.valueOf(app.getRating()));
}
@Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
@Override
public int getItemCount() {
return mApps.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public ImageView imageView;
public TextView nameTextView;
public TextView ratingTextView;
public ViewHolder(View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.igOne);
nameTextView = (TextView) itemView.findViewById(R.id.nameTV);
ratingTextView = (TextView) itemView.findViewById(R.id.rate);
}
}
}
请帮助。有人说这是因为imageView为null。我根本不知道。
MainActivity
public class MainActivity extends AppCompatActivity implements Toolbar.OnMenuItemClickListener {
private static final String TAG = "MainActivity";
private Context mContext = MainActivity.this;
private static final int ACTIVITY_NUM = 0;
public static final String ORIENTATION = "orientation";
private RecyclerView mRecyclerView;
private boolean mHorizontal;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "OnCreate: Starting MainActivity");
setupBottomNavigationView();
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setHasFixedSize(true);
if (savedInstanceState == null) {
mHorizontal = true;
} else {
mHorizontal = savedInstanceState.getBoolean(ORIENTATION);
}
setupAdapter();
}
/**
* Setup Bottom Navigation View
*/
private void setupBottomNavigationView() {
Log.d(TAG, "setupBottomNavigationView: Setting up Bottom Navigation View");
BottomNavigationViewEx bottomNavigationViewEx = (BottomNavigationViewEx) findViewById(R.id.bottom_nav_bar);
BottomNavigationViewHelper.setupBottomNavigationView(bottomNavigationViewEx);
BottomNavigationViewHelper.enableNavigation(mContext, bottomNavigationViewEx);
Menu menu = bottomNavigationViewEx.getMenu();
MenuItem menuItem = menu.getItem(ACTIVITY_NUM);
menuItem.setChecked(true);
}
public void click(View v) {
Intent mIntent = null;
switch (v.getId()) {
case R.id.serve:
mIntent = new Intent(this, SearchActivity.class);
break;
case R.id.imageButton2:
mIntent = new Intent(this, SearchActivity.class);
break;
}
startActivity(mIntent);
}
///////////
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(ORIENTATION, mHorizontal);
}
private void setupAdapter() {
List<App> apps = getApps();
SnapAdapter snapAdapter = new SnapAdapter();
if (mHorizontal) {
snapAdapter.addSnap(new Snap(Gravity.CENTER_HORIZONTAL, "Snap Start", apps));
snapAdapter.addSnap(new Snap(Gravity.START, "Snap Middle", apps));
snapAdapter.addSnap(new Snap(Gravity.END, "Snap End", apps));
snapAdapter.addSnap(new Snap(Gravity.CENTER, "Pager snap", apps));
} else {
snapAdapter.addSnap(new Snap(Gravity.CENTER_VERTICAL, "Snap center", apps));
snapAdapter.addSnap(new Snap(Gravity.TOP, "Snap top", apps));
snapAdapter.addSnap(new Snap(Gravity.BOTTOM, "Snap bottom", apps));
}
mRecyclerView.setAdapter(snapAdapter);
}
private List<App> getApps() {
List<App> apps = new ArrayList<>();
apps.add(new App("Google+", "http://uupload.ir/files/aud7_brickone.jpg", 4.6f));
return apps;
}
@Override
public boolean onMenuItemClick(MenuItem item) {
return false;
}
}
SnapAdapter
public class SnapAdapter extends RecyclerView.Adapter<SnapAdapter.ViewHolder> implements GravitySnapHelper.SnapListener {
public static final int VERTICAL = 0;
public static final int HORIZONTAL = 1;
private Context mContext;
private ArrayList<Snap> mSnaps;
// Disable touch detection for parent recyclerView if we use vertical nested recyclerViews
private View.OnTouchListener mTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
};
public SnapAdapter() {
mSnaps = new ArrayList<>();
}
public void addSnap(Snap snap) {
mSnaps.add(snap);
}
@Override
public int getItemViewType(int position) {
Snap snap = mSnaps.get(position);
switch (snap.getGravity()) {
case Gravity.CENTER_VERTICAL:
return VERTICAL;
case Gravity.CENTER_HORIZONTAL:
return HORIZONTAL;
case Gravity.START:
return HORIZONTAL;
case Gravity.TOP:
return VERTICAL;
case Gravity.END:
return HORIZONTAL;
case Gravity.BOTTOM:
return VERTICAL;
}
return HORIZONTAL;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = viewType == VERTICAL ? LayoutInflater.from(parent.getContext())
.inflate(R.layout.adapter_snap_vertical, parent, false)
: LayoutInflater.from(parent.getContext())
.inflate(R.layout.adapter_snap, parent, false);
if (viewType == VERTICAL) {
view.findViewById(R.id.recycle_view).setOnTouchListener(mTouchListener);
}
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Snap snap = mSnaps.get(position);
holder.snapTextView.setText(snap.getText());
if (snap.getGravity() == Gravity.START || snap.getGravity() == Gravity.END) {
holder.recyclerView.setLayoutManager(new LinearLayoutManager(holder
.recyclerView.getContext(), LinearLayoutManager.HORIZONTAL, false));
holder.recyclerView.setOnFlingListener(null);
new GravitySnapHelper(snap.getGravity(), false, this).attachToRecyclerView(holder.recyclerView);
} else if (snap.getGravity() == Gravity.CENTER_HORIZONTAL) {
holder.recyclerView.setLayoutManager(new LinearLayoutManager(holder
.recyclerView.getContext(), snap.getGravity() == Gravity.CENTER_HORIZONTAL ?
LinearLayoutManager.HORIZONTAL : LinearLayoutManager.VERTICAL, false));
holder.recyclerView.setOnFlingListener(null);
new LinearSnapHelper().attachToRecyclerView(holder.recyclerView);
} else if (snap.getGravity() == Gravity.CENTER) { // Pager snap
holder.recyclerView.setLayoutManager(new LinearLayoutManager(holder
.recyclerView.getContext(), LinearLayoutManager.HORIZONTAL, false));
holder.recyclerView.setOnFlingListener(null);
new PagerSnapHelper().attachToRecyclerView(holder.recyclerView);
} else { // Top / Bottom
holder.recyclerView.setLayoutManager(new LinearLayoutManager(holder
.recyclerView.getContext()));
holder.recyclerView.setOnFlingListener(null);
new GravitySnapHelper(snap.getGravity()).attachToRecyclerView(holder.recyclerView);
}
holder.recyclerView.setAdapter(new Adapter(mContext, snap.getGravity() == Gravity.START
|| snap.getGravity() == Gravity.END
|| snap.getGravity() == Gravity.CENTER_HORIZONTAL,
snap.getGravity() == Gravity.CENTER, snap.getApps()));
}
@Override
public int getItemCount() {
return mSnaps.size();
}
@Override
public void onSnap(int position) {
Log.d("Snapped: ", position + "");
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView snapTextView;
public RecyclerView recyclerView;
public ViewHolder(View itemView) {
super(itemView);
snapTextView = (TextView) itemView.findViewById(R.id.snapTextView);
recyclerView = (RecyclerView) itemView.findViewById(R.id.recycle_view);
}
}
}
答案 0 :(得分:2)
java.lang.IllegalArgumentException: Context must not be null.
Java中的构造函数是一个类似于方法的代码块 在创建对象的实例时调用。
您应该通过Context
private Context mContext;
public Adapter(Context ctx,boolean horizontal, boolean pager, List<App> apps) {
mHorizontal = horizontal;
mApps = apps;
mPager = pager;
this.mContext=ctx; // Call here
}
Logcat抛出
错误是:错误:(110,40)错误:类中的构造函数适配器 适配器不能应用于给定类型;需要: boolean,boolean,List,Context found:boolean,boolean,List 原因:实际和正式的参数列表长度不同
通过价值
new Adapter(Your_Activity.this, boolean, boolean , List<App>);
答案 1 :(得分:2)
像这样调用适配器
Adapter mAdapter = new Adapter (Youractivity_name.this, boolean, boolean , list);
并将构造函数更改为
public Adapter(Context context ,boolean horizontal, boolean pager, List<App> apps) {
mHorizontal = horizontal;
mApps = apps;
mPager = pager;
mContext = context;
}