0

此活动是从 db.select 接收值,然后我将一些值显示在列表视图模型中。我不知道如何修复它。如果您想了解更多信息,请告诉这是我的 runtimeError 在初始化时编辑

08-21 21:23:36.047: E/AndroidRuntime(408): FATAL EXCEPTION: main
08-21 21:23:36.047: E/AndroidRuntime(408): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sqlite/com.example.sqlite.FriendsListActivity}: java.lang.NullPointerException
08-21 21:23:36.047: E/AndroidRuntime(408):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-21 21:23:36.047: E/AndroidRuntime(408):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-21 21:23:36.047: E/AndroidRuntime(408):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-21 21:23:36.047: E/AndroidRuntime(408):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-21 21:23:36.047: E/AndroidRuntime(408):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-21 21:23:36.047: E/AndroidRuntime(408):  at android.os.Looper.loop(Looper.java:123)
08-21 21:23:36.047: E/AndroidRuntime(408):  at android.app.ActivityThread.main(ActivityThread.java:3683)
08-21 21:23:36.047: E/AndroidRuntime(408):  at java.lang.reflect.Method.invokeNative(Native Method)
08-21 21:23:36.047: E/AndroidRuntime(408):  at java.lang.reflect.Method.invoke(Method.java:507)
08-21 21:23:36.047: E/AndroidRuntime(408):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-21 21:23:36.047: E/AndroidRuntime(408):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-21 21:23:36.047: E/AndroidRuntime(408):  at dalvik.system.NativeStart.main(Native Method)
08-21 21:23:36.047: E/AndroidRuntime(408): Caused by: java.lang.NullPointerException
08-21 21:23:36.047: E/AndroidRuntime(408):  at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
08-21 21:23:36.047: E/AndroidRuntime(408):  at com.example.sqlite.db.FriendsDB.<init>(FriendsDB.java:19)
08-21 21:23:36.047: E/AndroidRuntime(408):  at com.example.sqlite.FriendsListActivity.onCreate(FriendsListActivity.java:34)
08-21 21:23:36.047: E/AndroidRuntime(408):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-21 21:23:36.047: E/AndroidRuntime(408):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-21 21:23:36.047: E/AndroidRuntime(408):  ... 11 more

这是我的活动

package com.example.sqlite;

import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.sqlite.db.FriendsDB;
import com.example.sqlite.entry.FriendEntry;

public class FriendsListActivity extends Activity {
private Context context;
private FriendsDB db;
private ArrayList<FriendEntry> friends;
private TextView hellotext;
private ListView hellolistview;
ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.friendlist_layout);
            db = new FriendsDB(context);
    friends = new ArrayList<FriendEntry>(); 
    showAllList();
}
public void showAllList(){
    //view matching
    hellotext = (TextView) findViewById(R.id.hellotext);
    hellolistview = (ListView) findViewById(R.id.hellolistview);
    //select data
    friends = db.selectAll();
        if(friends.size()==0){
            Toast.makeText(context,"You dont have any friend.",Toast.LENGTH_SHORT).show();
        }else{

            for (int i = 1;i<=friends.size();i++){
            // set value for friends    
                map = new HashMap<String, String>();
                map.put("item_nickname", friends.get(i).getNickname());
                map.put("item_fname", friends.get(i).getFname());
                map.put("item_lname", friends.get(i).getLname());
                MyArrList.add(map);
                Log.i("item_nickname", friends.get(i).getNickname());
                Log.i("item_fname", friends.get(i).getFname());
                Log.i("item_lname", friends.get(i).getLname());
            }

            //adapter
            hellolistview.setAdapter(new adapter());
        }
    }

private class adapter extends BaseAdapter{
    private Holder holder;
    @Override

    public int getCount() {
        // TODO Auto-generated method stub
        return friends.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        //create
        if( view == null){
            view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_layout,null);
            holder = new Holder();
            holder.nickname = (TextView) view.findViewById(R.id.item_nickname);
            holder.fname = (TextView)view.findViewById(R.id.item_fname);
            holder.lname = (TextView)view.findViewById(R.id.item_lname);
            view.setTag(holder);
        }else{
            holder = (Holder) view.getTag();
        }

        //assign data / wait for data 
        holder.nickname.setText(map.get(position));


        return view;
    }

    private class Holder{

        public TextView nickname;
        public TextView fname;
        public TextView lname;
    }
}


}

谢谢

4

3 回答 3

0
 ArrayList<FriendEntry> friends;
private FriendsDB db;

请初始化它

于 2013-08-21T09:41:45.897 回答
0

请确保您以这种方式初始化了 ArrayList 和 DB..

public class FriendsListActivity extends Activity
 {
private Context context;
private FriendsDB db;
private ArrayList<FriendEntry> friends;
private TextView hellotext;
private ListView hellolistview;
ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.friendlist_layout); 
    **friends=new ArrayList<FriendEntry>;
    db=new FriendsDB(FriendsListActivity.this);
    db.getReadableDatabse();** // For reading sqlite database
    showAllList();
}
于 2013-08-21T09:51:38.080 回答
-1

由于 NullPointerException,活动无法启动

引起:java.lang.NullPointerException 08-21 16:15:06.771: E/AndroidRuntime(536): at com.example.sqlite.FriendsListActivity.showAllList(FriendsListActivity.java:41)

所以在第 41 行,导致 NPE 的可能组件可能是 friends.get(i).getFname()

请调试线路

            map.put("item_fname", friends.get(i).getFname());
于 2013-08-21T09:45:01.033 回答