void is an invalid type for this variable

2020-07-23 06:49发布

问题:

I am getting this error:

void is an invalid type for the variable onRadioButtonClicked

But the developer site says that void is a must! So where is the problem? The coding of the xml's is correc.. The problem must lie somewhere here:

package com.example.kernel.version;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;

public class MainPage extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent=getIntent();
        setContentView(R.layout.activity_main_page);       

        public void onRadioButtonClicked(View view) {
            // Is the button now checked?
            boolean checked = ((RadioButton) view).isChecked();

            // Check which radio button was clicked
            switch(view.getId()) {
                case R.id.radio_pirates:
                    if (checked)
                        // Pirates are the best
                    break;
                case R.id.radio_ninjas:
                    if (checked)
                        // Ninjas rule
                    break;
            }

        }

回答1:

Your onRadioButtonClicked is contained within your onCreate method—make them separate methods. Add a closing brace } after:

setContentView(R.layout.activity_main_page); 


回答2:

Do it like this:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;

public class MainPage extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent=getIntent();
        setContentView(R.layout.activity_main_page);       




    }

    public void onRadioButtonClicked(View view) {
        // Is the button now checked?
        boolean checked = ((RadioButton) view).isChecked();

        // Check which radio button was clicked
        switch(view.getId()) {
            case R.id.radio_pirates:
                if (checked)
                    // Pirates are the best
                break;
            case R.id.radio_ninjas:
                if (checked)
                    // Ninjas rule
                break;
        }

    }
}