我对android很新,所以通过参考教程我带来了一个应用程序。我试图从数据库查询数据并将其显示在自定义列表视图中。但它给我一个NUllPointer异常并且我无法调试它,请帮助我。发布代码片段。 MainActivity.java
public class MainActivity extends Activity implements OnClickListener {
Button add, delete;
ListView lv;
DataHandler enter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
add = (Button) findViewById(R.id.btn_add);
delete = (Button) findViewById(R.id.btn_delete);
add.setOnClickListener(this);
//opening DB
enter.open();
showList();
enter.close();
}
private void showList() {
// TODO Auto-generated method stub
ArrayList<MenuListItems> MenuList = new ArrayList<MenuListItems>();
// MenuList.clear();
Cursor c1 = enter.getAllRows();
if (c1 != null) {
if (c1.moveToFirst()) {
do {
MenuListItems menuListItems = new MenuListItems();
menuListItems.setSlno(c1.getString(c1.getColumnIndex("_id")));
menuListItems.setTitle(c1.getString(c1.getColumnIndex("title")));
menuListItems.setNote(c1.getString(c1.getColumnIndex("note")));
menuListItems.setPhone(c1.getString(c1.getColumnIndex("phone_number")));
MenuList.add(menuListItems);
} while (c1.moveToNext());
}
}
c1.close();
MenuListAdapter menuListAdapter = new MenuListAdapter(MainActivity.this, MenuList);
lv.setAdapter(menuListAdapter);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.btn_add :
Intent next = new Intent("com.testing.callreminder.ADDITEM");
startActivity(next);
break;
}
}}
List Adapter class (custom )
public class MenuListAdapter extends BaseAdapter {
Context c;
ArrayList<MenuListItems> menuList;
public MenuListAdapter(Context context, ArrayList<MenuListItems> list){
c=context;
menuList=list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return menuList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return menuList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
MenuListItems menuListItems = menuList.get(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) c
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.customlist, parent, false);
}
TextView title = (TextView) convertView.findViewById(R.id.tvtitle);
title.setText(menuListItems.getTitle());
TextView phone = (TextView) convertView.findViewById(R.id.tvpnumber);
phone.setText(menuListItems.getPhone());
return convertView;
}
}
MenuList.Java
package com.testing.callreminder;
public class MenuListItems {
String _id;
String title;
String note;
String phone_number;
public String getSlno() {
return _id;
}
public void setSlno(String id) {
this._id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public String getPhone() {
return phone_number;
}
public void setPhone(String number) {
this.phone_number = number;
}
}
list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tvnumber"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Title :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="15dp"
android:text="Phone :"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/tvtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView2"
android:layout_toRightOf="@+id/textView2"
android:text="TextView" />
<TextView
android:id="@+id/tvpnumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_alignLeft="@+id/tvtitle"
android:layout_marginLeft="10dp"
android:text="TextView" />
</RelativeLayout>
mainactivity.xml中的Listview
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:background="@color/white"
android:layout_height="wrap_content"
android:layout_above="@+id/footer"
android:layout_below="@+id/custombar" >
</ListView>
logcat的 `
12-22 22:01:41.353: E/AndroidRuntime(6614): FATAL EXCEPTION: main
12-22 22:01:41.353: E/AndroidRuntime(6614): Process: com.testing.callreminder, PID: 6614
12-22 22:01:41.353: E/AndroidRuntime(6614): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.testing.callreminder/com.testing.callreminder.MainActivity}: java.lang.NullPointerException
12-22 22:01:41.353: E/AndroidRuntime(6614): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
12-22 22:01:41.353: E/AndroidRuntime(6614): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
12-22 22:01:41.353: E/AndroidRuntime(6614): at android.app.ActivityThread.access$900(ActivityThread.java:161)
12-22 22:01:41.353: E/AndroidRuntime(6614): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
12-22 22:01:41.353: E/AndroidRuntime(6614): at android.os.Handler.dispatchMessage(Handler.java:102)
12-22 22:01:41.353: E/AndroidRuntime(6614): at android.os.Looper.loop(Looper.java:157)
12-22 22:01:41.353: E/AndroidRuntime(6614): at android.app.ActivityThread.main(ActivityThread.java:5356)
12-22 22:01:41.353: E/AndroidRuntime(6614): at java.lang.reflect.Method.invokeNative(Native Method)
12-22 22:01:41.353: E/AndroidRuntime(6614): at java.lang.reflect.Method.invoke(Method.java:515)
12-22 22:01:41.353: E/AndroidRuntime(6614): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
12-22 22:01:41.353: E/AndroidRuntime(6614): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
12-22 22:01:41.353: E/AndroidRuntime(6614): at dalvik.system.NativeStart.main(Native Method)
12-22 22:01:41.353: E/AndroidRuntime(6614): Caused by: java.lang.NullPointerException
12-22 22:01:41.353: E/AndroidRuntime(6614): at com.testing.callreminder.MainActivity.onCreate(MainActivity.java:34)
12-22 22:01:41.353: E/AndroidRuntime(6614): at android.app.Activity.performCreate(Activity.java:5426)
12-22 22:01:41.353: E/AndroidRuntime(6614): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
12-22 22:01:41.353: E/AndroidRuntime(6614): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
12-22 22:01:41.353: E/AndroidRuntime(6614): ... 11 more
`
我知道它太久了,但我不知道我哪里出错了。请帮忙。
答案 0 :(得分:0)
您的问题出现在onCreate
//opening DB
enter.open();
showList();
enter.close();
您永远不会初始化DataHandler
实例enter
。
这是你的NPE的原因
为了将来参考,一个非常简单的调试练习是读取堆栈跟踪以找出程序失败的位置。
例如,阅读您发布的跟踪:
12-22 22:01:41.353: E/AndroidRuntime(6614): Caused by: java.lang.NullPointerException
12-22 22:01:41.353: E/AndroidRuntime(6614): at com.testing.callreminder.MainActivity.onCreate(MainActivity.java:34) <--- right here is the line that's throwing the exception
12-22 22:01:41.353: E/AndroidRuntime(6614): at android.app.Activity.performCreate(Activity.java:5426)
12-22 22:01:41.353: E/AndroidRuntime(6614): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
12-22 22:01:41.353: E/AndroidRuntime(6614): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
12-22 22:01:41.353: E/AndroidRuntime(6614): ... 11 more
您可以从第二行看到抛出异常的类,方法和行号。当你遇到问题时,这将是一个很好的开始寻找的地方。
答案 1 :(得分:0)
问题在第34行。您正在尝试使用仅enter
的{{1}},但您忘记初始化它。