Android Intent使我的应用程序崩溃,为什么?

时间:2013-11-02 13:45:29

标签: java android debugging android-layout android-intent

所以我尝试了我的应用程序而没有通过得分变量,它工作。但是,如果我尝试使用它,它根本不起作用。

主要课程:

int start = 10;



// start Play Intent
public void onPlay(View view){
    Intent playIntent = new Intent(this, Quiz_1.class);
    playIntent.putExtra("Score", start);
    startActivity(playIntent);
}

Quiz_1类:

public class Quiz_1 extends Activity {
int printScore;
TextView printPoints;


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.quiz_1);
    printPoints = (TextView) findViewById(R.id.q_result);
}

Intent intent = getIntent();
int score = intent.getIntExtra("Score", 0);
String printFin = String.valueOf(score);

public void q_result(View view){
    printPoints.setText(printFin);
}

我确信,当我尝试将起始值传递给我的其他活动时,我做错了什么。在那里我想将textView的文本更改为前一个int start value = 10;

所以,

第一项活动: int t start = 10;

二。活动: int score = start;
printScore(它是一个TextView)setText = score

5 个答案:

答案 0 :(得分:4)

如果要将值传递给另一个意图,可以查看下面的代码。

您可以像在意图中一样放置键值对,并且可以在您调用的活动中检索这些值。您可以查看: -

Intent returnIntent = new Intent(getApplicationContext, SecondActivity.class);
returnIntent.putExtra("name","abc");
startActivity(returnIntent,11);

在你的其他活动结果方法中,你可以做到

Bundle extras = getIntent().getExtras(); 
if (extras != null) {
String name = bundle.getString("name");
    // and get whatever data you are adding
}

你可以调查一下。

答案 1 :(得分:0)

所有Java代码都必须在方法中。这里,最后三行不是任何方法,因此不执行。

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.quiz_1);
    printPoints = (TextView) findViewById(R.id.q_result);

} <----- move this closing brace

Intent intent = getIntent();
int score = intent.getIntExtra("Score", 0);
String printFin = String.valueOf(score);    

} <----- to here

答案 2 :(得分:0)

您需要在onCreate

之类的方法中使用这些内容
Intent intent = getIntent();
int score = intent.getIntExtra("Score", 0);
String printFin = String.valueOf(score);

然而,崩溃可能仍然会发生,因此更好地发布堆栈跟踪

编辑:

String printFin; //declare as class member
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.quiz_1);
    printPoints = (TextView) findViewById(R.id.q_result);
    Intent intent = getIntent();
    int score = intent.getIntExtra("Score", 0);
    printFin = String.valueOf(score);
}

答案 3 :(得分:0)

将所有代码更改为

public class Quiz_1 extends Activity {
int printScore;
TextView printPoints;


int score = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
      setContentView(R.layout.quiz_1);

     Bundle extras = getIntent().getExtras(); 
    if (extras != null) 
     {
       score   = extras.getInt("Score");
    }


    printPoints = (TextView) findViewById(R.id.q_result);
 printPoints.setText(String.valueOf(score));
}

答案 4 :(得分:0)

我已经浏览了这个链接 - http://pastebin.com/BGAkdeCv

我可能发现了错误

案例1: - 您在passScore密钥中传递defaultScore值并在Quiz1.Java中检索它 MainActivity.Java - &gt;

startQuiz.putExtra("passScore", defaultScore);

Quiz_1.Java - &gt;

  current_score   = extras.getInt("passScore");

案例2: - 您在passNewScore中传递current_score并在Quiz2.Java中检索它 Quiz_1.Java - &gt;

   quiz1.putExtra("passNewScore", current_score);

Quiz2.Java

  current_score   = extras.getInt("passScore");
  getScore = extras.getInt("passNewScore");

现在,这里的错误是: - 在quiz2.java中,你在current_score 中提取 passScore,因为这个键值放在你的main_activity中,而不是你调用quiz2.java的quiz1.java。,

所以要在quiz2.java中检索这个值,你必须在你调用quiz2 intent的quiz1.java中再次传递它

要更正它,请修改quiz1.java: -

public void on_quiz_1_wrong(View view){  // button clicked the wrong answer
   current_score = current_score - 2;
   Intent quiz1 = new Intent(this, Quiz_2.class);
   startActivity(quiz1);
   quiz1.putExtra("passNewScore", current_score);
   quiz1.putExtra("passScore", whatever value you want to pass here);

}