Probleme dans une activité avec AppCompatActivity
Bonjour la communauté,
je reviens sur le site après avoir été absent depuis longtemps. Je félicite toute l'équipe et les participants sur les nombreux tutos et les aides cela m a permis de progresser.
je suis en train d améliorer une vieille application que j avais développé et je bute sur un problème bête de clic sur un bouton. je n ai pas d erreur dans mon code que je vous joins ,mais cela me plante l application. J utilise ce code sur toute les autres activités et cela fonctionne...
Mon Activité lance deux fragments et elle possède un menu qui lui est propre j ai bien instensié l appel du fichier xml et mon titre s affiche . le bouton retour est reconnu mais quand je clic dessus l application plante. je signale que le titre et le bouton son dans le même fichier xml.
j ai bien regarder de nombreux commentaire et tutos sur la fonction OnclicListener et j avoue je bloque.
je pense que par rapport a mes anciennes classes activity dans ma classe LancementFragment l' extends est AppCompatActivity et non Activity tout cours ,
je ne connais pas bien la différence.
donc voici mon code de l activité: en rouge les parties qui concerne ce maudit bouton.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
public class LancementFragment extends AppCompatActivity {
ImageButton retour;
TextView titre;
String resultproduits;
static int resultatclic;
public static final int CODE_GO_TO_HOME = 0;
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle extras = getIntent().getExtras();
resultproduits = extras.getString("produitsInter");// recup nom du produit
TextView titre =(TextView)findViewById ( R.id.titre ); // gestion affichage
titre.setText ( String.valueOf ( resultproduits +" BLEVE ")); // affichage nom du produit
retour = (ImageButton)findViewById ( R.id.imageButtonRetourBleve ); //gestion du bouton
retour.setOnClickListener ( (View.OnClickListener) this );//ecoute sur clic
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
public void onClick(View v) { // action sur clic bouon retour
if (v == retour) {
Intent retourIntent = new Intent(this, Intervention.class);
setResult(CODE_GO_TO_HOME);
finish();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private String fragments []= {"Calcul","Carte"};
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new Tab1();
case 1:
return new Tab2();
default:
return null;
}
}
@Override
public int getCount() {
return fragments.length;
}
@Override
public CharSequence getPageTitle(int position) {
return fragments[position];
}
}
} |
et voici le logCAt avec le message d'erreur
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
10-30 11:03:39.165 16820-16820/com.loleas.explosionthermic E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.loleas.explosionthermic, PID: 16820
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.loleas.explosionthermic/com.loleas.explosionthermic.LancementFragment}: java.lang.ClassCastException: com.loleas.explosionthermic.LancementFragment cannot be cast to android.view.View$OnClickListener
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2658)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5832)
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:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.ClassCastException: com.loleas.explosionthermic.LancementFragment cannot be cast to android.view.View$OnClickListener
at com.loleas.explosionthermic.LancementFragment.onCreate(LancementFragment.java:70)
at android.app.Activity.performCreate(Activity.java:6221)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2611)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723)*
at android.app.ActivityThread.access$900(ActivityThread.java:172)*
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)*
at android.os.Handler.dispatchMessage(Handler.java:102)*
at android.os.Looper.loop(Looper.java:145)*
at android.app.ActivityThread.main(ActivityThread.java:5832)*
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:1399)*
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)* |
Merci par avance si vous avez une piste ou une solution . n'étant pas codeur a la base.
Cordialement Eric