2014-04-27 46 views
-2

我目前在Eclipse中從我發現的教程中構建了一個應用程序,但在代碼中出現錯誤。變量的非法修飾符

我已經包括下面的主要活動文件(MainActivity.java):

package com.example.youdothemath; 

import android.app.ActionBar; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.Fragment; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.os.Build; 
import android.os.Bundle; 
import android.R; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View.OnClickListener; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 

public class MainActivity extends Activity implements OnClickListener 
{ 
    private Button playBtn, helpBtn, highBtn; 

    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); //error 

     playBtn = (Button)findViewById(R.id.play_btn); //error 
     helpBtn = (Button)findViewById(R.id.help_btn); //error 
     highBtn = (Button)findViewById(R.id.high_btn); //error 

     playBtn.setOnClickListener(this); 
     helpBtn.setOnClickListener(this); 
     highBtn.setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View view) 
    { 
     //respond to clicks 
     if(view.getId()==R.id.play_btn) 
     { 
      //play button 
      private String[] levelNames = {"Easy", "Medium", "Hard"}; 
      AlertDialog.Builder builder = new AlertDialog.Builder(this); 
      builder.setTitle("Choose a level") 
      .setSingleChoiceItems(levelNames, 0, new DialogInterface.OnClickListener() 
      { 
       public void onClick(DialogInterface dialog, int which) 
       { 
        dialog.dismiss(); 
        //start gameplay 
        startPlay(which); 
       } 

      }); 

      AlertDialog ad = builder.create(); 
      ad.show(); 
     } 

     else if(view.getId()==R.id.help_btn) 
     { 
      //how to play button 
      Intent helpIntent = new Intent(this, HowToPlay.class); 
      this.startActivity(helpIntent); 
     } 
     else if(view.getId()==R.id.high_btn) 
     { 
      //high scores button 
      Intent highIntent = new Intent(this, HighScores.class); 
      this.startActivity(highIntent); 
     } 
    } 

    private void startPlay(int chosenLevel) 
    { 
     //start gameplay 
     Intent playIntent = new Intent(this, PlayGame.class); 
     playIntent.putExtra("level", chosenLevel); 
     this.startActivity(playIntent); 
    } 
} 

錯誤讀取,在線路

private String[] levelNames = {"Easy", "Medium", "Hard"}; 

有一個「用於可變levelNames非法改性劑;只有最後被允許「

有沒有人有任何想法?

+0

http://stackoverflow.com/questions/21280038/java-error-illegal-modifier-for-parameter-only-final-permitted? – Larme

回答

4

這是一個局部變量。你不需要聲明它private,因爲它已經是私人的範圍。你可以通過將它放在方法之外而將它移動到場中,並且在該類中將會有一個private修飾符。