0
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    int count = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button abButton = (Button) findViewById(R.id.button1);
        final TextView changelingtext = (TextView) findViewById(R.id.changeling);
         abButton.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 Toast.makeText(getBaseContext(), "Buttons are working baby", Toast.LENGTH_LONG).show();
                 count++;
                String a = Integer.toString(count);
                 changelingtext.setText(a); 
                 gotonextpage(v);
             }
         });
    }
    public void gotonextpage(View view){
        Intent intent = new Intent(this, SecondpageActivity.class);
        startActivity(intent);
        intent.putExtra("count", count);
        //finish(); if you want to end this page

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }


}

头等舱在上面,二等舱在下面

package com.example.collegematch;



import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class SecondpageActivity extends Activity {
    int values;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.secondpage);
        Intent intent = getIntent();
        values = intent.getExtras().getInt("count");
        Button exitButton = (Button) findViewById(R.id.exit);
        Button textbutton = (Button) findViewById(R.id.coolbutton);
        TextView texty = (TextView) findViewById(R.id.cooltext);
        textbutton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(getBaseContext(), Integer.toString(values), Toast.LENGTH_LONG).show();
                System.out.println(values);
            }
        });
        exitButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(getBaseContext(), "Seeya", Toast.LENGTH_LONG).show();

                finish();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.secondpage, menu);
        return true;
    }

}

在 mainActivity 中,每次按下按钮 abButton 时,它都会将 count 变量增加 1。它还会创建一个新意图并通过 extra 将该变量发送到该意图。在第二个活动中,从意图中获取数据的“值”变量给了我一个空指针异常。为什么?

4

2 回答 2

4
 Intent intent = new Intent(this, SecondpageActivity.class);
 startActivity(intent);
 intent.putExtra("count", count);

改成

 Intent intent = new Intent(this, SecondpageActivity.class);
 intent.putExtra("count", count);
 startActivity(intent);

您已经开始第二个活动后设置额外的

于 2013-01-28T18:33:15.123 回答
2

只需更改这两个代码行,

Intent intent = new Intent(this, SecondpageActivity.class);
intent.putExtra("count", count);
startActivity(intent);

您的SecondActivity Intent在设置额外计数之前开始。

于 2013-01-28T18:33:35.840 回答