简体   繁体   中英

SecondActivity is not receiving the value - Intent related

import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;


public class MainActivity extends AppCompatActivity {
TextView t1,t2;
Button generate1;
MediaPlayer pickupgold, pickupplatinum;
SharedPreferences mySharedPreferences;
private static Button button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
    mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    pickupgold = MediaPlayer.create(this, R.raw.pickup_gold);
    pickupplatinum = MediaPlayer.create(this, R.raw.pickup_platinum);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    OnClickButtonListener();
    final ImageView imageView = (ImageView) findViewById(R.id.imageView);
    final ImageView imageView2 = (ImageView) findViewById(R.id.imageView2);
    t1 = (TextView) findViewById(R.id.textView3);
    t2 = (TextView) findViewById(R.id.textView8);
    generate1 = (Button) findViewById(R.id.button);
    generate1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            TextView userTextEntry = (TextView) findViewById(R.id.textView3);
            String userData = userTextEntry.getText().toString();
            int num2 = mySharedPreferences.getInt("INT_KEY1", Integer.parseInt(userData));
            int num3 = mySharedPreferences.getInt("INT_KEY2", -1);
            Intent intent = new Intent(MainActivity.this, SecondActivity.class);
            intent.putExtra("parameter1", num2);
            Random rand = new Random();
            int num1 = rand.nextInt(100);
                if (num1 % 2 == 0) {
                    pickupgold.start();
                    num2 += 1;
                    SharedPreferences.Editor editor = mySharedPreferences.edit();
                    editor.putInt("INT_KEY1", num2);
                    editor.apply();
                    t1.setText(String.format("%d", num2));
                    imageView.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, android.R.anim.slide_in_left));
                    Toast.makeText(getApplicationContext(), R.string.foundgold2, Toast.LENGTH_LONG).show();
                }else{
                    Toast.makeText(getApplicationContext(), R.string.notfoundgold, Toast.LENGTH_LONG).show();
                } if (num1 == 1){
                    pickupplatinum.start();
                    num3 += 1;
                    SharedPreferences.Editor editor = mySharedPreferences.edit();
                    editor.putInt("INT_KEY2", num3);
                    editor.apply();
                    t2.setText(String.format("%d", num3));
                    imageView2.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, android.R.anim.slide_in_left));
                    Toast.makeText(getApplicationContext(), R.string.foundplatinum1, Toast.LENGTH_LONG).show();
                }
            }
        });
}

public void OnClickButtonListener() {
    button2 = (Button)findViewById(R.id.button2);
    button2.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent("com.example.tans.goldminer1.SecondActivity");
                    startActivity(intent);
                }
            }
    );
}

This is MainActivity.java

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

public class SecondActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    Intent intent = getIntent();
    int goldamountval = intent.getIntExtra("parameter1", 0);
    TextView goldamount = (TextView) findViewById(R.id.goldamount);
    goldamount.setText("" + goldamountval);
}

and This is SecondActivity.java

I think the int value should be loaded from MainActivity.java but When I go to SecondActivity, It does not seem to appear, Just showing 0. What's the reason and how can i solve it? and Sorry, My Code may look like confused. :<

Try this on SecondActivity

 Intent intent = new Intent(MainActivity.this, SecondActivity.class);
   intent.putExtra("parameter1", num2);
   startActivity(intent );

You need to start the SecondActivity after the first two lines :

Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("parameter1", num2);
startActivity(intent);

Intent intent = new Intent(context, YourActivityClass.class);

intent.putExtra(KEY, );

startActivity(intent);

Seocnd Activity-

Intent intent = getIntent();

if (null != intent) {

String stringData= intent.getStringExtra(KEY);

int numberData = intent.getIntExtra(KEY, defaultValue);

boolean booleanData = intent.getBooleanExtra(KEY, defaultValue);

char charData = intent.getCharExtra(KEY, defaultValue); 

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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