1

我想檢索SQLite值並加載到listView MainActivity。我遵循這個tutorialListView.set.adapter cause NullPointerException

但是當我的應用程序剛剛開始時,它崩潰並顯示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); 
    } 

} 

錯誤的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) 

這兩個線路,其中的代碼是指BuildList();listView.setAdapter(dataAdapter);我知道這是一個常見的錯誤和很快將被標記爲重複,但我真的不知道。有人可以幫幫我嗎 ?謝謝你

回答

5

在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(); 

} 
+1

我做了什麼愚蠢的錯誤。非常感謝 !! – Tony

+2

很高興幫助你.. :) –

相關問題