简体   繁体   中英

android listactivity not working

i am learning android from newboston .. i want to show a menu first as showing in this tutorial .. i have done all the same but dont know why menu activity is not coming up .. help me where i am doing wrong

this is my code

android manfiest.xml

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".Splash"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <!-- 2nd activity -->


    <activity
        android:name="com.example.android.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.example.android.MainActivity" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

    <!-- 3rd Activity Test -->

      <activity
        android:name=".Menu"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.example.android.Menu" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>


       <activity
        android:name=".TesxtPlay"
        android:label="@string/app_name" >

    </activity>
</application>

this is the menuClass

  public class Menu extends ListActivity {

//declaring above so both methods can access these
String classes[] = {"MainActivity","TextPlay","example2",
        "example3","example4","example5","example6",};

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setListAdapter(new ArrayAdapter<String>(Menu.this,  android.R.layout.simple_list_item_1, classes));
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
    String cheese = classes[position];

    Class ourClass;
    try {
        ourClass = Class.forName("com.example.android." + cheese);

        Intent ourIntent = new Intent(Menu.this,ourClass);
        startActivity(ourIntent);
    }catch(ClassNotFoundException e){
        e.printStackTrace();

    }

}


  }

textPlayclass

  public class TextPlay extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test);

    Button chkCmd = (Button) findViewById(R.id.bResults);
    final ToggleButton passTog = (ToggleButton) findViewById(R.id.tbPassword);
    final EditText input = (EditText) findViewById(R.id.etCommands);
    TextView display = (TextView) findViewById(R.id.display);
    passTog.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            if(passTog.isChecked()){
                input.setInputType(InputType.TYPE_CLASS_TEXT
                        | InputType.TYPE_TEXT_VARIATION_PASSWORD);

            }else{
                input.setInputType(InputType.TYPE_CLASS_TEXT);

            }

        }
    });
}

         }

splash

  public class Splash extends Activity {

MediaPlayer ourSong;

@Override
protected void onCreate(Bundle iloveyou) {
    // TODO Auto-generated method stub
    super.onCreate(iloveyou);

    setContentView(R.layout.splash);
 ourSong = new MediaPlayer().create(Splash.this, R.raw.kalimba);
  ourSong.start();
    Thread timer = new Thread(){
        public void run(){
            try{
                sleep(5000);
            }catch(InterruptedException e){
                e.printStackTrace();
            }finally{
                //starting activity
                Intent openMainActivityClass = new Intent("com.example.android.MainActivity");
                startActivity(openMainActivityClass);
            }

        }
    };

    timer.start();

}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    ourSong.release();
    finish();
}


  }

The menu is not showing up because you do not inflate it.

In your Menu class add the following lines of code:

@Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);
    return super.onCreateOptionsMenu(menu);
}

This assumes your menu is stored as menu.xml the folder /res/menu .

EDIT:

Your menu.xml should look something like this:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/itemAboutUs"
        android:icon="@android:drawable/ic_menu_info_details"
        android:title="About Us">
    </item>
    <item
        android:id="@+id/itemPreferences"
        android:icon="@android:drawable/ic_menu_preferences"
        android:title="Preferences">
    </item>

</menu>

Your .Splash activity has the <category android:name="android.intent.category.LAUNCHER" /> intent-filter in it.

That means that's the activity that's launching when you run your app.

EDIT :

In your Splash.java, youre launching the activity with intent filter ""com.example.android.MainActivity"

This isnt your Menu Activity.. your menu Activity has the intent filter "com.example.android.Menu"

So just change

Intent openMainActivityClass = new Intent("com.example.android.MainActivity");

to

Intent openMainActivityClass = new Intent("com.example.android.Menu");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM