2016-04-18 47 views
0

アダプタを使用してdialogFragmentを開こうとしていますが、ドキュメントやその他の質問がありましたが、問題は見つかりません。これは、ログからの完全なエラーラインであるjava.lang.NullPointerException: Attempt to invoke virtual method 'android.app.FragmentManager android.app.Activity.getFragmentManager()' on a null object reference Android:dialogFragをアダプタに表示するとNullPointerExceptionが発生する

マイアダプタ:

public class SubjectsAdapter extends RecyclerView.Adapter<SubjectsAdapter.ViewHolder> { 

    public List<String> items = new ArrayList<>(); 
    public Activity mcontext; 

    public SubjectsAdapter(Activity context) { 
     this.mcontext=context.getParent(); 
     assert mcontext != null; 

    } 

//...usual adapter code here... 

    static int i; 

    class ViewHolder extends RecyclerView.ViewHolder{ 

     public Button GridButton; 

     public ViewHolder(View itemView) { 
      super(itemView); 

      GridButton = (Button) itemView.findViewById(R.id.grid_button); 

      GridButton.setId(++i); 
      GridButton.setText("Button " + i); 

      assert GridButton != null; 
      GridButton.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        //removeItem(getAdapterPosition()); 
        Fragment_Subject_Edit newFragment = Fragment_Subject_Edit.newInstance(); 
        newFragment.show(mcontext.getFragmentManager(), "Title"); 
       } 
      }); 
     } 
    }  
} 

DialogFragment:

 public static Fragment_Subject_Edit newInstance() { 

     return new Fragment_Subject_Edit(); 
    } 
    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 

     LayoutInflater inflater = getActivity().getLayoutInflater(); 

     // Pass null as the parent view because its going in the dialog layout 
     builder.setView(inflater.inflate(R.layout.fragment_subject_edit, null)); 

     return builder.create(); 
    } 

[OK]をので、どのような私は私DialogFragmentときを示すために、ここで行方不明です私はGridButtonをクリックしますか?このラインで

+1

'mcontext = context;'を指定するとどうなりますか?あなたが現在 'mcontext = context.getParent();'を使っている特別な理由は? –

+0

さて、それは働いて、ありがとう! –

答えて

1

:あなたはfragmentManagerとあなたの問題のためにmcontextするために求めている

newFragment.show(mcontext.getFragmentManager(), "Title"); 

は、その時点でmcontextがnullであるということです。ジョージが言ったように、あなたが本当にそれを必要としない限り、親の代わりに文脈をまっすぐに設定して、Nullの潜在的な原因である。

+0

これはうまくいきましたが、すぐに質問しました。もし私が '.getParent'を設定していないのであれば、どのコンテキストに' mcontext'を正確に載せていますか? –

+0

これは、作成時に 'SubjectsAdapter'オブジェクトに渡すパラメータによって異なります。私の推測では、あなたの主なアクティビティからそれを作成し、 'this'をパラメータとして渡すことです。そうであればアクティビティのコンテキストを取得しています。 –

+0

あなたは正しいと推測しました。説明のためにありがとう=) –

0

あなたのgradle依存関係にPopライブラリを入れた後、次のコードを使ってみてください。アプリのモジュールのGradle

dependencies { 
    compile 'com.vistrav:pop:2.0' 
} 

とのあなたのクラスでこれを追加するには文句を言わないも、あなたが任意のより多くのコードを必要としません。この

public class SubjectsAdapter extends RecyclerView.Adapter<SubjectsAdapter.ViewHolder> { 

    public List<String> items = new ArrayList<>(); 
    public Activity mcontext; 

    public SubjectsAdapter(Activity context) { 
     this.mcontext=context.getParent(); 
     assert mcontext != null; 

    } 

//...usual adapter code here... 

    static int i; 

    class ViewHolder extends RecyclerView.ViewHolder{ 

     public Button GridButton; 

     public ViewHolder(View itemView) { 
      super(itemView); 

      GridButton = (Button) itemView.findViewById(R.id.grid_button); 

      GridButton.setId(++i); 
      GridButton.setText("Button " + i); 

      assert GridButton != null; 
      GridButton.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        Pop.on(mcontext).with().title("Title").body(R.layout.fragment_subject_edit).show(); 
       } 
      }); 
     } 
    }  
} 

などの簡単なものでメソッドにonCreateDialog

を実装する必要がありますこの後にダイアログを構築する。このライブラリの詳細を確認することができますhere

+0

解決しましたが、ありがとうございます –

関連する問題