2017-08-15 20 views
-1

私はfirebaseデータベースからデータを取り込むためにカスタムのrecyclerviewを使用しています。私は正常にそれを行うことができたが、今は項目がクリックされたときに、新たな活動を開始するに苦しんでいます。私は項目をクリックするとnullポインタエラーが発生します。アイテムのプッシュIDを取得し、詳細アクティビティを開始するために使用しようとしています。これはfirebaseデータベースを使用してrecyclerviewから新しいアクティビティを開始します

MYアダプタ

public class SearchResultsAdapter extends RecyclerView.Adapter<SearchResultsAdapter.SearchViewHolder> { 


    Context context; 
    String downloadUrl; 

    String post_key; 
    ArrayList<Search1> mSearchList = new ArrayList<Search1>(); 
    private ItemClickListener mItemClickListener; 


    private DatabaseReference databaseReference15; 
    //Constructor of this class 
    public SearchResultsAdapter(ArrayList<Search1> mSearchList,Context context) { 
     this.context = context; 
     //Getting all superheroes 
     this.mSearchList = mSearchList; 
    } 

    @Override 
    public SearchViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View itemView = LayoutInflater.from(parent.getContext()) 
       .inflate(R.layout.searchrow, parent, false); 
     SearchViewHolder searchViewHolder = new SearchViewHolder(itemView, mItemClickListener); 
     return searchViewHolder; 
    } 

    @Override 
    public void onBindViewHolder(SearchViewHolder viewHolder, final int position) { 

     final Context context = viewHolder.downloadUrls.getContext(); 

     //Showing data on the views 
     final Search1 c = mSearchList.get(position); 
     post_key = c.getKey(); 
     viewHolder.uzername.setText(c.getUsername()); 
     Picasso.with(context).load(c.downloadUrl).into(viewHolder.downloadUrls); 
     databaseReference15 = FirebaseDatabase.getInstance().getReference().child("UG").child("UzersAll"); 
     viewHolder.setItemClickListener(new ItemClickListener() { 
      @Override 
      public void onClick(View view, int position) { 
       databaseReference15.child(post_key).addListenerForSingleValueEvent(new ValueEventListener() { 
        @Override 
        public void onDataChange(DataSnapshot dataSnapshot) { 

         String searched = (String) dataSnapshot.child("username").getValue(); 
         String halls = (String) dataSnapshot.child("halls").getValue(); 
         Intent SearchIntent = new Intent(context, SearchView1.class); 
         SearchIntent.putExtra("key", post_key); 
         SearchIntent.putExtra("username", searched); 
         SearchIntent.putExtra("halls", halls); 

         context.startActivity(SearchIntent); 


        } 

        @Override 
        public void onCancelled(DatabaseError databaseError) { 
         Toast.makeText(context, "Check Connection", Toast.LENGTH_LONG).show(); 
        } 
       }); 
      } 
     }); 


    } 


    @Override 
    public int getItemCount() { 
     return mSearchList.size(); 
    } 




    public class SearchViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 
     //Views 

     public TextView uzername; 
     public CircularImageView downloadUrls; 
     private ItemClickListener mItemClickListener; 


     Context context; 
     // ArrayList<Search1> mSearchList = new ArrayList<Search1>(); 

     //Initializing Views 
     public SearchViewHolder(View itemView,ItemClickListener itemClickListener) { 
      super(itemView); 
      context = itemView.getContext(); 
      this.mItemClickListener = itemClickListener; 
      // this.context = context; 
      //this.mSearchList = mSearchList; 

      uzername = (TextView) itemView.findViewById(R.id.uzername); 
      downloadUrls = (CircularImageView) itemView.findViewById(R.id.downloadUrls); 
     // itemView.setClickable(true); 
      itemView.setOnClickListener(this); 


     } 

     public void setItemClickListener(ItemClickListener itemClickListener){ 
      this.mItemClickListener = itemClickListener ; 
     } 


     @Override 
     public void onClick(View v) { 
      // int position = getAdapterPosition(); 
      //Search1 mSearchList = this.mSearchList.get(position); 
      // post_key = mSearchList.getKey(); 
      if(mItemClickListener != null){ 
       mItemClickListener.onClick(v,getAdapterPosition()); 
      } 


     } 
    } 


} 

私の活動

public class SearchResultsTest extends AppCompatActivity { 
    private RecyclerView mSearchlist; 

    private DatabaseReference databaseReference1, databaseReference2; 
    private FirebaseAuth firebaseAuth; 
    private ProgressDialog mProgressDialog; 
    Query query; 
    String user_key; 
    String halls; 
    String lastKnownKey; 
    private ArrayList<Search1> searchList = new ArrayList<>(); 
    private LinearLayoutManager mlinearLayout; 
    private SearchResultsAdapter mAdapter; 
    private SwipeRefreshLayout mRefreshLayout; 
    private static final int TOTAL_ITEMS_TO_LOAD = 2; 
    private int mCurrentPage = 1; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.searchlist); 

     Intent SearchIntent = getIntent(); 
     halls = SearchIntent.getStringExtra("halls"); 
     // final String sex = SearchIntent.getStringExtra("sex"); 

     mProgressDialog = new ProgressDialog(this); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar3); 
     setSupportActionBar(toolbar); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setDisplayShowHomeEnabled(true); 
     getSupportActionBar().setTitle(halls); 

     mAdapter = new SearchResultsAdapter(searchList,this); 

     mSearchlist = (RecyclerView) findViewById(R.id.searchlist); 
     mlinearLayout = new LinearLayoutManager(this); 
     mRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe); 

     mSearchlist.setHasFixedSize(true); 
     mSearchlist.setLayoutManager(mlinearLayout); 
     // mSearchlist.setAdapter(mAdapter); 
     mSearchlist.swapAdapter(mAdapter, true); 

     //setAdapter(adapter) 


     databaseReference1 = FirebaseDatabase.getInstance().getReference().child("UG").child("UzersH"); 
     databaseReference2 = FirebaseDatabase.getInstance().getReference().child("UG").child("Users1"); 

     firebaseAuth = FirebaseAuth.getInstance(); 
     final String user_id = firebaseAuth.getCurrentUser().getUid(); 

     loadResults(); 

     /* mSearchlist.addOnScrollListener(new EndlessRecyclerOnScrollListener(mlinearLayout) { 
      @Override 
      public void onLoadMore(int current_page) { 
       mCurrentPage++; 
       searchList.clear();// when we have reached end of RecyclerView this event fired 
       loadResults(); 
      } 
     });*/ 

     mProgressDialog.setMessage("Loading Please Wait..."); 
     mProgressDialog.show(); 

     /* mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener(){ 
      @Override 
      public void onRefresh() { 
       mCurrentPage++; 
       searchList.clear();// when we have reached end of RecyclerView this event fired 
       loadResults(); 
      } 



     });*/ 
     mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 
      @Override 
      public void onRefresh() { 
       mCurrentPage++; 
       searchList.clear();// when we have reached end of RecyclerView this event fired 
       mAdapter.notifyDataSetChanged();// when we have reached end of RecyclerView this event fired 

       loadResults(); 
      } 
     }); 



    } 


    private void loadResults() { 

     // Query query = databaseReference1.child(halls); 
     /* Query query = databaseReference1.child(halls).orderByChild("ts").limitToFirst(mCurrentPage * TOTAL_ITEMS_TO_LOAD); 
     query.addValueEventListener(new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       if(!dataSnapshot.hasChildren()){ 

        mCurrentPage--; 
       } 

       for (DataSnapshot data : dataSnapshot.getChildren()) { 
        Search1 search = data.getValue(Search1.class); 
        searchList.add(search); 
        mAdapter.notifyDataSetChanged(); 
        mProgressDialog.dismiss(); 

       } 
       *//* Search1 search = dataSnapshot.getValue(Search1.class); 
       searchList.add(search); 
       mAdapter.notifyDataSetChanged(); 
       mProgressDialog.dismiss();*//* 


      } 

      @Override 
      public void onCancelled(DatabaseError databaseError) { 

      } 
     });*/ 



    // .startAt(mCurrentPage*TOTAL_ITEMS_TO_LOAD);; 
    //.startAt(2).orderByChild("ts"); 
    //.limitToFirst(TOTAL_ITEMS_TO_LOAD); 
    //.startAt(mCurrentPage*TOTAL_ITEMS_TO_LOAD); 


     Query query = databaseReference1.child(halls).orderByChild("ts").limitToFirst(mCurrentPage * TOTAL_ITEMS_TO_LOAD); 
     query.addChildEventListener(new ChildEventListener() { 
      @Override 
      public void onChildAdded(DataSnapshot dataSnapshot, String s) { 
       if (!dataSnapshot.hasChildren()){ 


        mProgressDialog.dismiss(); 

       } 
       Search1 search = dataSnapshot.getValue(Search1.class); 
       searchList.add(search); 
       mAdapter.notifyDataSetChanged(); 
       mSearchlist.scrollToPosition(searchList.size()-1); 
       mRefreshLayout.setRefreshing(false); 

       mProgressDialog.dismiss(); 

      } 

      @Override 
      public void onChildChanged(DataSnapshot dataSnapshot, String s) { 

      } 

      @Override 
      public void onChildRemoved(DataSnapshot dataSnapshot) { 

      } 

      @Override 
      public void onChildMoved(DataSnapshot dataSnapshot, String s) { 

      } 

      @Override 
      public void onCancelled(DatabaseError databaseError) { 

      } 
     }); 
} 


    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      case android.R.id.home: 
       // todo: goto back activity from here 

       /*Intent intent = new Intent(ViewProfile.this, MainActivity.class); 
       intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 
       startActivity(intent);*/ 
       finish(); 
       return true; 

      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 
} 

LOGのCATのERROR

E/AndroidRuntime: FATAL EXCEPTION: main 
        Process: dev.mintz.minzter, PID: 4703 
        java.lang.NullPointerException: Can't pass null for argument 'pathString' in child() 
         at com.google.firebase.database.DatabaseReference.child(Unknown Source) 
         at dev.mintz.minzter.SearchResultsAdapter$1.onClick(SearchResultsAdapter.java:73) 
         at dev.mintz.minzter.SearchResultsAdapter$SearchViewHolder.onClick(SearchResultsAdapter.java:158) 
         at android.view.View.performClick(View.java:4780) 
         at android.view.View$PerformClick.run(View.java:19866) 
         at android.os.Handler.handleCallback(Handler.java:739) 
         at android.os.Handler.dispatchMessage(Handler.java:95) 
         at android.os.Looper.loop(Looper.java:135) 
         at android.app.ActivityThread.main(ActivityThread.java:5254) 
         at java.lang.reflect.Method.invoke(Native Method) 
         at java.lang.reflect.Method.invoke(Method.java:372) 
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
+0

[NullPointerExceptionとは何ですか?それを修正するにはどうすればいいですか?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix -it) –

+0

このラインあまりにmItemClickListener.onClick(V、getAdapterPosition());このライン –

答えて

0

この行post_key = c.getKey();がnull値を代入する私のコードです。
databaseReference15.child(post_key)を呼び出すと、nullが許容値ではないことが示されます。

変数mSearchListの値を調べて、その理由を確認します。

+0

@JuanCruzSolerすぎmItemClickListener.onClick(V、getAdapterPosition()); –

関連する問題