Java 如何访问以前的活动
我从OnCreate方法中的“firstplayer=player1.getText().toString();”中获取“java.lang.NullPointerException”。我的代码中的player1是上一个活动中的编辑文本之一Java 如何访问以前的活动,java,android,android-activity,Java,Android,Android Activity,我从OnCreate方法中的“firstplayer=player1.getText().toString();”中获取“java.lang.NullPointerException”。我的代码中的player1是上一个活动中的编辑文本之一 import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
public class startwith2player extends ActionBarActivity {
private boolean playerone = true , playertwo = false ;
private Integer score1 = 0 , score2 = 0;
private Integer flag4 = 0 , flag5 = 0 , flag6 = 0 , flag7 = 0, flag8 = 0, flag9 = 0 , flag10 = 0, flag11 = 0, flag12 = 0;
private Integer counter1 = 0 , counter2 = 0;
TextView tex1 ;
TextView tex2 ;
String firstplayer = "";
String secondplayer = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_startwith2player);
tex1 = (TextView) findViewById(R.id.textView3);
tex2 = (TextView) findViewById(R.id.textView4);
EditText player1 = (EditText)findViewById(R.id.editText);
EditText player2 = (EditText)findViewById(R.id.editText2);
firstplayer = player1.getText().toString();
secondplayer = player2.getText().toString();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_startwith2player, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void refresh(){
BlueAllBoxes();
if(playerone){
playerone = false;
playertwo = true;
}
else{
playerone = true;
playertwo = false;
}
flag4 = 0 ; flag5 = 0 ; flag6 = 0 ; flag7 = 0; flag8 = 0; flag9 = 0 ; flag10 = 0; flag11 = 0; flag12 = 0;
counter1 = 0 ; counter2 = 0;
}
public void BlueAllBoxes(){
Button button4 = (Button) findViewById(R.id.button4);
button4.setBackgroundColor(0xFF4B4BFF);
Button button5 = (Button) findViewById(R.id.button5);
button5.setBackgroundColor(0xFF4B4BFF);
Button button6 = (Button) findViewById(R.id.button6);
button6.setBackgroundColor(0xFF4B4BFF);
Button button7 = (Button) findViewById(R.id.button7);
button7.setBackgroundColor(0xFF4B4BFF);
Button button8 = (Button) findViewById(R.id.button8);
button8.setBackgroundColor(0xFF4B4BFF);
Button button9 = (Button) findViewById(R.id.button9);
button9.setBackgroundColor(0xFF4B4BFF);
Button button10 = (Button) findViewById(R.id.button10);
button10.setBackgroundColor(0xFF4B4BFF);
Button button11 = (Button) findViewById(R.id.button11);
button11.setBackgroundColor(0xFF4B4BFF);
Button button12 = (Button) findViewById(R.id.button12);
button12.setBackgroundColor(0xFF4B4BFF);
}
public void button4(View view) {
if(counter1 == 3){
if(flag4 == 1) {
view.setBackgroundColor(0xFF67FF5A);
counter2++;
if(playerone) {
score1 = score1 + 30;
// tex1.setText(String.valueOf(score1));
tex1.setText(firstplayer + " : " + score1 );
}
else {
score2 = score2 + 30;
tex2.setText(secondplayer + " : " + score2 );
}
if(counter2 == 3)
refresh();
}
else{
view.setBackgroundColor(0xFFFF231E);
if(playerone) {
score1 = score1 - 10;
tex1.setText(firstplayer + " : " + score1 );
}
else {
score2 = score2 - 10;
tex2.setText(secondplayer + " : " + score2 );
}
}
}
else {
if (flag4 == 0) {
flag4 = 1;
view.setBackgroundColor(0xFF67FF5A);
counter1++;
if(counter1 == 3)
BlueAllBoxes();
}
}
}
public void button5(View view) {
if(counter1 == 3){
if(flag5 == 1) {
view.setBackgroundColor(0xFF67FF5A);
counter2++;
if(playerone) {
score1 = score1 + 30;
tex1.setText(firstplayer + " : " + score1 );
}
else {
score2 = score2 + 30;
tex2.setText(secondplayer + " : " + score2 );
}
if(counter2 == 3)
refresh();
}
else{
view.setBackgroundColor(0xFFFF231E);
if(playerone) {
score1 = score1 - 10;
tex1.setText(firstplayer + " : " + score1 );
}
else {
score2 = score2 - 10;
tex2.setText(secondplayer + " : " + score2 );
}
}
}
else {
if (flag5 == 0) {
flag5 = 1;
view.setBackgroundColor(0xFF67FF5A);
counter1++;
if(counter1 == 3)
BlueAllBoxes();
}
}
}
public void button6(View view) {
if(counter1 == 3){
if(flag6 == 1) {
view.setBackgroundColor(0xFF67FF5A);
counter2++;
if(playerone) {
score1 = score1 + 30;
tex1.setText(firstplayer + " : " + score1 );
}
else {
score2 = score2 + 30;
tex2.setText(secondplayer + " : " + score2 );
}
if(counter2 == 3)
refresh();
}
else{
view.setBackgroundColor(0xFFFF231E);
if(playerone) {
score1 = score1 - 10;
tex1.setText(firstplayer + " : " + score1 );
}
else {
score2 = score2 - 10;
tex2.setText(secondplayer + " : " + score2 );
}
}
}
else {
if (flag6 == 0) {
flag6 = 1;
view.setBackgroundColor(0xFF67FF5A);
counter1++;
if(counter1 == 3)
BlueAllBoxes();
}
}
}
public void button7(View view) {
if(counter1 == 3){
if(flag7 == 1) {
view.setBackgroundColor(0xFF67FF5A);
counter2++;
if(playerone) {
score1 = score1 + 30;
tex1.setText(firstplayer + " : " + score1 );
}
else {
score2 = score2 + 30;
tex2.setText(secondplayer + " : " + score2 );
}
if(counter2 == 3)
refresh();
}
else{
view.setBackgroundColor(0xFFFF231E);
if(playerone) {
score1 = score1 - 10;
tex1.setText(firstplayer + " : " + score1);
}
else {
score2 = score2 - 10;
tex2.setText(secondplayer + " : " + score2 );
}
}
}
else {
flag7 = 1;
view.setBackgroundColor(0xFF67FF5A);
counter1++;
if(counter1 == 3)
BlueAllBoxes();
}
}
public void button8(View view) {
if(counter1 == 3){
if(flag8 == 1) {
view.setBackgroundColor(0xFF67FF5A);
counter2++;
if(playerone) {
score1 = score1 + 30;
tex1.setText(firstplayer + " : " + score1 );
}
else {
score2 = score2 + 30;
tex2.setText(secondplayer + " : " + score2 );
}
if(counter2 == 3)
refresh();
}
else{
view.setBackgroundColor(0xFFFF231E);
if(playerone) {
score1 = score1 - 10;
tex1.setText(firstplayer + " : " + score1 );
}
else {
score2 = score2 - 10;
tex2.setText(secondplayer + " : " + score2 );
}
}
}
else {
flag8 = 1;
view.setBackgroundColor(0xFF67FF5A);
counter1++;
if(counter1 == 3)
BlueAllBoxes();
}
}
public void button9(View view) {
if(counter1 == 3){
if(flag9 == 1) {
view.setBackgroundColor(0xFF67FF5A);
counter2++;
if(playerone) {
score1 = score1 + 30;
tex1.setText(firstplayer + " : " + score1 );
}
else {
score2 = score2 + 30;
tex2.setText(secondplayer + " : " + score2 );
}
if(counter2 == 3)
refresh();
}
else{
view.setBackgroundColor(0xFFFF231E);
if(playerone) {
score1 = score1 - 10;
tex1.setText(firstplayer + " : " + score1 );
}
else {
score2 = score2 - 10;
tex2.setText(secondplayer + " : " + score2 );
}
}
}
else {
flag9 = 1;
view.setBackgroundColor(0xFF67FF5A);
counter1++;
if(counter1 == 3)
BlueAllBoxes();
}
}
public void button10(View view) {
if(counter1 == 3){
if(flag10 == 1) {
view.setBackgroundColor(0xFF67FF5A);
counter2++;
if(playerone) {
score1 = score1 + 30;
tex1.setText(firstplayer + " : " + score1 );
}
else {
score2 = score2 + 30;
tex2.setText(secondplayer + " : " + score2 );
}
if(counter2 == 3)
refresh();
}
else{
view.setBackgroundColor(0xFFFF231E);
if(playerone) {
score1 = score1 - 10;
tex1.setText(firstplayer + " : " + score1 );
}
else {
score2 = score2 - 10;
tex2.setText(secondplayer + " : " + score2 );
}
}
}
else {
flag10 = 1;
view.setBackgroundColor(0xFF67FF5A);
counter1++;
if(counter1 == 3)
BlueAllBoxes();
}
}
public void button11(View view) {
if(counter1 == 3){
if(flag11 == 1) {
view.setBackgroundColor(0xFF67FF5A);
counter2++;
if(playerone) {
score1 = score1 + 30;
tex1.setText(firstplayer + " : " + score1 );
}
else {
score2 = score2 + 30;
tex2.setText(secondplayer + " : " + score2 );
}
if(counter2 == 3)
refresh();
}
else{
view.setBackgroundColor(0xFFFF231E);
if(playerone) {
score1 = score1 - 10;
tex1.setText(firstplayer + " : " + score1 );
}
else {
score2 = score2 - 10;
tex2.setText(secondplayer + " : " + score2);
}
}
}
else {
flag11 = 1;
view.setBackgroundColor(0xFF67FF5A);
counter1++;
if(counter1 == 3)
BlueAllBoxes();
}
}
public void button12(View view) {
if(counter1 == 3){
if(flag12 == 1) {
view.setBackgroundColor(0xFF67FF5A);
counter2++;
if(playerone) {
score1 = score1 + 30;
tex1.setText(firstplayer + " : " + score1 );
}
else {
score2 = score2 + 30;
tex2.setText(secondplayer + " : " + score2);
}
if(counter2 == 3)
refresh();
}
else{
view.setBackgroundColor(0xFFFF231E);
if(playerone) {
score1 = score1 - 10;
tex1.setText(firstplayer + " : " + score1 );
}
else {
score2 = score2 - 10;
tex2.setText(secondplayer + " : " + score2);
}
}
}
else {
flag12 = 1;
view.setBackgroundColor(0xFF67FF5A);
counter1++;
if(counter1 == 3)
BlueAllBoxes();
}
}
}
我认为findviewbyd(R.id.editText);返回NULL,但它不应返回NULL,因为我在以前的活动中有EditText id为的EditText这里是我以前的XML活动
我想从上一个活动中的EditText中获取文本,然后在当前活动中使用该字符串。我该怎么做
我的代码中的player1是上一个活动中的编辑文本之一
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
public class startwith2player extends ActionBarActivity {
private boolean playerone = true , playertwo = false ;
private Integer score1 = 0 , score2 = 0;
private Integer flag4 = 0 , flag5 = 0 , flag6 = 0 , flag7 = 0, flag8 = 0, flag9 = 0 , flag10 = 0, flag11 = 0, flag12 = 0;
private Integer counter1 = 0 , counter2 = 0;
TextView tex1 ;
TextView tex2 ;
String firstplayer = "";
String secondplayer = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_startwith2player);
tex1 = (TextView) findViewById(R.id.textView3);
tex2 = (TextView) findViewById(R.id.textView4);
EditText player1 = (EditText)findViewById(R.id.editText);
EditText player2 = (EditText)findViewById(R.id.editText2);
firstplayer = player1.getText().toString();
secondplayer = player2.getText().toString();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_startwith2player, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void refresh(){
BlueAllBoxes();
if(playerone){
playerone = false;
playertwo = true;
}
else{
playerone = true;
playertwo = false;
}
flag4 = 0 ; flag5 = 0 ; flag6 = 0 ; flag7 = 0; flag8 = 0; flag9 = 0 ; flag10 = 0; flag11 = 0; flag12 = 0;
counter1 = 0 ; counter2 = 0;
}
public void BlueAllBoxes(){
Button button4 = (Button) findViewById(R.id.button4);
button4.setBackgroundColor(0xFF4B4BFF);
Button button5 = (Button) findViewById(R.id.button5);
button5.setBackgroundColor(0xFF4B4BFF);
Button button6 = (Button) findViewById(R.id.button6);
button6.setBackgroundColor(0xFF4B4BFF);
Button button7 = (Button) findViewById(R.id.button7);
button7.setBackgroundColor(0xFF4B4BFF);
Button button8 = (Button) findViewById(R.id.button8);
button8.setBackgroundColor(0xFF4B4BFF);
Button button9 = (Button) findViewById(R.id.button9);
button9.setBackgroundColor(0xFF4B4BFF);
Button button10 = (Button) findViewById(R.id.button10);
button10.setBackgroundColor(0xFF4B4BFF);
Button button11 = (Button) findViewById(R.id.button11);
button11.setBackgroundColor(0xFF4B4BFF);
Button button12 = (Button) findViewById(R.id.button12);
button12.setBackgroundColor(0xFF4B4BFF);
}
public void button4(View view) {
if(counter1 == 3){
if(flag4 == 1) {
view.setBackgroundColor(0xFF67FF5A);
counter2++;
if(playerone) {
score1 = score1 + 30;
// tex1.setText(String.valueOf(score1));
tex1.setText(firstplayer + " : " + score1 );
}
else {
score2 = score2 + 30;
tex2.setText(secondplayer + " : " + score2 );
}
if(counter2 == 3)
refresh();
}
else{
view.setBackgroundColor(0xFFFF231E);
if(playerone) {
score1 = score1 - 10;
tex1.setText(firstplayer + " : " + score1 );
}
else {
score2 = score2 - 10;
tex2.setText(secondplayer + " : " + score2 );
}
}
}
else {
if (flag4 == 0) {
flag4 = 1;
view.setBackgroundColor(0xFF67FF5A);
counter1++;
if(counter1 == 3)
BlueAllBoxes();
}
}
}
public void button5(View view) {
if(counter1 == 3){
if(flag5 == 1) {
view.setBackgroundColor(0xFF67FF5A);
counter2++;
if(playerone) {
score1 = score1 + 30;
tex1.setText(firstplayer + " : " + score1 );
}
else {
score2 = score2 + 30;
tex2.setText(secondplayer + " : " + score2 );
}
if(counter2 == 3)
refresh();
}
else{
view.setBackgroundColor(0xFFFF231E);
if(playerone) {
score1 = score1 - 10;
tex1.setText(firstplayer + " : " + score1 );
}
else {
score2 = score2 - 10;
tex2.setText(secondplayer + " : " + score2 );
}
}
}
else {
if (flag5 == 0) {
flag5 = 1;
view.setBackgroundColor(0xFF67FF5A);
counter1++;
if(counter1 == 3)
BlueAllBoxes();
}
}
}
public void button6(View view) {
if(counter1 == 3){
if(flag6 == 1) {
view.setBackgroundColor(0xFF67FF5A);
counter2++;
if(playerone) {
score1 = score1 + 30;
tex1.setText(firstplayer + " : " + score1 );
}
else {
score2 = score2 + 30;
tex2.setText(secondplayer + " : " + score2 );
}
if(counter2 == 3)
refresh();
}
else{
view.setBackgroundColor(0xFFFF231E);
if(playerone) {
score1 = score1 - 10;
tex1.setText(firstplayer + " : " + score1 );
}
else {
score2 = score2 - 10;
tex2.setText(secondplayer + " : " + score2 );
}
}
}
else {
if (flag6 == 0) {
flag6 = 1;
view.setBackgroundColor(0xFF67FF5A);
counter1++;
if(counter1 == 3)
BlueAllBoxes();
}
}
}
public void button7(View view) {
if(counter1 == 3){
if(flag7 == 1) {
view.setBackgroundColor(0xFF67FF5A);
counter2++;
if(playerone) {
score1 = score1 + 30;
tex1.setText(firstplayer + " : " + score1 );
}
else {
score2 = score2 + 30;
tex2.setText(secondplayer + " : " + score2 );
}
if(counter2 == 3)
refresh();
}
else{
view.setBackgroundColor(0xFFFF231E);
if(playerone) {
score1 = score1 - 10;
tex1.setText(firstplayer + " : " + score1);
}
else {
score2 = score2 - 10;
tex2.setText(secondplayer + " : " + score2 );
}
}
}
else {
flag7 = 1;
view.setBackgroundColor(0xFF67FF5A);
counter1++;
if(counter1 == 3)
BlueAllBoxes();
}
}
public void button8(View view) {
if(counter1 == 3){
if(flag8 == 1) {
view.setBackgroundColor(0xFF67FF5A);
counter2++;
if(playerone) {
score1 = score1 + 30;
tex1.setText(firstplayer + " : " + score1 );
}
else {
score2 = score2 + 30;
tex2.setText(secondplayer + " : " + score2 );
}
if(counter2 == 3)
refresh();
}
else{
view.setBackgroundColor(0xFFFF231E);
if(playerone) {
score1 = score1 - 10;
tex1.setText(firstplayer + " : " + score1 );
}
else {
score2 = score2 - 10;
tex2.setText(secondplayer + " : " + score2 );
}
}
}
else {
flag8 = 1;
view.setBackgroundColor(0xFF67FF5A);
counter1++;
if(counter1 == 3)
BlueAllBoxes();
}
}
public void button9(View view) {
if(counter1 == 3){
if(flag9 == 1) {
view.setBackgroundColor(0xFF67FF5A);
counter2++;
if(playerone) {
score1 = score1 + 30;
tex1.setText(firstplayer + " : " + score1 );
}
else {
score2 = score2 + 30;
tex2.setText(secondplayer + " : " + score2 );
}
if(counter2 == 3)
refresh();
}
else{
view.setBackgroundColor(0xFFFF231E);
if(playerone) {
score1 = score1 - 10;
tex1.setText(firstplayer + " : " + score1 );
}
else {
score2 = score2 - 10;
tex2.setText(secondplayer + " : " + score2 );
}
}
}
else {
flag9 = 1;
view.setBackgroundColor(0xFF67FF5A);
counter1++;
if(counter1 == 3)
BlueAllBoxes();
}
}
public void button10(View view) {
if(counter1 == 3){
if(flag10 == 1) {
view.setBackgroundColor(0xFF67FF5A);
counter2++;
if(playerone) {
score1 = score1 + 30;
tex1.setText(firstplayer + " : " + score1 );
}
else {
score2 = score2 + 30;
tex2.setText(secondplayer + " : " + score2 );
}
if(counter2 == 3)
refresh();
}
else{
view.setBackgroundColor(0xFFFF231E);
if(playerone) {
score1 = score1 - 10;
tex1.setText(firstplayer + " : " + score1 );
}
else {
score2 = score2 - 10;
tex2.setText(secondplayer + " : " + score2 );
}
}
}
else {
flag10 = 1;
view.setBackgroundColor(0xFF67FF5A);
counter1++;
if(counter1 == 3)
BlueAllBoxes();
}
}
public void button11(View view) {
if(counter1 == 3){
if(flag11 == 1) {
view.setBackgroundColor(0xFF67FF5A);
counter2++;
if(playerone) {
score1 = score1 + 30;
tex1.setText(firstplayer + " : " + score1 );
}
else {
score2 = score2 + 30;
tex2.setText(secondplayer + " : " + score2 );
}
if(counter2 == 3)
refresh();
}
else{
view.setBackgroundColor(0xFFFF231E);
if(playerone) {
score1 = score1 - 10;
tex1.setText(firstplayer + " : " + score1 );
}
else {
score2 = score2 - 10;
tex2.setText(secondplayer + " : " + score2);
}
}
}
else {
flag11 = 1;
view.setBackgroundColor(0xFF67FF5A);
counter1++;
if(counter1 == 3)
BlueAllBoxes();
}
}
public void button12(View view) {
if(counter1 == 3){
if(flag12 == 1) {
view.setBackgroundColor(0xFF67FF5A);
counter2++;
if(playerone) {
score1 = score1 + 30;
tex1.setText(firstplayer + " : " + score1 );
}
else {
score2 = score2 + 30;
tex2.setText(secondplayer + " : " + score2);
}
if(counter2 == 3)
refresh();
}
else{
view.setBackgroundColor(0xFFFF231E);
if(playerone) {
score1 = score1 - 10;
tex1.setText(firstplayer + " : " + score1 );
}
else {
score2 = score2 - 10;
tex2.setText(secondplayer + " : " + score2);
}
}
}
else {
flag12 = 1;
view.setBackgroundColor(0xFF67FF5A);
counter1++;
if(counter1 == 3)
BlueAllBoxes();
}
}
}
这是一种糟糕的设计实践,如果没有实质性的欺骗,这是行不通的
我认为findviewbyd(R.id.editText);返回NULL,但它不应返回NULL,因为我在以前的活动中有EditText id为的EditText这里是我以前的XML活动
您无法通过这种方式获得对另一个活动视图的引用,即使可以,您也不应该假设,一旦该活动离开屏幕并且您当前的活动已替换它,这些视图仍然有效
相反,让第一个活动将此信息转发给第二个活动作为额外的意图。如果信息是相当持久的,你也可以考虑把它放在共享的首选项中。在某些情况下,将其存储为应用程序对象的字段可能是合适的,在一些特殊情况下,不可能使用全局变量
始终仔细检查前一项活动可能未获得该值的可能性;事实上,取决于清单的设置方式,如果Android需要终止并重新创建您的进程,那么之前的活动可能从未运行过,或者在当前进程中可能没有运行过,因为在此期间运行了其他资源匮乏的程序。答案很简单。文本为空。看它上面的那条线
EditText player1 = (EditText)findViewById(R.id.editText);
EditText player2 = (EditText)findViewById(R.id.editText2);
firstplayer = player1.getText().toString();
secondplayer = player2.getText().toString();
您已经初始化了为空的player1和player2,然后可以使用firstplayer=player1.getText().toString();从空的player1获取文本
我的建议是添加按钮,然后使用gettext()获取其值。我对本机Android开发一无所知,但很明显,
findViewById(R.id.editText)
正在返回null
。我在这里看到了一大堆关于Android资源的问题,所以我猜在资源定义上有一些问题,你在寻找什么。在Android中,当它发生时,你认为它不应该清理项目。如何调用取决于哪个IDE,但通常在“构建”菜单中。之所以会出现错误,是因为调用的edittext不在活动的xml范围内,即R.layout.activity\u startwith2player。这不是从其他活动中获取文本的正确方法。使用意图将数据从一个活动传递到另一个活动。从上一个活动中,将意图传递给第二个活动。Intent Intent=新Intent(这个,startwith2player.class);intent.putExtra(“firstplayer”,player1.getText().toString());意向性;在startwith2player活动中,删除player1的编辑文本,因为这将导致错误。您可以通过字符串firstplayer=intent.getStringExtra(“firstplayer”)获取从上一个活动传递的数据;这不是问题所在