2016-07-26 9 views
0

警告Dialogレスポンスの後にフラグメントをロードしようとしていますが、logcatをロードしようとすると「IllegalStateException:フラグメントがアクティビティに添付されていません」というメッセージが表示されます。フラグメントがアクティビティーに接続されなくなった後に作業を実行しようとすると、IllegalStateExceptionが発生します。しかし、私の場合、すべてのことがうまくいけば、なぜフラグメントがアクティビティに付随していないのかわかりません。AlertDialogを使用してフラグメントをロードする方法は?

これは私のMainActivityです:DialogFragmentを拡張し、私はDilogCreateを呼び出し、このクラスを使用して

public class MainActivity extends AppCompatActivity { 
Button btn; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     btn= (Button) findViewById(R.id.button); 

     btn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 

       new DilogCreate(view.getContext(),R.string.tilte,R.string.no,R.string.yes); 
      } 
     }); 
    } 

これは私のDilogCreateクラスです:

ダイアログ応答に基づいて、私はフラグメントをロードしようとするダイアログ応答がはい、私はこのクラスの下に別のアクティビティ名Second.javaを呼び出す場合の断片がロードされてかないことができます決めます。

public class DilogCreate extends DialogFragment { 
     AlertDialog alertDialog; 


     public DilogCreate(final Context context, int tilte, int no, int yes) { 

      AlertDialog.Builder mAlertDilog = new AlertDialog.Builder(context); 

      mAlertDilog.setNegativeButton(yes, new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialogInterface, int i) { 

        Intent intent = new Intent(context, second.class); 
        startActivity(intent); 
       } 
      }); 
      alertDialog = mAlertDilog.create(); 
      alertDialog.show(); 
     } 

    } 

これは私の私のSecond.javaクラスです:

このクラスためのダイアログ応答のと私はフラグメントをロードしようとした、このクラスの下に表示されています。

public class Second extends AppCompatActivity { 
    FragmentManager fragmentManager; 
    FragmentTransaction fragmentTransaction; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.loadFragment); 

     fragmentManager=getSupportFragmentManager(); 
     fragmentTransaction=fragmentManager.beginTransaction(); 

     Myfragment myfragment=new Myfragment(); 

     fragmentTransaction.replace(R.id.cont,myfragment); 
     fragmentTransaction.commit(); 
} 
} 

これはMyFragment.javaがフラグメント拡張です:私たちは、私はこのエラーが来ている理由はわかりません助けてください

java.lang.IllegalStateException: Fragment DilogCreate{8aea1d4} not attached to Activity 

public class Myfragment extends Fragment{ 
    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.frag,container,false); 
    } 
} 

Logcatのステータスを。

答えて

1
new DilogCreate(view.getContext(),R.string.tilte,R.string.no,R.string.yes); 

パスMainActivity.thisの代わりに、view.getContext()。あなた、ここでの活動コンテキストを渡す必要があります。

+0

view.getContextは()ので、活動状況の両方が同じである –

+0

あなたのビューで使用されるコンテキストであることを保証するものではございませんここでActivityリファレンスを渡してビルダーに渡したいと思うと思います。 – codemonger

+0

彼はそれを手に入れました。また、Stringをintパラメータに渡すのはなぜですか? – klutch

0

フラグメントは、基本アクティビティrefrenceを使用してstartActivity()メソッドを呼び出す必要があるため、ベース参照を使用してアクティビティにロードされ、1つのアクティビティが別のアクティビティに切り替わります。

ので、私はこのようDilogCreateクラスにstartActivit()メソッドを変更:

Intent intent=new Intent(context,Second.class); 
context.startActivity(intent);//make sure context is the refrence of the base context 
関連する問題