如何在2个活动*享2个变量

时间:2022-05-10 11:17:44
 public void select(View v) {
        ImageButton imageButton = (ImageButton) v;
        //startActivity(new Intent(getApplicationContext(), level1_game.class));
        startActivity(new Intent(getApplicationContext(), charicter.class));
    }

    public void loadout(View v) {
        Button Button = (Button) v;
        coinvar = coinvar +1;
        TextView mTextView = (TextView) findViewById(R.id.coin1);
        mTextView.setText(Integer.toString(coinvar));
        //startActivity(new Intent(getApplicationContext(), charicter.class));
    }
}

Now as I switch activities I am trying to display the same number but it does not seem to work and I get a error java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference

现在,当我切换活动时,我试图显示相同的数字,但它似乎不起作用,我得到一个错误java.lang.NullPointerException:尝试调用虚方法'android.content.Context android.content.Context.getApplicationContext( ''在空对象引用上

Here is the other activity I am changing to.

这是我要改变的其他活动。

 int mcoin = ((level) getApplicationContext()).coinvar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_charicter);
    TextView mTextView = (TextView) findViewById(R.id.coin2);
    mTextView.setText(Integer.toString(mcoin));

Is there any way to do this simple and easy.

有没有办法做到这一点简单容易。

3 个解决方案

#1


1  

I suppose you are trying to send only an int variable to the 2nd activity and doing nothing else. For this problem I suggest to send it by the Intent:

我想你试图只将一个int变量发送到第二个活动而不做任何其他事情。对于这个问题,我建议通过Intent发送:

In first activity:

在第一项活动中:

startActivity(new Intent(getApplicationContext(), charicter.class).putExtra("coins",coinvar));

In second activity:

在第二项活动中:

private coinvar;
protected void onCreate(Bundle savedInstanceState) {
...
coinvar=getIntent().getIntExtra("coins",coinvar);

Note that it leads to an one-way data share.

请注意,它会导致单向数据共享。

#2


0  

Create a class and define two static value in it.

创建一个类并在其中定义两个静态值。

public class SharedValue {
   public static int value1;
   public static int value2;
}

Then in each of your activities you can use SharedValue.value1 for example to set and get the value.

然后,在您的每个活动中,您可以使用SharedValue.value1来设置和获取值。

Also you can do it without static fields, just put this fields inside the Application class. If you don't have an application class you can in define it by inheriting from Application, Then you have to go to manifest file and update the application name filed. Unlike activities application will be alive while your app is open and your data will not get lost.

您也可以在没有静态字段的情况下执行此操作,只需将此字段放在Application类中即可。如果您没有应用程序类,则可以通过继承Application来定义它,然后您必须转到清单文件并更新应用程序名称。与活动不同,应用程序打开时应用程序将处于活动状态,您的数据不会丢失。

#3


0  

Any object being public static can be accessed from anywhere, you can store them wherever you want, maybe an Activity, Service, etc. But I would suggest you to store them in your custom Application class since it is always running. If you store the shared values in an Activity you have to deal with memory leaks.

任何公共静态对象都可以从任何地方访问,你可以将它们存储在任何你想要的地方,也许是一个Activity,Service等。但我建议你将它们存储在自定义Application类中,因为它始终在运行。如果将共享值存储在Activity中,则必须处理内存泄漏。

if you do

如果你这样做

public class MyApp extends Application{
    public static int sharedValue1;
    public static int sharedValue2;
}

Then you can access those with MyApp.sharedValue1, or if you want to implement some logic to the getters you could do:

然后你可以使用MyApp.sharedValue1访问那些,或者如果你想为getter实现一些逻辑:

getApplicationContext().getSharedValue1();

if you can not call getApplicationContext(), then create a method inside MyApp:

如果你不能调用getApplicationContext(),那么在MyApp中创建一个方法:

public static Context getAppContext(){
    return this;
}

then MyApp.getAppContext().getSharedValue1();

#1


1  

I suppose you are trying to send only an int variable to the 2nd activity and doing nothing else. For this problem I suggest to send it by the Intent:

我想你试图只将一个int变量发送到第二个活动而不做任何其他事情。对于这个问题,我建议通过Intent发送:

In first activity:

在第一项活动中:

startActivity(new Intent(getApplicationContext(), charicter.class).putExtra("coins",coinvar));

In second activity:

在第二项活动中:

private coinvar;
protected void onCreate(Bundle savedInstanceState) {
...
coinvar=getIntent().getIntExtra("coins",coinvar);

Note that it leads to an one-way data share.

请注意,它会导致单向数据共享。

#2


0  

Create a class and define two static value in it.

创建一个类并在其中定义两个静态值。

public class SharedValue {
   public static int value1;
   public static int value2;
}

Then in each of your activities you can use SharedValue.value1 for example to set and get the value.

然后,在您的每个活动中,您可以使用SharedValue.value1来设置和获取值。

Also you can do it without static fields, just put this fields inside the Application class. If you don't have an application class you can in define it by inheriting from Application, Then you have to go to manifest file and update the application name filed. Unlike activities application will be alive while your app is open and your data will not get lost.

您也可以在没有静态字段的情况下执行此操作,只需将此字段放在Application类中即可。如果您没有应用程序类,则可以通过继承Application来定义它,然后您必须转到清单文件并更新应用程序名称。与活动不同,应用程序打开时应用程序将处于活动状态,您的数据不会丢失。

#3


0  

Any object being public static can be accessed from anywhere, you can store them wherever you want, maybe an Activity, Service, etc. But I would suggest you to store them in your custom Application class since it is always running. If you store the shared values in an Activity you have to deal with memory leaks.

任何公共静态对象都可以从任何地方访问,你可以将它们存储在任何你想要的地方,也许是一个Activity,Service等。但我建议你将它们存储在自定义Application类中,因为它始终在运行。如果将共享值存储在Activity中,则必须处理内存泄漏。

if you do

如果你这样做

public class MyApp extends Application{
    public static int sharedValue1;
    public static int sharedValue2;
}

Then you can access those with MyApp.sharedValue1, or if you want to implement some logic to the getters you could do:

然后你可以使用MyApp.sharedValue1访问那些,或者如果你想为getter实现一些逻辑:

getApplicationContext().getSharedValue1();

if you can not call getApplicationContext(), then create a method inside MyApp:

如果你不能调用getApplicationContext(),那么在MyApp中创建一个方法:

public static Context getAppContext(){
    return this;
}

then MyApp.getAppContext().getSharedValue1();