本文介绍了运行时错误,当活动开始(约ListView的活动)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此活动是由db.select获得价值andthen我把一些值列表视图模型显示
。而我不知道,如何修复它。如果您想了解更多信息,请告诉
这是我的runtimeError编辑时intialize
This activity is receive value from db.select andthen i put some value to show in the listview model.And i don't know ,how to repair it.If you want more information please tellThis is my runtimeError edit when intialize
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;
}
}
}
感谢您
推荐答案
请确保您以这种方式初始化都ArrayList和DB ..
Please make sure that your initialized both ArrayList and DB in this way..
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();
}
这篇关于运行时错误,当活动开始(约ListView的活动)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!