我目前正在从我发现的教程中构建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的非法修饰符;仅允许final”
有没有人有任何想法?
答案 0 :(得分:4)
这是一个局部变量。您不需要声明它private
,因为它已经是其范围的私有。你可以将它移到方法之外但是在类中,并且private
修饰符有效。