【问题标题】:java.lang.NullPointerException: 'void androidx.recyclerview.widget.RecyclerView.setLayoutManager on a null object reference [duplicate]java.lang.NullPointerException:'空对象引用上的 'void androidx.recyclerview.widget.RecyclerView.setLayoutManager [重复]
【发布时间】:2022-01-17 11:35:20
【问题描述】:

我有一个从 SQL 读取数据的应用程序,我想在回收站视图中显示。但我有一个问题原因是此警告无法在列表中显示数据:

W/System.err: java.lang.NullPointerException: 尝试在空对象引用上调用虚拟方法“void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)”

public class Remain extends AppCompatActivity {
RecyclerView recyclerViewMain;
ArrayList<ProducModel> producModels;
private Context mContext;
ProductAdapter productAdapter;
Connection connect;
ResultSet rs;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_remain);

    connect = DBHelpers.GetConnection();
   
    SeachProduct();

    recyclerViewMain = findViewById(R.id.ProductSearch_lv2);

}

private void SeachProduct() {

    //internet checking
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    assert cm != null;
    NetworkInfo ni = cm.getActiveNetworkInfo();
    if (ni == null) {
        Toast.makeText(Remain.this, "please check the internet", Toast.LENGTH_LONG).show();
    } else {

        String query = "exec [Android].[spAgentINV] " + ID +",''"; 

        //Connect to SQL server and read data
        try {

            Statement statement = connect.createStatement();
            rs = statement.executeQuery(query);
            producModels = null;
            producModels = new ArrayList<ProducModel>();

            while (rs.next()) {
                producModels.add(new ProducModel(Integer.parseInt(rs.getString("PartRef")) ,5 ,(int)(rs.getFloat("Rate")), (int) (rs.getFloat("finalQty")) , 1, rs.getString("PartName"),0, false));
              

 }

            productAdapter = new ProductAdapter(mContext, producModels);
            recyclerViewMain.setLayoutManager(new LinearLayoutManager(mContext));
            recyclerViewMain.setAdapter(productAdapter);

         
        } catch (Exception e) {
            Toast.makeText(Remain.this, e + "", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }

    }

}

请帮忙

【问题讨论】:

    标签: android android-studio


    【解决方案1】:

    一开始引用RecyclerView,开头是null,不能调用它的任何方法

    recyclerViewMain = findViewById(R.id.ProductSearch_lv2);
    

    在对其进行操作之后,因为它不再是null,所以调用

    SeachProduct();
    

    总结一下:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.activity_remain);
        recyclerViewMain = findViewById(R.id.ProductSearch_lv2);
    
        connect = DBHelpers.GetConnection();
       
        SeachProduct();
    }
    

    【讨论】:

      【解决方案2】:

      变量recyclerViewMain在使用前没有赋值。

      你应该在调用 SeachProduct 方法之前初始化 recyclerViewMain

      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_remain);
          recyclerViewMain = findViewById(R.id.ProductSearch_lv2);
          connect = DBHelpers.GetConnection();
          SeachProduct();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-30
        • 2015-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多