0

My object is to let the user click on the list view and it will turn to the page to let the user modify the data and then update to the database.

Here is the database reference: https://imgur.com/6Qx10ll

Also,on the item onclick , I want to send the position's data to the modify page. But the error is

java.lang.NullPointerException: Attempt to invoke virtual method 
'java.lang.String java.lang.Object.toString()' on a null object reference
at com.example.fjuim.tronclass.Me.MeTodolist$2$1.onDataChange(MeTodolist.java:89)
                  at com.google.firebase.database.zzp.onDataChange(Unknown Source:7)
                  at com.google.android.gms.internal.to.zza(Unknown Source:13)
                  at com.google.android.gms.internal.vj.zzHX(Unknown Source:2)
                  at com.google.android.gms.internal.vp.run(Unknown Source:65)
                  at android.os.Handler.handleCallback(Handler.java:789)
                  at android.os.Handler.dispatchMessage(Handler.java:98)
                  at android.os.Looper.loop(Looper.java:164)
                  at android.app.ActivityThread.main(ActivityThread.java:6541)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

Me To do list.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_me_todolist);
    Intent intent = getIntent();
    student_id = intent.getStringExtra("student_id");
    student_name = intent.getStringExtra("student_name");
    createTop();
    createDetail();

}
     private void  createDetail() {
  final FirebaseDatabase database = FirebaseDatabase.getInstance();
  final DatabaseReference hom = database.getReference("Student").child(student_id).child("event");
  listview = findViewById(R.id.listview);
  final ArrayAdapter adapter = new ArrayAdapter<String>(this,
          android.R.layout.simple_list_item_1,android.R.id.text1);
  listview.setAdapter(adapter);

  hom.addValueEventListener(new ValueEventListener() {
                                @Override
                                public void onDataChange(DataSnapshot dataSnapshot) {
                                    for (DataSnapshot event : dataSnapshot.getChildren() ) {
                                        Event = event.getValue().toString();
                                        Log.e("Metodolist",Event);
                                        adapter.add(Event);
                                        adapter.notifyDataSetChanged();
                                    }

                                }
                                @Override
                                public void onCancelled(DatabaseError databaseError) {

                                }
                            });
  Log.e("MeTo list","Pass1");

  listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {

       hom.addListenerForSingleValueEvent(new ValueEventListener() {
           @Override
           public void onDataChange(DataSnapshot dataSnapshot) {

               Intent intent = new Intent(MeTodolist.this, MeTodolistModify.class);
               String title = dataSnapshot.child(Integer.toString(position)).getValue().toString();
               Bundle bundle = new Bundle();
               bundle.putString("title", title);
               bundle.putInt("position",position);
               bundle.putString("student_id",student_id);
               bundle.putString("student_name",student_name);
               intent.putExtras(bundle);
               startActivity(intent);
               Log.e("CHANGE",title);
           }

           @Override
           public void onCancelled(DatabaseError databaseError) {

           }
       });
      }
  });

Log.e("Metodolist","pass2");

  listview.setOnItemLongClickListener(new 
AdapterView.OnItemLongClickListener() {
      @Override
      public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                                     final int position, long id) {
          hom.addListenerForSingleValueEvent(new ValueEventListener() {

              @Override
              public void onDataChange(DataSnapshot dataSnapshot) {
//                       String STR= event.getValue().toString();
                  adapter.getItem(position);
//                   Log.e("To do List", String.valueOf(position));
                  adapter.remove(position);
//               Log.e("GETUID",event.getUid());
                  for(position1 = position; position1<listview.getCount(); 
position1 ++) {
                      hom.child(Integer.toString(position1)).removeValue();
                      hom.child(Integer.toString(position1)).setValue(dataSnapshot.child(Integer.toString(position1 + 1)).getValue());
                      hom.child(Integer.toString(position1 + 1)).removeValue();
                      adapter.notifyDataSetChanged();
                  }
              }
              @Override
              public void onCancelled(DatabaseError databaseError) {
              }
          });
          return true;
      }
  });
}

private void createTop() {
    title = findViewById(R.id.me_calendar_title);
    title.setText(student_name + "todolist");
    back = findViewById(R.id.me_calendar_back);
    back.setOnClickListener(this);
    add = findViewById(R.id.me_calendar_add);
    add.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    Intent intent;
    switch (view.getId()) {
        case R.id.me_calendar_back:
            intent = new Intent(this, Me.class);
            intent.putExtra("student_id", student_id);
            startActivity(intent);
            break;
        case R.id.me_calendar_add:
            intent = new Intent(this,MeTodolistAdd.class);
            intent.putExtra("student_id",student_id);
            intent.putExtra("student_name",student_name);
            startActivity(intent);
            break;
    }
  }
}

And the MeTodolist.java:89 is below:

listview.setOnItemClickListener, String title = dataSnapshot.child(Integer.toString(position)).getValue().toString();
Zaid Mirza
  • 3,540
  • 2
  • 24
  • 40
Mark
  • 39
  • 8

0 Answers0