为什么onCreate()中的方法无法识别变量?

时间:2014-08-24 08:02:09

标签: android methods

在increaseProgress中,编译器说没有声明所有的Widgets,比如textViewTotalProgress,progressBar,它表示未知的实体&text;文本查看'

这里是java代码(抱歉,我不知道如何通过电话缩进):

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setNeutralButton("Ok",new DialogInterface.OnClickListener(){
        @Override
        public void onClick(DialogInterface dialog, int id){

        }
    });

    final View layoutUpgrades = findViewById(R.id.layoutUpgrades);
    final Button buttonUpgrade = (Button)findViewById(R.id.buttonUpgrades);
    final Button buttonUpgradePPC = (Button)findViewById(R.id.buttonUpgradePPC);
    final Button buttonChangelog = (Button)findViewById(R.id.buttonChangelog);
    final Button buttonUpgradePPS = (Button)findViewById(R.id.buttonUpgradePPS);
    final Button buttonHelp = (Button)findViewById(R.id.buttonHelp);
    final Button buttonUpgradeScissors = (Button)findViewById(R.id.buttonUpgradeScissors);
    final ImageView progressBar = (ImageView)findViewById(R.id.progressBar);
    final LinearLayout progressBarLayout = (LinearLayout)findViewById(R.id.progressBarLayout);
    final TextView textViewProgress = (TextView)findViewById(R.id.textViewProgress);
    final TextView textViewLevel = (TextView)findViewById(R.id.textViewLevel);
    final TextView textViewTotalProgress = (TextView)findViewById(R.id.textViewTotalProgress);
    final TextView textViewUpgradePPCInfo = (TextView)findViewById(R.id.textViewUpgradePPCInfo);
    final TextView textViewUpgradePPSInfo = (TextView)findViewById(R.id.textViewUpgradePPSInfo);
    final TextView textViewUpgradeScissorsInfo = (TextView)findViewById(R.id.textViewUpgradeScissorsInfo);
    final TextView textViewDebug = (TextView)findViewById(R.id.textViewDebug);

    public final void increaseProgress(int increase){
        progress += increase;
        totalProgress += increase;
        textViewTotalProgress.setText(getString(R.string.totalProgress) + ": " + Float.toString(totalProgress));

        if (progress >= forNextLevel){
            level += 1;
            MainActivity.textViewLevel.setText(getString(R.string.level) + ": " +  Integer.toString(level));
            progress = 0;
            multiplier = random.nextFloat() * 2 + 1;
            scissors = scissorsLevel * 2;
            forNextLevel = (level * baseNextLevel * multiplier);
            forNextLevel -= Math.round(forNextLevel / 100f * scissors);
        }

        LayoutParams.weight = (100.0f / forNextLevel) * progress;
        progressBar.setLayoutParams(layoutParams);
        textViewProgress.setText(Float.toString(progress) + " / " + Float.toString(forNextLevel));
    }

    public void showAlertDialog(String title, String message){
        alertDialogBuilder.setTitle(title);
        alertDialogBuilder.setMessage(message);
        alertDialog = alertDialogBuilder.create();
        alertDialog.show();
    }
}

2 个答案:

答案 0 :(得分:0)

我本可以在某个地方忽略一个大括号,但似乎你在increaseProgress方法中定义方法onCreate。 Java中不允许使用嵌套方法。

答案 1 :(得分:0)

<强> Try This Code


View layoutUpgrades;
Button buttonUpgrade,buttonUpgradePPC,buttonChangelog,buttonUpgradePPS,buttonHelp,buttonUpgradeScissors;
ImageView progressBar;
LinearLayout progressBarLayout;
TextView textViewProgress,textViewLevel,textViewTotalProgress,textViewUpgradePPCInfo,textViewUpgradePPSInfo,textViewUpgradeScissorsInfo,textViewDebug;

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setNeutralButton("Ok",new DialogInterface.OnClickListener(){
        @Override 
        public void onClick(DialogInterface dialog, int id){

        } 
    }); 

    layoutUpgrades =(View) findViewById(R.id.layoutUpgrades);
    buttonUpgrade = (Button) findViewById(R.id.buttonUpgrades);
    buttonUpgradePPC = (Button) findViewById(R.id.buttonUpgradePPC);
    buttonChangelog = (Button) findViewById(R.id.buttonChangelog);
    buttonUpgradePPS = (Button) findViewById(R.id.buttonUpgradePPS);
    buttonHelp = (Button) findViewById(R.id.buttonHelp);
    buttonUpgradeScissors = (Button) findViewById(R.id.buttonUpgradeScissors);
    progressBar = (ImageView) findViewById(R.id.progressBar);

    progressBarLayout = (LinearLayout) findViewById(R.id.progressBarLayout);

    textViewProgress = (TextView) findViewById(R.id.textViewProgress);
    textViewLevel = (TextView) findViewById(R.id.textViewLevel);
    textViewTotalProgress = (TextView) findViewById(R.id.textViewTotalProgress);
    textViewUpgradePPCInfo = (TextView) findViewById(R.id.textViewUpgradePPCInfo);
    textViewUpgradePPSInfo = (TextView) findViewById(R.id.textViewUpgradePPSInfo);
    textViewUpgradeScissorsInfo = (TextView) findViewById(R.id.textViewUpgradeScissorsInfo);
    textViewDebug = (TextView) findViewById(R.id.textViewDebug);

    public final void increaseProgress(int increase){
        progress += increase;
        totalProgress += increase;

        textViewTotalProgress.setText(getString(R.string.totalProgress) + ": " + Float.toString(totalProgress));
        if (progress >= forNextLevel){ 
            level += 1; 

            MainActivity.textViewLevel.setText(getString(R.string.level) + ": " +  Integer.toString(level));
            progress = 0; 
            multiplier = random.nextFloat() * 2 + 1; 
            scissors = scissorsLevel * 2; 
            forNextLevel = (level * baseNextLevel * multiplier); 
            forNextLevel -= Math.round(forNextLevel / 100f * scissors);
        } 
        LayoutParams.weight = (100.0f / forNextLevel) * progress; 
        progressBar.setLayoutParams(layoutParams);
        textViewProgress.setText(Float.toString(progress) + " / " + Float.toString(forNextLevel));
    } 

    public void showAlertDialog(String title, String message){
        alertDialogBuilder.setTitle(title);
        alertDialogBuilder.setMessage(message);
        alertDialog = alertDialogBuilder.create();
        alertDialog.show(); 
    } 
}