有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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;
    }

}

美努利主义者。爪哇

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;
         }
}

名单。xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    安卓:id="@+id/tvnumber"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent" >

    <TextView
        安卓:id="@+id/textView1"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:layout_alignParentLeft="true"
        安卓:layout_alignParentTop="true"
        安卓:text="Title :"
        安卓:textAppearance="?安卓:attr/textAppearanceMedium" />

    <TextView
        安卓:id="@+id/textView2"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:layout_alignParentLeft="true"
        安卓:layout_below="@+id/textView1"
        安卓:layout_marginTop="15dp"
        安卓:text="Phone :"
        安卓:textAppearance="?安卓:attr/textAppearanceSmall" />

    <TextView
        安卓:id="@+id/tvtitle"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:layout_above="@+id/textView2"
        安卓:layout_toRightOf="@+id/textView2"
        安卓:text="TextView" />

    <TextView
        安卓:id="@+id/tvpnumber"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:layout_alignBaseline="@+id/textView2"
        安卓:layout_alignBottom="@+id/textView2"
        安卓:layout_alignLeft="@+id/tvtitle"
        安卓:layout_marginLeft="10dp"
        安卓:text="TextView" />

</RelativeLayout>

mainactivity中的Listview。xml

<ListView
    安卓:id="@+id/listView1"
    安卓:layout_width="match_parent"
    安卓:background="@color/white"
    安卓:layout_height="wrap_content"
    安卓:layout_above="@+id/footer"
    安卓: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 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
12-22 22:01:41.353: E/AndroidRuntime(6614):     at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
12-22 22:01:41.353: E/AndroidRuntime(6614):     at 安卓.app.ActivityThread.access$900(ActivityThread.java:161)
12-22 22:01:41.353: E/AndroidRuntime(6614):     at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
12-22 22:01:41.353: E/AndroidRuntime(6614):     at 安卓.os.Handler.dispatchMessage(Handler.java:102)
12-22 22:01:41.353: E/AndroidRuntime(6614):     at 安卓.os.Looper.loop(Looper.java:157)
12-22 22:01:41.353: E/AndroidRuntime(6614):     at 安卓.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.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
12-22 22:01:41.353: E/AndroidRuntime(6614):     at com.安卓.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 安卓.app.Activity.performCreate(Activity.java:5426)
12-22 22:01:41.353: E/AndroidRuntime(6614):     at 安卓.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
12-22 22:01:41.353: E/AndroidRuntime(6614):     at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
12-22 22:01:41.353: E/AndroidRuntime(6614):     ... 11 more

`

我知道它太长了,但我不知道我哪里出错了。请帮忙


共 (2) 个答案

  1. # 1 楼答案

    问题是第34行。您正试图使用enter,它只是声明:DataHandler enter;,但您忘记了初始化它

  2. # 2 楼答案

    您的问题是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
    

    从第二行可以看到引发异常的类、方法和行号。当你遇到问题时,那将是一个开始寻找的好地方