ListView.set.adapter 导致 NullPointerException

Posted

技术标签:

【中文标题】ListView.set.adapter 导致 NullPointerException【英文标题】:ListView.set.adapter cause NullPointerException 【发布时间】:2016-03-07 16:47:51 【问题描述】:

我正在尝试检索 SQLite 值并加载到 listView MainActivity。我关注这个tutorial。

但是当我的应用刚启动时,它崩溃并显示Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference

似乎我还没有初始化listView.setAdapter。我查看了教程,但仍然无法弄清楚我在这里错过了什么。

活动 A

public class MainActivity extends AppCompatActivity 

    InfoAPI sqlcon;
    private SimpleCursorAdapter dataAdapter;
    private SQLiteDatabase database;
    private MyDatabaseHelper dbHelper;
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        listView=(ListView)findViewById(R.id.listView2);
        setContentView(R.layout.activity_main);
        dbHelper = new MyDatabaseHelper(this);
        sqlcon = new InfoAPI(this);
        BuildList();

    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
        // Inflate the menu; this adds items to the action bar if it is present.
        MenuInflater inflater=getMenuInflater();
        inflater.inflate(R.menu.create_menu, menu);
        return true;
    

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        switch(item.getItemId()) 
            case R.id.add: // create new file
                View menuItemView = findViewById(R.id.add);
                PopupMenu po = new PopupMenu(MainActivity.this, menuItemView); //for drop-down menu
                po.getMenuInflater().inflate(R.menu.popup_menu, po.getMenu());
                po.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() 
                    public boolean onMenuItemClick(MenuItem item) 
                        Toast.makeText(MainActivity.this, "You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show();
                        if ("Create New File".equals(item.getTitle()) ) 
                            Intent intent = new Intent(MainActivity.this, Information.class);  // go to Information class
                            startActivity(intent);

                        
                        return true;
                    
                );
                po.show(); //showing popup menu
        
        return super.onOptionsItemSelected(item);
    

    public void BuildList()
    
        sqlcon.open();
        Cursor cursor1=sqlcon.readData();

        String[] columns=new String[]
        MyDatabaseHelper.Weather,MyDatabaseHelper.Date,MyDatabaseHelper.Status,MyDatabaseHelper.TimeIn_Info,MyDatabaseHelper.TimeOut_Info
        ;

        int[] to=new int[]
         
          R.id.weather, R.id.date,R.id.status,R.id.in,R.id.out
         ;

        dataAdapter = new SimpleCursorAdapter(this, R.layout.retrieve_data,
                cursor1,
                columns,
                to,
                0);

        listView.setAdapter(dataAdapter);
    


错误日志

12-03 05:53:49.622    2588-2588/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.project.myapplication, PID: 2588
    java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.project.myapplication/com.example.project.myapplication.MainActivity: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)

这两行代码引用BuildList();listView.setAdapter(dataAdapter); 我知道这是一个常见的错误,很快就会被标记为重复,但我真的想不通。有人可以帮帮我吗 ?谢谢你

【问题讨论】:

【参考方案1】:

在 setContentView() 之后初始化 listview。

改变你的 onCreate() 方法:

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listView=(ListView)findViewById(R.id.listView2);
    dbHelper = new MyDatabaseHelper(this);
    sqlcon = new InfoAPI(this);
    BuildList();


【讨论】:

以上是关于ListView.set.adapter 导致 NullPointerException的主要内容,如果未能解决你的问题,请参考以下文章

python 数组拆分:N个分裂点,导致N + 1个子阵列

std::bitset<N> 实现导致大小被偷听

System.getProperty("line.separator") 和 "\n" 导致列表视图不填充

EF Core 嵌套 Linq 选择导致 N + 1 个 SQL 查询

什么会导致算法具有 O(log n) 复杂度?

GPU 上的计算导致驱动程序错误“停止响应”