尝试调用虚方法' java.lang.String android.content.Context.getPackageName()'在空对象引用上
我知道这个问题在这里问了很多次,但我无法解决这个问题 我是android新手,我无法解决这个问题
我想点击卡片视图并开始新的活动 我为此编写代码,但问题是当我点击cardview时出现此错误
请告诉我如何解决这个问题
以下是我的代码
Maia活动
public class MainActivity extends AppCompatActivity {
private List<Data> movieList = new ArrayList<>();
private RecyclerView recyclerView;
private MoviesAdapter mAdapter;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mAdapter = new MoviesAdapter(movieList,context) ;
context = getApplicationContext();
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
prepareMovieData();
}
private void prepareMovieData() {
Data data = new Data("MadMax");
movieList.add(data);
data = new Data("MadMax1");
movieList.add(data);
data = new Data("MadMax2");
movieList.add(data);
data = new Data("MadMax3");
movieList.add(data);
data = new Data("MadMax4");
movieList.add(data);
mAdapter.notifyDataSetChanged();
}
}
适配器
public class MoviesAdapter extends RecyclerView.Adapter<MoviesAdapter.MyViewHolder> {
private List<Data> moviesList;
private Context context;
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.single_row_layout, parent, false);
return new MyViewHolder(itemView);
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
public TextView title;
public View view;
public MyViewHolder(View v) {
super(v);
view = v;
title = (TextView) view.findViewById(R.id.title);
}
}
public MoviesAdapter(List<Data> moviesList,Context context) {
this.moviesList = moviesList;
this.context = context;
}
@Override
public void onBindViewHolder(MyViewHolder holder,int position) {
Data data = moviesList.get(position);
holder.title.setText(data.getTitle());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context,Parts.class);
context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return moviesList.size();
}
}
logcat的
02-25 18:05:43.121 11930-11930 / com.example.owner.theconstitutionofindia E / AndroidRuntime: 致命异议:主要 过程:com.example.owner.theconstitutionofindia,PID:11930 java.lang.NullPointerException:尝试调用虚方法 &#39; java.lang.String android.content.Context.getPackageName()&#39;在null 对象参考 在android.content.ComponentName。(ComponentName.java:77) 在android.content.Intent。(Intent.java:4170) 在 com.example.owner.theconstitutionofindia.MoviesAdapter $ 1.onClick(MoviesAdapter.java:86) 在android.view.View.performClick(View.java:4785) 在android.view.View $ PerformClick.run(View.java:19884) 在android.os.Handler.handleCallback(Handler.java:739) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:135) 在android.app.ActivityThread.main(ActivityThread.java:5343) at java.lang.reflect.Method.invoke(Native Method) 在java.lang.reflect.Method.invoke(Method.java:372) 在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:905) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
请告诉我如何解决此问题以及如何通过卡片视图开始新活动。
答案 0 :(得分:2)
试试这个
context=MainActivity.this
mAdapter = new MoviesAdapter(movieList,context) ;
答案 1 :(得分:0)
在您的代码中
mAdapter = new MoviesAdapter(movieList,context) ;
context = getApplicationContext();
您在初始化之前使用context
。
做到这一点
context = getApplicationContext();
mAdapter = new MoviesAdapter(movieList,context) ;
同时制作
context = getApplicationContext();
到
context = MainActivity.this;
答案 2 :(得分:0)
首先检查两个文件是否在同一个包中,并将活动也传递给适配器。不要只传递上下文并将其投射到活动中。
public MoviesAdapter(List<Data> moviesList,Context context, Activity activity) {
this.moviesList = moviesList;
this.context = context;
this,activity = activity;
}
这种方法总是不起作用;
context = ActivityName.this;