片段上的StartActivity

时间:2013-12-27 14:39:04

标签: android

我已经阅读了有关此问题的所有旧问题,但我找不到适用于我的解决方案。我快疯了。以下是我尝试过的所有代码,没有人可以使用。

1

submit.setOnClickListener(new View.OnClickListener() { //Listner OnClik sul bottone "Cerca"             

            @Override
            public void onClick (View v) { //OnClick                    
                    Bundle bundle = new Bundle(); //Creo il bundle
                    Log.d("4","ok");
                    bundle.putString("dati", camporicerca.getText().toString()); //Metto nel parametro "dati" il testo nel campo di ricerca
                    Log.d("5","ok");
                    Intent in = new Intent(getActivity(), Risultato.class); //Istanzio l'activity
                    Log.d("6","ok");   
                    in.putExtras(bundle); //Passo il payload all'activity
                    Log.d("7","ok");   
                    startActivity(in); //Avvio l'activity   
                    Log.d("8","ok");
                }

            });

2

submit.setOnClickListener(new View.OnClickListener() { //Listner OnClik sul bottone "Cerca"             

            @Override
            public void onClick (View v) { //OnClick                    
                    Bundle bundle = new Bundle(); //Creo il bundle
                    Log.d("4","ok");
                    bundle.putString("dati", camporicerca.getText().toString()); //Metto nel parametro "dati" il testo nel campo di ricerca
                    Log.d("5","ok");
                    Intent in = new Intent(getActivity(), Risultato.class); //Istanzio l'activity
                    Log.d("6","ok");   
                    in.putExtras(bundle); //Passo il payload all'activity
                    Log.d("7","ok");   
                    getActivity().startActivity(in); //Avvio l'activity 
                    Log.d("8","ok");
                }

            });

3

submit.setOnClickListener(new View.OnClickListener() { //Listner OnClik sul bottone "Cerca"             

                @Override
                public void onClick (View v) { //OnClick                    
                        Bundle bundle = new Bundle(); //Creo il bundle
                        Log.d("4","ok");
                        bundle.putString("dati", camporicerca.getText().toString()); //Metto nel parametro "dati" il testo nel campo di ricerca
                        Log.d("5","ok");
                        Intent in = new Intent(getActivity().getApplicationContext(), Risultato.class); //Istanzio l'activity
                        Log.d("6","ok");   
                        in.putExtras(bundle); //Passo il payload all'activity
                        Log.d("7","ok");   
                        getActivity().startActivity(in); //Avvio l'activity 
                        Log.d("8","ok");
                    }

                });

4

   submit.setOnClickListener(new View.OnClickListener() { //Listner OnClik sul bottone "Cerca"              

            @Override
            public void onClick (View v) { //OnClick                    
                    Bundle bundle = new Bundle(); //Creo il bundle
                    Log.d("4","ok");
                    bundle.putString("dati", camporicerca.getText().toString()); //Metto nel parametro "dati" il testo nel campo di ricerca
                    Log.d("5","ok");
                    Intent in = new Intent(getActivity(), Risultato.class); //Istanzio l'activity
                    Log.d("6","ok");   
                    in.putExtras(bundle); //Passo il payload all'activity
                    Log.d("7","ok");   
                    startActivity(in); //Avvio l'activity   
                    Log.d("8","ok");
                }

            });

其他变种在Google上搜索。

这是LogCat

12-27 09:31:34.991: D/4(1054): ok
12-27 09:31:34.991: D/5(1054): ok
12-27 09:31:35.001: D/6(1054): ok
12-27 09:31:35.001: D/7(1054): ok
12-27 09:31:35.001: I/ActivityManager(381): START u0 {cmp=com.example.android.effectivenavigation/.Risultato (has extras)} from pid 1054
12-27 09:31:35.021: E/SoundPool(381): error loading /system/media/audio/ui/Effect_Tick.ogg
12-27 09:31:35.021: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
12-27 09:31:35.021: E/SoundPool(381): error loading /system/media/audio/ui/Effect_Tick.ogg
12-27 09:31:35.031: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
12-27 09:31:35.031: E/SoundPool(381): error loading /system/media/audio/ui/Effect_Tick.ogg
12-27 09:31:35.031: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
12-27 09:31:35.041: E/SoundPool(381): error loading /system/media/audio/ui/Effect_Tick.ogg
12-27 09:31:35.051: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
12-27 09:31:35.051: E/SoundPool(381): error loading /system/media/audio/ui/Effect_Tick.ogg
12-27 09:31:35.051: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
12-27 09:31:35.051: E/SoundPool(381): error loading /system/media/audio/ui/KeypressStandard.ogg
12-27 09:31:35.051: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressStandard.ogg
12-27 09:31:35.061: E/SoundPool(381): error loading /system/media/audio/ui/KeypressSpacebar.ogg
12-27 09:31:35.061: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressSpacebar.ogg
12-27 09:31:35.061: E/SoundPool(381): error loading /system/media/audio/ui/KeypressDelete.ogg
12-27 09:31:35.061: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressDelete.ogg
12-27 09:31:35.071: E/SoundPool(381): error loading /system/media/audio/ui/KeypressReturn.ogg
12-27 09:31:35.071: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressReturn.ogg
12-27 09:31:35.071: E/SoundPool(381): error loading /system/media/audio/ui/KeypressInvalid.ogg
12-27 09:31:35.071: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressInvalid.ogg
12-27 09:31:35.071: W/AudioService(381): onLoadSoundEffects(), Error -1 while loading samples

为什么不起作用?任何解决方案?

1 个答案:

答案 0 :(得分:2)

是否真的有必要从Fragment开始活动? 我没有尝试过你在问题中分享过的代码。但你可以按照以下方式实现这个目标

  1. 创建片段

    public class ClickFragment extends Fragment{
           << Fragment related code>>
    }
    
  2. 创建一个接口,该接口具有以Bundle对象作为参数调用另一个活动的方法,例如。 callSecondActivity(Bundle bundle)

    public interface FragmentCoordinator{
    
         void callSecondActivity(Bundle bundle);
    
    }
    
  3. 在您的Activity类

    中实现此接口
    public class YourActivity extends Activity implements FragmentCoordinator {
    
      <<< Activity related code>>>
    
    @Override
    public void callSecondActivity(Bundle bundle){
        Intent in = new Intent(YourActivity.this, Risultato.class);
        in.putExtras(bundle);
        startActivity(in);
    }
    
  4. 在当前片段中添加以下代码

         public class ClickFragment extends Fragment{
               << Fragment related code>>
    
             submit.setOnClickListener(new View.OnClickListener() { //Listner OnClik sul bottone "Cerca"              
    
                @Override
                public void onClick (View v) { //OnClick                    
                        Bundle bundle = new Bundle(); //Creo il bundle
    
                        bundle.putString("dati", camporicerca.getText().toString()); //Metto nel parametro "dati" il testo nel campo di ricerca
                        Activity activity = getActivity();
                FragmentCoordinator coordinator = (FragmentCoordinator)activity;
                coordinator.callSecondActivity(bundle);
                    }
    
                });
        }