0

メインアクティビティでonclicklistenerを設定したいときにレイアウトダイアグラムにレイアウトを使用してダイアログフラグメントを拡張するカスタムクラスを作成しましたが、im設定時にnullポイント例外を返します私のクリックリスナーの私の主な活動と私のダイアログフラグメントが相続人:カスタムダイアログフラグメントクラス内のボタンのsetonclicklistener

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    final Button cancelDialog = (Button) findViewById(R.id.cancelDialogButton); 
    View.OnClickListener listenerDialog = new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

     } 
    }; 
//this is where i get the null point exception 
    cancelDialog.setOnClickListener(listenerDialog); 


    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    final ViewGroup Addition = (ViewGroup) findViewById(R.id.activity_addition); 
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 

       FragmentManager fm = getSupportFragmentManager(); 
       DialogCards editNameDialogFragment = DialogCards.newInstance(); 
       editNameDialogFragment.show(fm, "fragment_edit_name"); 

     } 
    }); 

、今の私の衣装のクラス拡張ダイアログフラグメントは:

public class DialogCards extends DialogFragment { 


public Dialog dialog; 

@NonNull 
@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    Dialog dialog = super.onCreateDialog(savedInstanceState); 
    // request a window without the title 
    dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE); 
    return dialog; 


} 

@Override 
public void onResume() { 
    // Store access variables for window and blank point 
    Window window = getDialog().getWindow(); 
    Point size = new Point(); 
    // Store dimensions of the screen in `size` 
    Display display = window.getWindowManager().getDefaultDisplay(); 
    display.getSize(size); 
    // Set the width of the dialog proportional to 75% of the screen width 
    window.setLayout((int) (size.x * 0.75), WindowManager.LayoutParams.WRAP_CONTENT + 20); 
    window.setGravity(Gravity.CENTER); 
    // Call super onResume after sizing 

    super.onResume(); 
} 

public DialogCards() { 

} 

public static DialogCards newInstance() { 
    DialogCards frag = new DialogCards(); 
    Bundle args = new Bundle(); 
    frag.setArguments(args); 
    return frag; 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.carddialog, container); 

} 

@Override 
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 

    getDialog().getWindow().setSoftInputMode(
      WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); 

}} 
+0

ここで、exacltyはヌルポインタを取得していますか?ログを投稿してください。 –

+0

私のボタンにクリックリスナーを設定した行に – caninethacher

+0

メインアクティビティレイアウトまたはカードダイアロジックレイアウトのボタンがありますか? –

答えて

0

あなたがDialogCardsクラスでこのメソッドを置き換えることができます

@Override 
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
super.onViewCreated(view, savedInstanceState); 

    getDialog().getWindow().setSoftInputMode(
     WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); 
    final Button cancelDialog = (Button)view.findViewById(R.id.cancelDialogButton); 
    View.OnClickListener listenerDialog = new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 

    } 
}; 
cancelDialog.setOnClickListener(listenerDialog); 
}} 
+0

あなたは素晴らしい人です。 – caninethacher

関連する問題