Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8688079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:16:11+00:00 2026-06-12T23:16:11+00:00

I’m new to Android and trying to learn how to pass some variables from

  • 0

I’m new to Android and trying to learn how to pass some variables from activity to another activity by Intents. The problem is when I run the app after finishing answering my questions it will take me to the Summary activity which has the result of the AskQusetions activity (the count of yes and no)

Here is my AskQuestion Activity:

    package com.example.quizapp;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.TextView;


public class AskQuestions extends Activity {

    private String[] messages;
    private int yesCount;
    private int noCount;
    private int messageNum;
    public final static String EXTRA_MESSAGE = "com.example.quizapp.MESSAGE";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ask_questions);

    }

    @Override
    public void onStart() {
        super.onStart();
        messages = getResources().getStringArray(R.array.lblask_questions);
        messageNum = 0;
        setYesCount(0);
        setNoCount(0);

        TextView lblQuestion = (TextView)findViewById(R.id.lblask_questions);
        lblQuestion.setText(messages[messageNum]);
    }

    public void onYes(View view) {
        Log.i("Quiz App", "viewing next question");

        setYesCount(getYesCount() + 1); // count yes
        messageNum++;
        if (messageNum >= messages.length) { // if the questions finished got to summary
            // go to summary
            Intent summaryIntent = new Intent(this, Summary.class);
            //int messageYes = getYesCount();
            summaryIntent.putExtra("value", getYesCount());
            startActivity(summaryIntent);
        }

        TextView lblQuestion = (TextView)findViewById(R.id.lblask_questions);
        lblQuestion.setText(messages[messageNum]);
    }

    public void onNo(View view) {
        Log.i("Quiz App", "viewing next question");

        setNoCount(getNoCount() + 1); // count not
        messageNum++;
        if (messageNum >= messages.length) { // if the questions finished got to summary
            // go to summary
            Intent summaryIntent = new Intent(this, Summary.class);
            summaryIntent.putExtra("value", getNoCount()); 
            startActivity(summaryIntent);
        }



        TextView lblQuestion = (TextView)findViewById(R.id.lblask_questions);
        lblQuestion.setText(messages[messageNum]);
    }

    public int getYesCount() {
        return yesCount;
    }

    public void setYesCount(int yesCount) {
        this.yesCount = yesCount;
    }

    public int getNoCount() {
        return noCount;
    }

    public void setNoCount(int noCount) {
        this.noCount = noCount;
    }

}

Here is my Summary Activity:

package com.example.quizapp;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.TextView;


/**
 * @author ***
 *
 */
public class Summary extends Activity {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Bundle extras = getIntent().getExtras();
        //String value = extras.getString(AskQuestions.EXTRA_MESSAGE);


        setContentView(R.layout.summary);

    }
    @Override
    public void onStart() {
        super.onStart();

        //String value = "HELLO";

        TextView lblYes = (TextView)findViewById(R.id.yes_result);
        lblYes.setText(getIntent().getExtras().getString("value"));

        TextView lblNo = (TextView)findViewById(R.id.no_result);
        lblNo.setText(getIntent().getExtras().getString("value"));


    }


}

The error (app crashes):

ScreenShot App Crash

The consol output:

[2012-10-16 01:28:04 – QuizApp] Android Launch!
[2012-10-16 01:28:04 – QuizApp] adb is running normally.
[2012-10-16 01:28:04 – QuizApp] Performing com.example.quizapp.MainActivity activity launch
[2012-10-16 01:28:04 – QuizApp] Automatic Target Mode: using existing emulator ’emulator-5554′ running compatible AVD ‘firstDevice’
[2012-10-16 01:28:05 – QuizApp] Attempting to connect debugger to ‘com.example.quizapp’ on port 8621
[2012-10-16 01:28:07 – QuizApp] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.quizapp/.MainActivity }
[2012-10-16 01:28:08 – QuizApp] Application already deployed. No need to reinstall.
[2012-10-16 01:28:08 – QuizApp] Starting activity com.example.quizapp.MainActivity on device emulator-5554
[2012-10-16 01:28:10 – QuizApp] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.quizapp/.MainActivity }
[2012-10-16 01:28:10 – QuizApp] Attempting to connect debugger to ‘com.example.quizapp’ on port 8621
[2012-10-16 01:28:12 – QuizApp] Attempting to connect debugger to ‘com.example.quizapp’ on port 8621

Logcat error (after restarting the eclipse):

10-16 02:17:34.469: D/AndroidRuntime(1496): Shutting down VM
10-16 02:17:34.469: W/dalvikvm(1496): threadid=1: thread exiting with uncaught exception (group=0xb3f68288)
10-16 02:17:34.479: E/AndroidRuntime(1496): FATAL EXCEPTION: main
10-16 02:17:34.479: E/AndroidRuntime(1496): java.lang.IllegalStateException: Could not execute method of the activity
10-16 02:17:34.479: E/AndroidRuntime(1496):     at android.view.View$1.onClick(View.java:3591)
10-16 02:17:34.479: E/AndroidRuntime(1496):     at android.view.View.performClick(View.java:4084)
10-16 02:17:34.479: E/AndroidRuntime(1496):     at android.view.View$PerformClick.run(View.java:16966)
10-16 02:17:34.479: E/AndroidRuntime(1496):     at android.os.Handler.handleCallback(Handler.java:615)
10-16 02:17:34.479: E/AndroidRuntime(1496):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-16 02:17:34.479: E/AndroidRuntime(1496):     at android.os.Looper.loop(Looper.java:137)
10-16 02:17:34.479: E/AndroidRuntime(1496):     at android.app.ActivityThread.main(ActivityThread.java:4745)
10-16 02:17:34.479: E/AndroidRuntime(1496):     at java.lang.reflect.Method.invokeNative(Native Method)
10-16 02:17:34.479: E/AndroidRuntime(1496):     at java.lang.reflect.Method.invoke(Method.java:511)
10-16 02:17:34.479: E/AndroidRuntime(1496):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-16 02:17:34.479: E/AndroidRuntime(1496):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-16 02:17:34.479: E/AndroidRuntime(1496):     at dalvik.system.NativeStart.main(Native Method)
10-16 02:17:34.479: E/AndroidRuntime(1496): Caused by: java.lang.reflect.InvocationTargetException
10-16 02:17:34.479: E/AndroidRuntime(1496):     at java.lang.reflect.Method.invokeNative(Native Method)
10-16 02:17:34.479: E/AndroidRuntime(1496):     at java.lang.reflect.Method.invoke(Method.java:511)
10-16 02:17:34.479: E/AndroidRuntime(1496):     at android.view.View$1.onClick(View.java:3586)
10-16 02:17:34.479: E/AndroidRuntime(1496):     ... 11 more
10-16 02:17:34.479: E/AndroidRuntime(1496): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=5; index=5
10-16 02:17:34.479: E/AndroidRuntime(1496):     at com.example.quizapp.AskQuestions.onYes(AskQuestions.java:56)
10-16 02:17:34.479: E/AndroidRuntime(1496):     ... 14 more
10-16 02:17:38.499: E/Trace(1604): error opening trace file: No such file or directory (2)
10-16 02:17:39.020: D/gralloc_goldfish(1604): Emulator without GPU emulation detected.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:16:12+00:00Added an answer on June 12, 2026 at 11:16 pm

    The first paramater to putExtra() is a String with the package name of the application AND the name of the value, for instance “com.example.quizapp.yesValues” or “com.example.quizapp.noValues”

    You then retrieve the values via

    getIntent().getExtras().getInt("com.example.quizapp.yesValues", -1);
    getIntent.getExtras().getInt("com.example.quizapp.noValues", -1);
    

    note the second parameter in getInt() is the default value

    Your second error is due to IndexOutOfBoundsException. You are trying to access an index that does not exist in your array at the line:

    lblQuestion.setText(messages[messageNum]);
    

    In your onYes() and onNo() methods. Notice right above it you check that messageNum >= messages.length, then immediately after you address the array by it. Do that inside of a check

    if(messageNum < messages.length)
    

    good luck and happy coding!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.