方法onCreate(Bundle)已在类MainActivity中定义

时间:2018-09-06 19:33:37

标签: android android-studio

我想在我的应用程序中有两个seekBars。为此,我有两个on create命令,但是我的错误是方法MainCreate类中已经定义了onCreate(Bundle)方法。因此,我尝试撤消其中之一,但随后许多命令变成红色,并说“无法解析符号”。如何在创建函数中同时使用两个seekBars? 也许我应该提到,在我复制粘贴之前,第一个seekBar就像一个饰物一样工作。

我对编程应用程序还很陌生。

谢谢。

package com.car.bluetooth.bluetoothcar;

import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SeekBar;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {

int gas = 0;
int steering = 0;

//Variable Anpassen GAS

private SeekBar seekBarGas;
private TextView textViewGas;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

seekBarGas = (SeekBar) findViewById(R.id.seekBarGas);
textViewGas = (TextView) findViewById(R.id.textViewGas);
seekBarGas.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        textViewGas.setText(progress + "  /  " + seekBarGas.getMax());
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

        }
   });
}





//Variable Anpassen Steering

private SeekBar seekBarSteering;
private TextView textViewSteering;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    seekBarSteering = (SeekBar) findViewById(R.id.seekBarSteering);
    textViewSteering = (TextView) findViewById(R.id.textViewSteering);
    seekBarSteering.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            textViewSteering.setText(progress + "  /  " + seekBarSteering.getMax());
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });
}



}

2 个答案:

答案 0 :(得分:0)

您的代码中的问题是您有两次onCreate方法。因此,为了完成这项工作,您必须删除其中之一。

您的代码应如下所示:

import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SeekBar;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {

int gas = 0;
int steering = 0;

//Variable Anpassen GAS

private SeekBar seekBarGas;
private TextView textViewGas;

private SeekBar seekBarSteering;
private TextView textViewSteering;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

seekBarGas = (SeekBar) findViewById(R.id.seekBarGas);
textViewGas = (TextView) findViewById(R.id.textViewGas);
seekBarGas.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        textViewGas.setText(progress + "  /  " + seekBarGas.getMax());
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

        }
   });

   seekBarSteering = (SeekBar) findViewById(R.id.seekBarSteering);
    textViewSteering = (TextView) findViewById(R.id.textViewSteering);
    seekBarSteering.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            textViewSteering.setText(progress + "  /  " + seekBarSteering.getMax());
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });
}

}

答案 1 :(得分:0)

您有onCreate方法的重复声明,因此在您的活动中删除第二个on Create方法

代码如下

public class MainActivity extends AppCompatActivity {

int gas = 0;
int steering = 0;

//Variable Anpassen GAS

  private SeekBar seekBarGas;
  private TextView textViewGas;

   private SeekBar seekBarSteering;
   private TextView textViewSteering;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

seekBarGas = (SeekBar) findViewById(R.id.seekBarGas);
textViewGas = (TextView) findViewById(R.id.textViewGas);
seekBarGas.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    textViewGas.setText(progress + "  /  " + seekBarGas.getMax());
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {

    }
  });

seekBarSteering = (SeekBar) findViewById(R.id.seekBarSteering);
textViewSteering = (TextView) findViewById(R.id.textViewSteering);
seekBarSteering.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        textViewSteering.setText(progress + "  /  " + seekBarSteering.getMax());
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
});
}

}

Android活动生命周期方法在您的活动中调用一次,而不是两次

here

谢谢您,希望对您有帮助