- Forum-Beiträge: 89
25.10.2013, 20:05:07 via Website
25.10.2013 20:05:07 via Website
mein Projekt besteht aus zwei Klassen:
1. MainActivity (die ganz normale main)
2. OptionsActivity (soll starten wenn auf den Optionen-Button gedrückt wird (neben dem Menü Button))
Ich versuch von der MainActivity aus die OptionsActivity zu starten (über das onKeyDown-Event vom Typ Keycode_Menu).
Code - MainActivity:
2 public boolean onKeyDown(int keycode, KeyEvent e) {
3 switch(keycode) {
4 case KeyEvent.KEYCODE_MENU:
5 Intent intent = new Intent(MainActivity.this, OptionsActivity.class);
6 MainActivity.this.startActivity(intent);
7 }
8}
Code - OptionsActivity:
2
3 @Override
4 protected void onCreate(Bundle savedInstanceState) {
5 super.onCreate(savedInstanceState);
6 setContentView(R.layout.options_layout);
7 }
8
9 public void onBackPressed(){
10 this.finish();
11 }
12
13 @Override
14 public boolean onCreateOptionsMenu(Menu menu) {
15 // Inflate the menu; this adds items to the action bar if it is present.
16 getMenuInflater().inflate(R.menu.main, menu);
17 return true;
18 }
19
20 @Override
21 public boolean onOptionsItemSelected(MenuItem item) {
22 // Handle action bar item clicks here. The action bar will
23 // automatically handle clicks on the Home/Up button, so long
24 // as you specify a parent activity in AndroidManifest.xml.
25 switch (item.getItemId()) {
26 case R.id.action_settings:
27 return true;
28 }
29 return super.onOptionsItemSelected(item);
30 }
31
32 /**
33 * A placeholder fragment containing a simple view.
34 */
35 public static class PlaceholderFragment extends Fragment {
36 public PlaceholderFragment() {
37 }
38
39 @Override
40 public View onCreateView(LayoutInflater inflater, ViewGroup container,
41 Bundle savedInstanceState) {
42 View rootView = inflater.inflate(R.layout.fragment_main, container, false);
43 return rootView;
44 }
45 }
46}
Wo liegt der Fehler?
— geändert am 25.10.2013, 20:31:40
Empfohlener redaktioneller Inhalt
Mit Deiner Zustimmung wird hier ein externer Inhalt geladen.
Mit Klick auf den oben stehenden Button erklärst Du Dich damit einverstanden, dass Dir externe Inhalte angezeigt werden dürfen. Dabei können personenbezogene Daten an Drittanbieter übermittelt werden. Mehr Infos dazu findest Du in unserer Datenschutzerklärung.