1

我正在尝试从此代码中扩展上下文菜单。但是,当我单击某个项目时,程序就会运行,没有任何反应。这是我正在使用的代码。你能帮我找出问题所在吗?我试图打开的菜单的名称称为“testmenu”。

package com.carouseldemo.main;

import com.carouseldemo.controls.Carousel;
import com.carouseldemo.controls.CarouselAdapter;
import com.carouseldemo.controls.CarouselItem;
import com.carouseldemo.controls.CarouselAdapter.OnItemClickListener;
import com.carouseldemo.controls.CarouselAdapter.OnItemSelectedListener;
import com.carouseldemo.main.R;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity implements OnClickListener {
 @Override
  public void onCreateContextMenu (ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.testmenu, menu);
  }


/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);




    MediaPlayer mp = MediaPlayer.create(this, R.raw.radio);

    mp.start();
    Carousel carousel = (Carousel)findViewById(R.id.carousel);
    carousel.setOnItemClickListener(new OnItemClickListener(){

        public void onItemClick(CarouselAdapter<?> parent, View view,
                int position, long id) {    
            registerForContextMenu(view);
            openContextMenu(view);


        }

        @Override
        public void onCreateContextMenu(ContextMenu menu, View v,
                CoTell me please, whenntextMenuInfo menuInfo) {
            // TODO Auto-generated method stub

        }



    });




    carousel.setOnItemSelectedListener(new OnItemSelectedListener(){

        public void onItemSelected(CarouselAdapter<?> parent, View view,
                int position, long id) {



            final TextView txt = (TextView)(findViewById(R.id.selected_item));

            switch(position){
            case 0:
                txt.setText("Power Source International ");
                break;
            case 1:
                txt.setText("Power Source International");
                break;
            case 2:
                txt.setText("Power Source International");
                break;
            case 3:
                txt.setText("Power Source International.");
                break;
            case 4:
                txt.setText("Power Source International");
            case 5:
                txt.setText("Power Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source International");
                break;
            }

        }

        public void onNothingSelected(CarouselAdapter<?> parent) {
        }

    }
    );

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}



}

如果有帮助,这里也是菜单的代码。

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/new_game"
      android:icon="@drawable/cat"
      android:title="@string/new_game"
      android:showAsAction="ifRoom" android:checkable="true" android:enabled="true"         android:visible="true"/>
<item android:id="@+id/help"
      android:icon="@drawable/cat"
      android:title="@string/help" />

如何在我的活动中打开此菜单?当我单击一个项目时,菜单不会出现。

4

0 回答 0