2017-07-05 7 views
-1

私のアプリケーションにrecyclerviewがあり、リストアイテムを押したときボトムシートダイアログが2つのボタンを持っています。 これは、アダプタボトムシート上のボタンでクリックリスナーを設定しようとしたときにNullPointerExceptionが発生しました

BottomSheetDialog offer_info_dialog; 
RelativeLayout rel; 
Button Yes,No; 
View parentView; 
@Override 
public void onClick(View view) { 
    rel = (RelativeLayout) activity.findViewById(R.id.bottomsheet); 
    Yes = (Button) offer_info_dialog.findViewById(R.id.confirm_btn_on_info); 
    No = (Button) offer_info_dialog.findViewById(R.id.cancel_btn_on_info); 
     offer_info_dialog = new BottomSheetDialog(context); 
     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     parentView = inflater.inflate(R.layout.offer_info_layout, null); 
     offer_info_dialog.setContentView(parentView); 
     ((View) parentView.getParent()).setBackgroundColor(context.getResources().getColor(android.R.color.transparent)); 
     offer_info_dialog.show(); 
     rel.setVisibility(View.INVISIBLE); 

    Yes.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      MainActivity.removeCheatOfferMarkers(2); 
     } 
    }); 

    No.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from((View) parentView.getParent()); 
      bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED); 
     } 
    }); 
    offer_info_dialog.setOnDismissListener(new DialogInterface.OnDismissListener() { 
     @Override 
     public void onDismiss(DialogInterface dialog) { 
      rel.setVisibility(View.VISIBLE); 
     } 
    }); 

} 

中のonClickメソッドであり、これは誤りです:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.support.design.widget.BottomSheetDialog.findViewById(int)' on a null object reference 
+1

[NullPointerExceptionがある何を、どのように私はそれを修正しますか?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix -それ) –

答えて

1

複数の問題がここに。

  1. ビューを拡張する前にfindViewByIdを呼び出しています。これにより、nullpointer例外が発生します。 findViewByIdはinflater.inflateが呼び出された後にのみ呼び出されます。ただし、あなたのアクティビティが既に膨張していると思われる場合を除きます。

  2. あなたのコードを見てください。 offer_info_dialogは、あなたがnullpointerを取得している理由で、そのメソッドを呼び出す前に何かに初期化されていません。

関連する問題