我有一个电影应用程序,其中一个名为“正在显示”的列表项将打开一个新活动,以加载从我的Firebase电影数据库中拍摄的电影列表。但是,当我按“现在显示”时,该应用程序将崩溃。 这是我的密码
public class NowShowing extends AppCompatActivity {
RecyclerView movieRecyclerView;
FirebaseDatabase movieFirebaseDatabase;
DatabaseReference moviesRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_now_showing);
//recyclerview
movieRecyclerView = findViewById(R.id.listnowshowingmovies);
movieRecyclerView.setAdapter(adapter);
movieRecyclerView.setLayoutManager(new GridLayoutManager(this, 1));
//send query to Firebasedatabase
movieFirebaseDatabase = FirebaseDatabase.getInstance();
moviesRef = movieFirebaseDatabase.getReference("Data");
}
//load data into recyclerview onstart
FirebaseRecyclerOptions <movies> options =
new FirebaseRecyclerOptions.Builder<movies>().setQuery(moviesRef, movies.class).build();
FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<movies, ViewHolder>( options ) {
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.movies_cardview_all,parent,false);
return new ViewHolder(view);
}
@Override
protected void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull movies model) {
holder.setDetails(getApplicationContext(), model.getMovieTitle(),model.getMovieThumbnail(),model.getMovieSynopsis(),model.getMovieReleaseDate(),model.getMovieGenre(),
model.getMovieCast(),model.getMovieAgeRestriction(),model.getMovieRating());
}
};
}
这是我得到的错误
ava.lang.RuntimeException:无法启动活动ComponentInfo {com.example.ethan.premovies / com.example.ethan.premovies.NowShowing}:java.lang.NullPointerException:尝试调用虚拟方法'void android.support .v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView $ Adapter)'在空对象引用上 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3254) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350) 在android.app.ActivityThread.access $ 1100(ActivityThread.java:222) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1795) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:158) 在android.app.ActivityThread.main(ActivityThread.java:7229) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1230) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 引起原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView $ Adapter)' 在com.example.ethan.premovies.NowShowing.onCreate(NowShowing.java:30) 在android.app.Activity.performCreate(Activity.java:6876) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3207) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350) 在android.app.ActivityThread.access $ 1100(ActivityThread.java:222) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1795) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:158) 在android.app.ActivityThread.main(ActivityThread.java:7229) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1230) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
我非常需要帮助,因为我仍然需要进行审核。我只是一个初学者,我才知道这一点。我的学校不费心教我们,所以我不得不找出自己。我也在寻找一些人来指导我参加Firebase。谢谢!
答案 0 :(得分:1)
movieRecyclerView.setAdapter(adapter);
adapter调用时为null。 它需要使用FirebaseRecyclerAdapter将提供的信息实例化,但是从提供的代码中看,它就像适配器是在您尝试调用它之后创建的。
答案 1 :(得分:0)
RecyclerView movieRecyclerView;
FirebaseDatabase movieFirebaseDatabase;
DatabaseReference moviesRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_now_showing);
//recyclerview
movieRecyclerView = findViewById(R.id.listnowshowingmovies);
movieRecyclerView.setAdapter(adapter);
movieRecyclerView.setLayoutManager(new GridLayoutManager(this, 1));
//send query to Firebasedatabase
movieFirebaseDatabase = FirebaseDatabase.getInstance();
moviesRef = movieFirebaseDatabase.getReference("Data");
//load data into recyclerview in onCreate
FirebaseRecyclerOptions <movies> options =
new FirebaseRecyclerOptions.Builder<movies>().setQuery(moviesRef, movies.class).build();
FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<movies, ViewHolder>( options ) {
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.movies_cardview_all,parent,false);
return new ViewHolder(view);
}
@Override
protected void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull movies model) {
holder.setDetails(getApplicationContext(), model.getMovieTitle(),model.getMovieThumbnail(),model.getMovieSynopsis(),model.getMovieReleaseDate(),model.getMovieGenre(),
model.getMovieCast(),model.getMovieAgeRestriction(),model.getMovieRating());
}
});
}
按照上面的步骤在onCreate()中移动代码。