调用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();
}
我使用不同的按钮通过Onclick从XML调用“ cargarFotoPerfil”
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();
}
这是我在下面显示的logcat报告。
// ERROR.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)
有人知道该问题的解决方案吗?
预先感谢
答案 0 :(得分:0)
我相信您可以通过任何方式关闭对话框,并且它可能会解决您的崩溃问题...
尝试将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);
}
}
答案 1 :(得分:0)
该异常表明您未附加片段,否则可能会破坏附加的片段,请重新附加片段。
答案 2 :(得分:0)
使用iwego检查片段是否附着在Activity上。
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();
}
}
});
}
答案 3 :(得分:0)
我认为您需要检查您的代码。因为我回答了该片段,并且您的代码运行正常。也许您的错误是您用来在Activity中加载片段的方式。
我让您用来启动片段的代码。该代码有效。
从 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;
}
}
TestFragmentTwo
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。
希望对您有帮助。