2013-01-05 164 views
2

我收到此錯誤:無效的類型無效,此變量

void is an invalid type for the variable onRadioButtonClicked

但開發者網站說,虛空是必須的!那麼問題在哪裏?該XML的的編碼是correc ..這個問題必須位於這裏的某個地方:

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; 
      } 

     } 

回答

8

onRadioButtonClicked包含您的onCreate方法中—讓他們單獨的方法。添加一個右括號}後:

setContentView(R.layout.activity_main_page); 
+0

謝謝!它解決了問題! –

+0

不客氣,不要忘記[接受這個答案](http://meta.stackexchange.com/a/65088/155831),如果你發現它很有用:) – Reimeus

0

做這樣的:

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; 
     } 

    } 
} 
+0

非常感謝你! –