我有一个nullpointer异常

时间:2013-08-23 10:18:56

标签: android sqlite listview

嗨我在Android中创建了一个应用程序,但在运行时我得到一个空指针异常

我在下面给我的log cat和java类我需要一个更好的解决方案来克服这个错误

08-23 10:09:57.426: E/AndroidRuntime(3373): FATAL EXCEPTION: main

08-23 10:09:57.426: E/AndroidRuntime(3373): java.lang.RuntimeException: Unable to start
activity ComponentInfo{com.neochat/com.neochat.Friends_list}:java.lang.NullPointerException

08-23 10:09:57.426: E/AndroidRuntime(3373):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)

08-23 10:09:57.426: E/AndroidRuntime(3373):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:2023)

08-23 10:09:57.426: E/AndroidRuntime(3373):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)`

这是我的java类

          public class Friends_list extends Activity implements OnItemClickListener{


     private ArrayList<String> results = new ArrayList<String>();



     private SQLiteDatabase newDB;
      LoginDataBaseAdapter loginDataBaseAdapter ;

        ListView listview;
            Context context;





       @Override
       protected void onCreate(Bundle savedInstanceState) {



    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_friends);
    listview=(ListView)findViewById(R.id.listview);




    context=this;
    loginDataBaseAdapter.getfriends();


        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) 
        {

            Intent in2=new   
                      Intent(Friends_list.this,InboxActivity.class);
            startActivity(in2);
        }



                 }

我在

下面提供了我的getFriends()函数
              public List<String>getfriends(){
    List<String> FriendList=new ArrayList<String>();
    String selectQuery="select * from   UserDetails";
    db=dbHelper.getWritableDatabase();
    Cursor cursor=db.rawQuery(selectQuery, null);
    while(cursor.moveToNext()){
        FriendList.add(cursor.getString(1));
    }
    return FriendList;
            }

我想将数据库中表格的内容显示到我的活动中的列表视图可以有人帮助我

2 个答案:

答案 0 :(得分:2)

您正在访问loginDataBaseAdapter而未初始化

   loginDataBaseAdapter = new ....
   loginDataBaseAdapter.getfriends();

答案 1 :(得分:0)

在onCreate里面添加以下行

  LoginDataBaseAdapter loginDataBaseAdapter = new LoginDataBaseAdapter(this);