此活动是从db.select接收的值,然后我在listview模型中显示了一些值 。我不知道,如何修复它。如果您想了解更多信息,请告诉我 这是intialize
时的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;
}
}
}
谢谢
答案 0 :(得分:0)
ArrayList<FriendEntry> friends;
private FriendsDB db;
请初始化
答案 1 :(得分: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();
}
答案 2 :(得分:-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());