简体   繁体   English

如何在多个方法之间共享变量?

[英]How do I share a variable among multiple methods?

after saved instance I need share "int audio" to check() method to do several operations. 保存后的实例我需要共享“int audio”来check()方法做几个操作。 I've an "info" option: when I click I see info dialog and I'd like listen audio (stop it when I click on "cancel"). 我有一个“信息”选项:当我点击时,我看到信息对话框,我想听音频(当我点击“取消”时停止它)。 But when I rotate emulator, I've another audio stars. 但是当我旋转模拟器时,我还有另一个音频明星。

int audio;
static int Info;

@Override
public void onCreate(Bundle savedInstanceState){ 
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
int Info = savedInstanceState.getInt("dialoginfo");
int audio = savedInstanceState.getInt("audio");
}

if(Info != 0)
  {
      Info();
}

private void Info(){
    Info = 1;
    sobCheck();
    LayoutInflater li = LayoutInflater.from(this);
    View view = li.inflate(R.layout.info, null);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setView(view).create();
    TextView text=(TextView) findViewById(R.id.infoView1);
    builder.setCancelable(false); 
    builder.setPositiveButton("Chiudi", new DialogInterface.OnClickListener() {  
           public void onClick(DialogInterface dialog, int id) {  
               Info = 0;
               mp.stop();
               mp.reset();
               mp.release();
                mp = null;
                audio = 0;                  
               dialog.cancel();
        }  
        });  
    builder.show();
        }


private void sobCheck(){
if (audio == 0){
mp = MediaPlayer.create(this, R.raw.sob);
mp.start();
audio = 1;
}



protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("audio", a);

after rotate screen, "audio" is always "0" 旋转屏幕后,“音频”始终为“0”

The problem is that you've created locally scoped variables that shadow your application level variables here: 问题是您创建了本地范围的变量,这些变量会影响您的应用程序级变量:

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState != null) {
        int Info = savedInstanceState.getInt("dialoginfo");
        int audio = savedInstanceState.getInt("audio");
    }

    if (Info != 0) {
        Info();
    }
}

This declares and initializes two new variables Info and audio that are scoped to your if check and discarded immediately after. 这声明并初始化两个变量Info音频 ,这些变量作为if检查的范围,并在之后立即丢弃。 They do not actually change the application level variables you declared outside the onCreate method. 它们实际上并不会更改您在onCreate方法之外声明的应用程序级别变量。 Try changing it to this: 尝试将其更改为:

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState != null) {
        Info = savedInstanceState.getInt("dialoginfo");
        audio = savedInstanceState.getInt("audio");
    }

    if (Info != 0) {
        Info();
    }
}

This prevents Info and audio from being redeclared in your local scope and should instead result in your already declared variables being initialized. 这可以防止Info音频在本地范围内重新声明,而应该导致已经声明的变量被初始化。

You have to declare it outside onCreate() scope: 你必须在onCreate()范围之外声明它:

int audio;

@Override
public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState);
    if (savedInstanceState != null) {
        audio = savedInstanceState.getInt("audio");
}

you re-initialize "audio" inside the method. 你重新初始化方法内的“音频”。 just take out the "int" before you assign the value of audio. 只需在分配音频值之前取出“int”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM