mercredi 2 décembre 2015

ListView.set.adapter cause NullPointerException

I'm trying to retrieve SQLite value and load to listView MainActivity. I'm follow this tutorial.

But when my app just get started, it crashed and shows Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference .

It seems like I not yet initialized the listView.setAdapter.I checked the tutorial and still cannot figure out what did I missed here.

Activity 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);
    }

}

Error Logcat

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 ComponentInfo{com.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)

This two line where code refer to BuildList(); and listView.setAdapter(dataAdapter); I know it is a common mistake and soon will be marked as duplicated, but I really can't figure out. Can someone please help me ? Thanks you

Aucun commentaire:

Enregistrer un commentaire