質問

I am trying to make a custom list which is getting data from database and has two text views but its giving NPE and I am not getting why is it so and banged too much head.Please help Here is my code:

public class NotesList extends ListActivity{
    private DataSource datasource;
     ArrayAdapter<Message> adapter;
     CustomList adap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list);

       //  List<Message> values = datasource.getAllMessages();
     //  adapter = new ArrayAdapter<Message>(this, android.R.layout.simple_list_item_1, values);
    //  setListAdapter(adapter);
      adap = new CustomList();

      setListAdapter(adap);



    }
    @Override
    public void onResume() {
        datasource.open();

        super.onResume();
      }


      @Override
    public void onPause() {
        datasource.close();

        super.onPause();

      }
      public class CustomList extends BaseAdapter {

            List<Message> DataList = datasource.getAllMessages();
            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return DataList.size();
            }

            @Override
            public Message getItem(int arg0) {
                // TODO Auto-generated method stub
                return DataList.get(arg0);
            }

            @Override
            public long getItemId(int arg0) {
                // TODO Auto-generated method stub
                return arg0;
            }

            @Override
            public View getView(int arg0, View arg1, ViewGroup arg2) {

                if(arg1==null)
                {
                    LayoutInflater inflater = (LayoutInflater) NotesList.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    arg1 = inflater.inflate(R.layout.listitem, arg2,false);
                }

                TextView heading = (TextView)arg1.findViewById(R.id.textView1);
                TextView notes = (TextView)arg1.findViewById(R.id.textView2);

                Message MessageObj = DataList.get(arg0);

                heading.setText(MessageObj.getmessage());
                notes.setText(MessageObj.getmessage());

                return arg1;
            }

            public Message getCodeLearnChapter(int position)
            {
                return DataList.get(position);
            }

        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
         //   getMenuInflater().inflate(R.menu.list_view_with_simple_adapter, menu);
            return true;
        }

}

Logcat is:

03-19 07:24:00.936: E/AndroidRuntime(28816): FATAL EXCEPTION: main
03-19 07:24:00.936: E/AndroidRuntime(28816): java.lang.RuntimeException: Unable to start activity ComponentInfo{soft.b.notes/soft.b.notes.NotesList}: java.lang.NullPointerException
03-19 07:24:00.936: E/AndroidRuntime(28816):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
03-19 07:24:00.936: E/AndroidRuntime(28816):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
03-19 07:24:00.936: E/AndroidRuntime(28816):    at android.app.ActivityThread.access$600(ActivityThread.java:122)
03-19 07:24:00.936: E/AndroidRuntime(28816):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
03-19 07:24:00.936: E/AndroidRuntime(28816):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-19 07:24:00.936: E/AndroidRuntime(28816):    at android.os.Looper.loop(Looper.java:137)
03-19 07:24:00.936: E/AndroidRuntime(28816):    at android.app.ActivityThread.main(ActivityThread.java:4340)
03-19 07:24:00.936: E/AndroidRuntime(28816):    at java.lang.reflect.Method.invokeNative(Native Method)
03-19 07:24:00.936: E/AndroidRuntime(28816):    at java.lang.reflect.Method.invoke(Method.java:511)
03-19 07:24:00.936: E/AndroidRuntime(28816):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-19 07:24:00.936: E/AndroidRuntime(28816):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)

Regards !

役に立ちましたか?

解決

you never initialize datasource and invoking datasource.getAllMessages() cause the NPE

他のヒント

The NPE can be found below in your logcat, with helpful stacktrace to diagnose it.

One way to explain the NPE is that

List<Message> DataList = datasource.getAllMessages();

calls a method on datasource which is not initialized when you call new CustomList().

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top