【解決方法】[Resolved] 仮想メソッド recyclerview.setadapter の呼び出しを試みます


こんにちは、

データベースからリサイクラー ビューまでデータを検索したかったのですが、私のコードは以下のとおりですが、20 行目にエラーが表示されます。問題の解決にご協力ください。

Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setAdapter(androidx.recyclerview.widget.RecyclerView$Adapter)' on a null object reference
        at com.nyt.kitaab.lughatulquran.MainActivity.onCreate(MainActivity.java:85)

この行にはエラーがあります:

recyclerView.setLayoutManager(new LinearLayoutManager(this));

前もって感謝します。

申し訳ありませんが、この行を忘れました:

recyclerView = findViewById(R.id.recycler);

私が試したこと:

Java
  1  public class MainActivity extends AppCompatActivity {
  2      EditText searchtext;
  3      RecyclerView recyclerView;
  4      RecyclerView.LayoutManager layoutManager;
  5      MyAdapter adapter;
  6      Database db;
  7      AdView mAdView;
  8      Context context;
  9  
 10      @Override
 11      protected void onCreate(Bundle savedInstanceState) {
 12          super.onCreate(savedInstanceState);
 13          setContentView(R.layout.activity_main);
 14          mAdView = findViewById(R.id.adView);
 15          AdRequest adRequest = new AdRequest.Builder().build();
 16          mAdView.loadAd(adRequest);
 17          db = new Database(this);
 18          List<LughatDetail> lughatList = new ArrayList<>();
 19          searchtext = findViewById(R.id.searchText);
 20          recyclerView.setLayoutManager(new LinearLayoutManager(this));
 21          recyclerView.setHasFixedSize(true);
 22  
 23          searchtext.addTextChangedListener(new TextWatcher() {
 24              @Override
 25              public void beforeTextChanged(CharSequence s, int start, int count, int after) {
 26  
 27              }
 28  
 29              @Override
 30              public void onTextChanged(CharSequence s, int start, int before, int count) {
 31  
 32              }
 33  
 34              @Override
 35              public void afterTextChanged(Editable s) {
 36                  lughatList.clear();
 37                  if(s.toString().isEmpty()){
 38                      // Do something
 39                  }
 40                  else {
 41                      searchLughat(s.toString());
 42                  }
 43              }
 44  
 45              private void searchLughat(String text) {
 46                  for(LughatDetail lugat:lughatList){
 47                      if(lugat.getField2().equals(text)){
 48                          lughatList.add(lugat);
 49                      }
 50                  }
 51                  recyclerView.setAdapter(new MyAdapter(getApplicationContext(),lughatList));
 52                  adapter.notifyDataSetChanged();
 53              }
 54          });
 55  
 56  
 57  
 58      }
 59  
 60      public class SpeedyLinearLayoutManager extends LinearLayoutManager {
 61  
 62          private static final float MILLISECONDS_PER_INCH = 5f; //default is 25f (bigger = slower)
 63  
 64          public SpeedyLinearLayoutManager(Context context) {
 65              super(context);
 66          }
 67  
 68          public SpeedyLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
 69              super(context, orientation, reverseLayout);
 70          }
 71  
 72          public SpeedyLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
 73              super(context, attrs, defStyleAttr, defStyleRes);
 74          }
 75  
 76          @Override
 77          public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
 78  
 79              final LinearSmoothScroller linearSmoothScroller = new LinearSmoothScroller(recyclerView.getContext()) {
 80  
 81                  @Override
 82                  public PointF computeScrollVectorForPosition(int targetPosition) {
 83                      return super.computeScrollVectorForPosition(targetPosition);
 84                  }
 85  
 86                  @Override
 87                  protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
 88                      return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
 89                  }
 90              };
 91  
 92              linearSmoothScroller.setTargetPosition(position);
 93              startSmoothScroll(linearSmoothScroller);
 94          }
 95      }
 96  }

解決策 1

あなたは宣言しました RecyclerView recyclerView; あなたのクラスでは、それを使用しようとする前に初期化していません。 だから今でも null、表示されるエラーが発生します。

コメント

タイトルとURLをコピーしました