无法跨活动访问全局变量

时间:2018-07-19 17:33:00

标签: java android-studio global-variables

在此代码中,我希望根据文本编辑中输入的用户显示字符串sh1,shg2和shg3。我创建了一个保护程序类来设置和检索变量。但是,尽管它们是全局变量,但shg1,sh2和sh3不会存储值,也不显示任何内容。谢谢您的帮助

保存程序类:

public class Saver extends Application {

public String shg1,shg2,shg3;

public String getShg1() {
    return shg1;
}

public void setShg1(String s1) {
    shg1= s1;
}
public String getShg2() {
    return shg2;
}

public void setShg2(String s1) {
    shg2= s1;
}
public String getShg3() {
    return shg3;
}

public void setShg3(String s1) {
    shg3= s1;
}
}

SHGDetails类:

public class SHGDetails extends AppCompatActivity {

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

    final EditText e1 = (EditText) findViewById(R.id.shg1);
    final EditText e2 = (EditText) findViewById(R.id.shg2);
    final EditText e3 = (EditText) findViewById(R.id.shg3);
    final Button b1 = (Button) findViewById(R.id.b3);
    final LinearLayout l = findViewById(R.id.linearl);
    final TextView t1 = findViewById(R.id.textView2);
    final TextView t2 = findViewById(R.id.textView3);
    final TextView t3 = findViewById(R.id.textView4);
    b1.setVisibility(View.INVISIBLE);
    e1.setVisibility(View.INVISIBLE);
    e2.setVisibility(View.INVISIBLE);
    e3.setVisibility(View.INVISIBLE);
    Boolean isFirstRun = getSharedPreferences("PREFERENCE", MODE_PRIVATE)
            .getBoolean("isFirstRun", true);
    Saver s = new Saver();

    if (isFirstRun) {
        //show start activity
            b1.setVisibility(View.VISIBLE);
        e1.setVisibility(View.VISIBLE);
        e2.setVisibility(View.VISIBLE);
        e3.setVisibility(View.VISIBLE);
         String text = e1.getText().toString();
         String text2 = e2.getText().toString();
         String text3 = e3.getText().toString();
        ((Saver) this.getApplication()).setShg1(text);
        ((Saver) this.getApplication()).setShg2(text2);
        ((Saver) this.getApplication()).setShg3(text3);


        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                l.removeView(e1);
                l.removeView(e2);
                l.removeView(e3);
                l.removeView(b1);

                getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit()
                        .putBoolean("isFirstRun", false).commit();


            }
        });
    }
    else {
        Toast.makeText(getApplicationContext(), s.getShg1(),Toast.LENGTH_LONG).show();

        t1.setText(((Saver) this.getApplication()).getShg1());
        t2.setText(((Saver) this.getApplication()).getShg2());
        t3.setText(((Saver) this.getApplication()).getShg3());
    }


}
}

1 个答案:

答案 0 :(得分:0)

您需要获取数据并在用户按下按钮时进行设置,以便在单击按钮之前没有任何值

b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

         // 
               String text = e1.getText().toString();
               String text2 = e2.getText().toString();
               String text3 = e3.getText().toString();
              ((Saver) SHGDetails.this.getApplication()).setShg1(text);
              ((Saver) SHGDetails.this.getApplication()).setShg2(text2);
              ((Saver) SHGDetails.this.getApplication()).setShg3(text3);

                l.removeView(e1);
                l.removeView(e2);
                l.removeView(e3);
                l.removeView(b1);

                getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit()
                        .putBoolean("isFirstRun", false).commit();


            }
        });

似乎您已经在清单中注册了您的应用程序。