Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java android NullPointer异常错误自定义列表视图_Java_Android_Eclipse - Fatal编程技术网

Java android NullPointer异常错误自定义列表视图

Java android NullPointer异常错误自定义列表视图,java,android,eclipse,Java,Android,Eclipse,我是android新手,所以通过参考教程,我附带了一个应用程序。我试图从数据库中查询数据并在自定义列表视图中显示它。但是它抛出了一个空指针异常,我无法调试它,请帮助我。发布代码片段。 MainActivity.java public class MainActivity extends Activity implements OnClickListener { Button add, delete; ListView lv; DataHandler enter; @Override prote

我是android新手,所以通过参考教程,我附带了一个应用程序。我试图从数据库中查询数据并在自定义列表视图中显示它。但是它抛出了一个空指针异常,我无法调试它,请帮助我。发布代码片段。 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;
    }

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


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

您的问题是
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
12-22:01:41.353:E/AndroidRuntime(6614):由以下原因引起:java.lang.NullPointerException

12-22:01:41.353:E/AndroidRuntime(6614):在com.testing.callrementer.MainActivity.onCreate(MainActivity.java:34)上问题与第34行有关。您正在尝试使用
enter
,这是唯一的声明:
DataHandler-enter,但您忘记初始化它。

您的日志太小。实际上,我看到了一个
NullPointerException
,但我没有看到它的跟踪,这是stacktrace的唯一用途。请把它全部贴出来!发布的整个日志问题位于文件
MainActivity.java
,行
34
。在你的代码上显示那一行(例如,通过编辑你的文章作为代码注释)。
我无法调试它
为什么不能?@PavanKumar
menuListItems.setSlno(c1.getString(c1.getColumnIndex(“\u id”))这是您的
第34行吗,如果不是,请突出显示它好吗?我已初始化,但结果相同:(enter=new DataHandler(此);是我所做的更改。@PavanKumar没问题。您现在可以使用堆栈跟踪进一步了解程序失败的原因,即使在尝试初始化处理程序之后也是如此。感谢您的评论。我已成功调试,应用程序现在运行良好。
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
//opening DB 
enter.open();
showList();
enter.close(); 
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