将计数器传递给另一个Android活动

时间:2016-03-02 23:59:48

标签: android android-intent

抱歉,我的英语很差

当我点击第一个活动中的按钮时,我试图在第二个活动中增加一个整数值。 首先我点击一下按钮。在我的第二个活动之后,增加一个数字,并应使用共享首选项保存到我的TextView中显示。

但是我想要做的是没有工作

  nao=(ImageView)rootView.findViewById(R.id.nao);
      nao.setOnClickListener(new View.OnClickListener()

    {
        public void onClick(View v)
        {



            Intent intent2 = new Intent(getActivity(), BancoList.class);
            intent2.putExtra("num", 1);

第二项活动

 TextView tv;
SharedPreferences shre;
private int numero;

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

    tv=(TextView)findViewById(R.id.tv);

    shre = PreferenceManager.getDefaultSharedPreferences(this);
    String text = shre.getString("image_data", "");

    if( !text.equalsIgnoreCase("") ){
        tv.setText(text);
    }

Intent intent = getIntent();

    if(intent.getIntExtra("num", 1) == 1) {

        String present_value_string = tv.getText().toString();
        int present_value_int = Integer.parseInt(present_value_string);
        present_value_int++;

        tv.setText(String.valueOf(present_value_int));

        shre = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor edit=shre.edit();
        edit.putString("image_data", present_value_string);
        edit.commit();

我的日志猫,@ nolly-j

FATAL EXCEPTION: main

                                                                                  Process: com.example.wolney.tabhost, PID: 31184
                                                                             java.lang.ClassCastException: java.lang.String cannot be cast to      java.lang.Integer
                                                                                      at android.app.SharedPreferencesImpl.getInt(SharedPreferencesImpl.java:240)
                                                                                      at com.example.wolney.tabhost.fragments.FragmentUm$1.onClick(FragmentUm.java:50)
                                                                                       at android.view.View.performClick(View.java:4438)
                                                                                     at android.view.View$PerformClick.run(View.java:18422)
                                                                                     at android.os.Handler.handleCallback(Handler.java:733)
                                                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                        at android.os.Looper.loop(Looper.java:136)
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:5001)
                                                                                   at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                      at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                       at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
                                                                                      at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

当您转到第二个活动页面并返回第一个活动时,您的计数器变量可能会丢失其初始值。由于您使用的是共享首选项,因此最好在单击以移动到第二个活动页面时进行设置。

在第一个活动页面中,执行此操作

添加班级人员

private SharedPreferences preferences;

然后在onCreate方法中,添加

nao = (ImageView)rootView.findViewById(R.id.nao);
    nao.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // retrieve the value of counter from Shared Preference
            preferences = PreferenceManager.getDefaultSharedPreferences(this);
            int counter = preferences.getInt("image_data", 0);
            counter++;

            // store the value of counter after incrementing it by 1
            SharedPreferences.Editor edit = preferences.edit();
            edit.putInt("image_data", counter);
            edit.commit();

            // move to the second activity page
            Intent intent2 = new Intent(getActivity(), BancoList.class);
            startActivity(intent2);
        }
    });

在第二个活动页面中,使用此代码。在这种情况下,您无需将计数器存储在意图

// in the second activity page, get the stored counter value
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
int counter = preferences.getInt("image_data", 0);
tv.setText(String.valueOf(counter));

希望有所帮助