我无法解决程序错误,如何解决?

时间:2018-02-07 04:25:23

标签: java android firebase

我的目标是让用户点击列表视图,它将转到页面,让用户修改数据,然后更新到数据库。

这是数据库参考: https://imgur.com/6Qx10ll

此外,在项目onclick上,我想将位置的数据发送到修改页面。但错误是

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)

我要做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;
    }
  }
}

MeTodolist.java:89位于以下位置:

listview.setOnItemClickListener, String title = dataSnapshot.child(Integer.toString(position)).getValue().toString();

0 个答案:

没有答案