-1

嗨,我已经在 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;
            }

我想在我的活动中将数据库中表的内容显示到列表视图中, 任何人都可以帮助我

4

2 回答 2

2

您正在访问loginDataBaseAdapter而没有初始化它

   loginDataBaseAdapter = new ....
   loginDataBaseAdapter.getfriends();
于 2013-08-23T10:20:27.913 回答
0

在 onCreate 里面添加下面的行

  LoginDataBaseAdapter loginDataBaseAdapter = new LoginDataBaseAdapter(this); 
于 2013-08-23T10:31:17.623 回答