调用startActvityResult()
时发生错误。
我用它来从其他片段调用FragmentFotosdePerfil
public void verFotosPerfil(String userID){
fragmentFotosPerfil=new FragmentFotosPerfil();
Bundle bundle= new Bundle();
bundle.putString("userID",userID);
fragmentFotosPerfil.setArguments(bundle);
getFragmentManager().beginTransaction().
replace(R.id.frameLayout2,fragmentFotosPerfil).commit();
}
我从XML调用"cargarFotoPerfil",在不同的按钮中单击Onclick。
public void cargarFotoPerfil(View view){
switch(view.getId()) {
case R.id.btnCargarFoto2:
System.out.println("caso2");
cargarFotoPerfil2(view);
foto=2;
break;
case R.id.btnCargarFoto3:
cargarFotoPerfil2(view);
foto=3;
break;
case R.id.btnCargarFoto4:
cargarFotoPerfil2(view);
foto=4;
break;
case R.id.btnCargarFoto5:
cargarFotoPerfil2(view);
foto=5;
break;
}
public void cargarFotoPerfil2(View view){
final CharSequence[] opciones={"Elegir de Galeria","Cancelar"};
final AlertDialog.Builder builder=new
AlertDialog.Builder(view.getContext());
builder.setTitle("Elige una Opción");
builder.setItems(opciones, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (opciones[i].equals("Elegir de Galeria")){
Intent intent=new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/");
startActivityForResult
(intent.createChooser(intent,"Seleccione"),10);
}else{
dialogInterface.dismiss();
}
}
});
builder.show();
}
这是我的日志猫报告,如下所示。
//错误。CRASHES IN LINE STARTActivityForResult((
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.clemente.prueba6, PID: 4937
java.lang.IllegalStateException: Fragment FragmentFotosPerfil{18cbf6df} not attached to Activity
at android.support.v4.app.Fragment.startActivityForResult(Fragment.java:1019)
at android.support.v4.app.Fragment.startActivityForResult(Fragment.java:1010)
at com.example.clemente.prueba6.FragmentFotosPerfil$1.onClick(FragmentFotosPerfil.java:159)
at android.support.v7.app.AlertController$AlertParams$3.onItemClick(AlertController.java:1067)
at android.widget.AdapterView.performItemClick(AdapterView.java:300)
at android.widget.AbsListView.performItemClick(AbsListView.java:1143)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3044)
at android.widget.AbsListView$3.run(AbsListView.java:3833)
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:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
有谁知道这个问题的解决方案?
提前致谢
我相信你想关闭对话框的任何方式,它都可能会解决你的崩溃......
尝试将onClick
更改为如下所示:
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
if (opciones[i].equals("Elegir de Galeria")){
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/");
startActivityForResult(intent.createChooser(intent,"Seleccione"),10);
}
}
使用 isAdd 检查片段是否附加到活动。
if(isAdded){
final CharSequence[] opciones={"Elegir de Galeria","Cancelar"};
final AlertDialog.Builder builder=new AlertDialog.Builder(view.getContext());
builder.setTitle("Elige una Opción");
builder.setItems(opciones, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (opciones[i].equals("Elegir de Galeria")){
Intent intent=new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/");
startActivityForResult(intent.createChooser(intent,"Seleccione"),10);
}else{
dialogInterface.dismiss();
}
}
});
}
我认为你需要检查你的代码。因为我回复了片段并且您的代码工作正常。也许您的错误是您用于在活动中加载片段的方式。
我为您提供了我用来启动片段的代码。此代码有效。
从TestFragment调用另一个片段
public class TestFragment extends Fragment {
public TestFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_test_two, container, false);
TestFragmentTwo fragment = new TestFragmentTwo();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, fragment);
fragmentTransaction.addToBackStack(fragment.toString());
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.commit();
return v;
}
}
测试片段二
public class TestFragmentTwo extends Fragment {
public TestFragmentTwo() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_test_two, container, false);
final CharSequence[] opciones={"Elegir de Galeria","Cancelar"};
final AlertDialog.Builder builder=new AlertDialog.Builder(v.getContext());
builder.setTitle("Elige una Opción");
builder.setItems(opciones, (dialogInterface, i) -> {
if (opciones[i].equals("Elegir de Galeria")){
Intent intent=new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/");
getActivity().startActivityForResult(intent.createChooser(intent,"Seleccione"),10);
}else{
dialogInterface.dismiss();
}
});
builder.show();
return v;
}
}
如您所见,我正在从另一个片段加载一个片段,然后我调用 startActivityResult 没有问题。
我希望它对你有所帮助。
异常表明您没有附加片段,否则附加的片段可能会被销毁,请重新附加片段。