1

我正在尝试通过 View v 上的静态上下文打印对话框。我遇到了错误“无法从静态上下文中引用”。我也不确定如何将 View v 带入该功能。谢谢您的帮助。

public class QuizActivity extends Activity {
private InterstitialAd interstitial;
private static int SIZE = 30;
public static int location;
private int counter = 0;
public static int amountCorrect = 0;
private static float percentCorrect = 0;
private static TextView percentText;

public static int[] scores = new int[30];

private int lastPosition = 0;

// initialize buttons
public static ImageButton buttons[] = new ImageButton[SIZE];

// array names
public static String[] names = {...
};

// array images
public static int[] images = { ... };

private GridView gridview;
private static ImageAdapter imageAdapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    percentText = (TextView) findViewById(R.id.percentText);


    imageAdapter = new ImageAdapter(this, SIZE);
    gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(imageAdapter);

    gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            //Toast.makeText(QuizActivity.this, "" + position, Toast.LENGTH_SHORT).show();
            // PopUpActivity

            Intent intent = new Intent(QuizActivity.this, PopUpWindow.class);
            intent.putExtra("position", position);
            intent.putExtra("images", images);
            intent.putExtra("names", names);

            startActivity(intent);



        }
    });





}



public static void dimImage(int location){
    imageAdapter.dimImage(location);
    imageAdapter.notifyDataSetChanged();
}
public static void updateScore(){
    amountCorrect += 1;
    percentCorrect = amountCorrect*100f/SIZE;
    percentText.setText("Percent Correct: " + String.format("%.2f", percentCorrect) + "%");
    if(amountCorrect==SIZE) {
        //QuizActivity quizActivity = new QuizActivity();
        //quizActivity.printWin();
    }

    QuizActivity.printWin(); // wish to invoke the method from here
}

public static void printWin(){


    PopupMenu popup = new PopupMenu(QuizActivity.this, v); // here is where I am stuck
    popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());

    //registering popup with OnMenuItemClickListener
    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        public boolean onMenuItemClick(MenuItem item) {
            Toast.makeText(QuizActivity.this, "You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show();
            return true;
        }
    });

    popup.show();//showing popup menu

}


}
4

0 回答 0