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 と 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): 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 に答える