7 Replies - 649 Views - Last Post: 11 September 2015 - 06:24 AM

#1 parkie02   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 24
  • Joined: 15-May 13

Pass variable from one activity to another

Posted 09 September 2015 - 07:36 AM

I want to show the result activity on the end of the quiz which is working. And then show the scores activity on press of a button on the main activity
Advice I got is to Create a listener for the scores button on Main Activity and add code to launch the Scores Activity with an intent where you pass the score. And then retrieve the bundle on the onCreate method on your ScoresActivity. Can someone help me on this please regarding the code.

MainActivity
package app.mobiledevicesecurity;
 
import android.content.Intent;
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.Toast;
 

public class MainActivity extends ActionBarActivity {
    DatabaseHelper myDb;
    private static Button readbtn;
    private static Button quizbtn;
    private static Button scoresbtn;
    private static Button settingsbtn;
    private static Button helpbtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myDb = new DatabaseHelper(this);
        myDb.insertData();
 
         onclickReadButtonListener();
         onclickQuizButtonListener();
         onclickScoresButtonListener();
         onclickSettingsButtonListener();
         onclickHelpButtonListener();
 

    }
 
    public void onclickReadButtonListener() {
        readbtn = (Button) findViewById(R.id.readbutton);
        readbtn.setonclickListener(
                new View.onclickListener() {
                    @Override
                    public void onclick(View v) {
                        Intent intent = new Intent("app.mobiledevicesecurity.Read_Category");
                        startActivity(intent);
                    }
                }
        );
    }
 
    public void onclickQuizButtonListener() {
        quizbtn = (Button) findViewById(R.id.quizbutton);
        quizbtn.setonclickListener(
                new View.onclickListener() {
                    @Override
                    public void onclick(View v) {
                        Intent intent = new Intent("app.mobiledevicesecurity.Quiz");
                        startActivity(intent);
                    }
                }
        );
    }
 
    public void onclickScoresButtonListener() {
        scoresbtn = (Button) findViewById(R.id.scoresbutton);
        scoresbtn.setonclickListener(
                new View.onclickListener() {
                    @Override
                    public void onclick(View v) {
                        Intent intent = new Intent("app.mobiledevicesecurity.Scores");
                        startActivity(intent);
                    }
                }
        );
       
    }
 
    public void onclickSettingsButtonListener() {
        settingsbtn = (Button) findViewById(R.id.settingsbutton);
        settingsbtn.setonclickListener(
                new View.onclickListener() {
                    @Override
                    public void onclick(View v) {
                        Intent intent = new Intent("app.mobiledevicesecurity.Settings");
                        startActivity(intent);
                    }
                }
        );
    }
 
    public void onclickHelpButtonListener() {
        helpbtn = (Button) findViewById(R.id.helpbutton);
        helpbtn.setonclickListener(
                new View.onclickListener() {
                    @Override
                    public void onclick(View v) {
                        Intent intent = new Intent("app.mobiledevicesecurity.Help");
                        startActivity(intent);
                    }
                }
        );
 

    }
 
    @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_main, menu);
        return true;
    }
 
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
 
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
 
        return super.onOptionsItemSelected(item);
    }
}


Result
package app.mobiledevicesecurity;
 
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
 
public class Result extends Activity {
 
    private static Button playbtn;
    private static Button menubutton;
    int score;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);
        onclickPlayButtonListener();
        onclickMenuButtonListener();
        TextView textResult = (TextView) findViewById(R.id.textResult);
        Bundle b = getIntent().getExtras();
        score = b.getInt("score");
        textResult.setText("You scored" + " " + score + " for the quiz.");
 

    }
 
    public void getScore()
    {
       
        Intent intent2 = new Intent(Result.this,
                Scores.class);
        Bundle bun = new Bundle();
        bun.putInt("score", score);
        intent2.putExtras(bun);
        startActivity(intent2);
        finish();
    }
    public void onclickPlayButtonListener() {
        playbtn = (Button) findViewById(R.id.btn);
        playbtn.setonclickListener(
                new View.onclickListener() {
                    @Override
                    public void onclick(View v) {
                        Intent intent = new Intent("app.mobiledevicesecurity.Quiz");
                        startActivity(intent);
                    }
                }
        );
    }
 
    public void onclickMenuButtonListener() {
        menubutton = (Button) findViewById(R.id.menubtn);
        menubutton.setonclickListener(
                new View.onclickListener() {
                    @Override
                    public void onclick(View v) {
                        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                        startActivity(intent);
                    }
                }
        );
    }
}


Scores
package app.mobiledevicesecurity;
 
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.content.Intent;
 
public class Scores extends ActionBarActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scores);
 
        Result res = new Result();
        res.getScore();
 
        TextView txtScore1 = (TextView) findViewById(R.id.txtScore1);
        Bundle bun = getIntent().getExtras();
        int score = bun.getInt("score");
        txtScore1.setText("Last quiz score:" + " " + score + ".");
    }
 
    @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_scores, menu);
        return true;
    }
 
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
 
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
 
        return super.onOptionsItemSelected(item);
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Pass variable from one activity to another

#2 Pogaca   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 149
  • Joined: 24-August 13

Re: Pass variable from one activity to another

Posted 09 September 2015 - 11:52 PM

http://stackoverflow...ties-in-android
Was This Post Helpful? 0
  • +
  • -

#3 parkie02   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 24
  • Joined: 15-May 13

Re: Pass variable from one activity to another

Posted 10 September 2015 - 12:01 AM

Can you maybe help me on my code using this. Because I tried but do not get right
Was This Post Helpful? 0
  • +
  • -

#4 Pogaca   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 149
  • Joined: 24-August 13

Re: Pass variable from one activity to another

Posted 10 September 2015 - 12:13 AM

In your current (main activity of the game) set intent towards score like this:

Intent i = new Intent(MainActivity.this, Scores.class);
i.putExtra("Scores",HERE GOES THE VARIABLE THAT STORED SCORE); //i.putExtra("Scores",score);
startActivity(i);


And for the scores activity add this:

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String value = extras.getString("Scores"); //Scores is the named variable you sent.
}

Was This Post Helpful? 0
  • +
  • -

#5 parkie02   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 24
  • Joined: 15-May 13

Re: Pass variable from one activity to another

Posted 10 September 2015 - 12:35 AM

The score variable is stored in the result activity. I think this is why there is an error when entering the code you supplied.

Here is the changed code. Can you maybe assist me.

MainActivity
package app.mobiledevicesecurity;

import android.content.Intent;
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.Toast;


public class MainActivity extends ActionBarActivity {
    DatabaseHelper myDb;
    private static Button readbtn;
    private static Button quizbtn;
    private static Button scoresbtn;
    private static Button settingsbtn;
    private static Button helpbtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myDb = new DatabaseHelper(this);
        myDb.insertData();

         onclickReadButtonListener();
         onclickQuizButtonListener();
         onclickScoresButtonListener();
         onclickSettingsButtonListener();
         onclickHelpButtonListener();


    }

    public void onclickReadButtonListener() {
        readbtn = (Button) findViewById(R.id.readbutton);
        readbtn.setonclickListener(
                new View.onclickListener() {
                    @Override
                    public void onclick(View v) {
                        Intent intent = new Intent("app.mobiledevicesecurity.Read_Category");
                        startActivity(intent);
                    }
                }
        );
    }

    public void onclickQuizButtonListener() {
        quizbtn = (Button) findViewById(R.id.quizbutton);
        quizbtn.setonclickListener(
                new View.onclickListener() {
                    @Override
                    public void onclick(View v) {
                        Intent intent = new Intent("app.mobiledevicesecurity.Quiz");
                        startActivity(intent);
                    }
                }
        );
    }

    public void onclickScoresButtonListener() {
        scoresbtn = (Button) findViewById(R.id.scoresbutton);
        Intent i = new Intent(MainActivity.this,Scores.class);
        i.putExtra("Scores", score);
        startActivity(i);
        scoresbtn.setonclickListener(
                new View.onclickListener() {
                    @Override
                    public void onclick(View v) {
                        Intent intent = new Intent("app.mobiledevicesecurity.Scores");
                        startActivity(intent);
                    }
                }
        );

    }

    public void onclickSettingsButtonListener() {
        settingsbtn = (Button) findViewById(R.id.settingsbutton);
        settingsbtn.setonclickListener(
                new View.onclickListener() {
                    @Override
                    public void onclick(View v) {
                        Intent intent = new Intent("app.mobiledevicesecurity.Settings");
                        startActivity(intent);
                    }
                }
        );
    }

    public void onclickHelpButtonListener() {
        helpbtn = (Button) findViewById(R.id.helpbutton);
        helpbtn.setonclickListener(
                new View.onclickListener() {
                    @Override
                    public void onclick(View v) {
                        Intent intent = new Intent("app.mobiledevicesecurity.Help");
                        startActivity(intent);
                    }
                }
        );


    }

    @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_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}


Scores.java
package app.mobiledevicesecurity;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.content.Intent;

public class Scores extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scores);

        Result res = new Result();
        res.getScore();

        TextView txtScore1 = (TextView) findViewById(R.id.txtScore1);
        //Bundle bun = getIntent().getExtras();
        //int score = bun.getInt("score");
        //txtScore1.setText("Last quiz score:" + " " + score + ".");

        Bundle extras = getIntent().getExtras();
        if (extras != null){
            String value = extras.getString("Scores");
            txtScore1.setText("Last quiz score:" + " " + value + ".");
        }

    }

    @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_scores, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}


Result.java
package app.mobiledevicesecurity;

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

public class Result extends Activity {

    private static Button playbtn;
    private static Button menubutton;
    int score;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);
        onclickPlayButtonListener();
        onclickMenuButtonListener();
        TextView textResult = (TextView) findViewById(R.id.textResult);
        Bundle b = getIntent().getExtras();
        score = b.getInt("score");
        textResult.setText("You scored" + " " + score + " for the quiz.");


    }

    public void getScore()
    {

        Intent intent2 = new Intent(Result.this,
                Scores.class);
        Bundle bun = new Bundle();
        bun.putInt("score", score);
        intent2.putExtras(bun);
        startActivity(intent2);
        finish();
    }
    public void onclickPlayButtonListener() {
        playbtn = (Button) findViewById(R.id.btn);
        playbtn.setonclickListener(
                new View.onclickListener() {
                    @Override
                    public void onclick(View v) {
                        Intent intent = new Intent("app.mobiledevicesecurity.Quiz");
                        startActivity(intent);
                    }
                }
        );
    }

    public void onclickMenuButtonListener() {
        menubutton = (Button) findViewById(R.id.menubtn);
        menubutton.setonclickListener(
                new View.onclickListener() {
                    @Override
                    public void onclick(View v) {
                        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                        startActivity(intent);
                    }
                }
        );
    }
}

Was This Post Helpful? 0
  • +
  • -

#6 parkie02   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 24
  • Joined: 15-May 13

Re: Pass variable from one activity to another

Posted 10 September 2015 - 01:43 AM

I deleted code. What I want to do is The score variable in result must be passed on to the scores activity to show the score for the quiz when a person clicks on the scores button in the main activity. Can someone help me with the code because everything I have tried for two days do not work.

MainActivity
package app.mobiledevicesecurity;

import android.content.Intent;
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.Toast;


public class MainActivity extends ActionBarActivity {
    DatabaseHelper myDb;
    private static Button readbtn;
    private static Button quizbtn;
    private static Button scoresbtn;
    private static Button settingsbtn;
    private static Button helpbtn;
    @Override

        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myDb = new DatabaseHelper(this);
        myDb.insertData();

         onclickReadButtonListener();
         onclickQuizButtonListener();
         onclickScoresButtonListener();
         onclickSettingsButtonListener();
         onclickHelpButtonListener();
    }

    public void onclickReadButtonListener() {
        readbtn = (Button) findViewById(R.id.readbutton);
        readbtn.setonclickListener(
                new View.onclickListener() {
                    @Override
                    public void onclick(View v) {
                        Intent intent = new Intent("app.mobiledevicesecurity.Read_Category");
                        startActivity(intent);
                    }
                }
        );
    }

    public void onclickQuizButtonListener() {
        quizbtn = (Button) findViewById(R.id.quizbutton);
        quizbtn.setonclickListener(
                new View.onclickListener() {
                    @Override
                    public void onclick(View v) {
                        Intent intent = new Intent("app.mobiledevicesecurity.Quiz");
                        startActivity(intent);
                    }
                }
        );
    }

    public void onclickScoresButtonListener() {
        scoresbtn = (Button) findViewById(R.id.scoresbutton);
        scoresbtn.setonclickListener(
                new View.onclickListener() {
                    @Override
                    public void onclick(View v) {
                        Intent intent = new Intent("app.mobiledevicesecurity.Scores");
                        startActivity(intent);
                    }
                }
        );

    }

    public void onclickSettingsButtonListener() {
        settingsbtn = (Button) findViewById(R.id.settingsbutton);
        settingsbtn.setonclickListener(
                new View.onclickListener() {
                    @Override
                    public void onclick(View v) {
                        Intent intent = new Intent("app.mobiledevicesecurity.Settings");
                        startActivity(intent);
                    }
                }
        );
    }

    public void onclickHelpButtonListener() {
        helpbtn = (Button) findViewById(R.id.helpbutton);
        helpbtn.setonclickListener(
                new View.onclickListener() {
                    @Override
                    public void onclick(View v) {
                        Intent intent = new Intent("app.mobiledevicesecurity.Help");
                        startActivity(intent);
                    }
                }
        );
    }

    @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_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}



Result
package app.mobiledevicesecurity;

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

public class Result extends Activity {

    private static Button playbtn;
    private static Button menubutton;
    int score;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);
        onclickPlayButtonListener();
        onclickMenuButtonListener();
        TextView textResult = (TextView) findViewById(R.id.textResult);
        Bundle b = getIntent().getExtras();
        score = b.getInt("score");
        textResult.setText("You scored" + " " + score + " for the quiz.");




    }

    public void onclickPlayButtonListener() {
        playbtn = (Button) findViewById(R.id.btn);
        playbtn.setonclickListener(
                new View.onclickListener() {
                    @Override
                    public void onclick(View v) {
                        Intent intent = new Intent("app.mobiledevicesecurity.Quiz");
                        startActivity(intent);
                    }
                }
        );
    }

    public void onclickMenuButtonListener() {
        menubutton = (Button) findViewById(R.id.menubtn);
        menubutton.setonclickListener(
                new View.onclickListener() {
                    @Override
                    public void onclick(View v) {
                        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                        startActivity(intent);
                    }
                }
        );
    }
}


Scores
package app.mobiledevicesecurity;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.content.Intent;

public class Scores extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scores);

        TextView txtScore1 = (TextView) findViewById(R.id.txtScore1);         
        txtScore1.setText("Last quiz score:" + " " + "" + ".");
        }

    @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_scores, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

Was This Post Helpful? 0
  • +
  • -

#7 ChrisNt   User is offline

  • cute bug

Reputation: 262
  • View blog
  • Posts: 896
  • Joined: 31-July 13

Re: Pass variable from one activity to another

Posted 11 September 2015 - 06:09 AM

Quote

Can someone help me with the code because everything I have tried for two days do not work.

Do you get any errors?
Was This Post Helpful? 0
  • +
  • -

#8 parkie02   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 24
  • Joined: 15-May 13

Re: Pass variable from one activity to another

Posted 11 September 2015 - 06:24 AM

Main Activity
package app.mobiledevicesecurity;

import android.content.Intent;
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.Toast;


public class MainActivity extends ActionBarActivity {
    DatabaseHelper myDb;
    private static Button readbtn;
    private static Button quizbtn;
    private static Button scoresbtn;
    private static Button settingsbtn;
    private static Button helpbtn;
    @Override

        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myDb = new DatabaseHelper(this);
        myDb.insertData();

         onclickReadButtonListener();
         onclickQuizButtonListener();
         onclickScoresButtonListener();
         onclickSettingsButtonListener();
         onclickHelpButtonListener();
    }

    public void onclickReadButtonListener() {
        readbtn = (Button) findViewById(R.id.readbutton);
        readbtn.setonclickListener(
                new View.onclickListener() {
                    @Override
                    public void onclick(View v) {
                        Intent intent = new Intent("app.mobiledevicesecurity.Read_Category");
                        startActivity(intent);
                    }
                }
        );
    }

    public void onclickQuizButtonListener() {
        quizbtn = (Button) findViewById(R.id.quizbutton);
        quizbtn.setonclickListener(
                new View.onclickListener() {
                    @Override
                    public void onclick(View v) {
                        Intent intent = new Intent("app.mobiledevicesecurity.Quiz");
                        startActivity(intent);
                    }
                }
        );
    }

    public void onclickScoresButtonListener() {
        scoresbtn = (Button) findViewById(R.id.scoresbutton);
        scoresbtn.setonclickListener(
                new View.onclickListener() {
                    @Override
                    public void onclick(View v) {
                        Intent intent = new Intent("app.mobiledevicesecurity.Scores");
                        startActivity(intent);
                    }
                }
        );

    }

    public void onclickSettingsButtonListener() {
        settingsbtn = (Button) findViewById(R.id.settingsbutton);
        settingsbtn.setonclickListener(
                new View.onclickListener() {
                    @Override
                    public void onclick(View v) {
                        Intent intent = new Intent("app.mobiledevicesecurity.Settings");
                        startActivity(intent);
                    }
                }
        );
    }

    public void onclickHelpButtonListener() {
        helpbtn = (Button) findViewById(R.id.helpbutton);
        helpbtn.setonclickListener(
                new View.onclickListener() {
                    @Override
                    public void onclick(View v) {
                        Intent intent = new Intent("app.mobiledevicesecurity.Help");
                        startActivity(intent);
                    }
                }
        );
    }

    @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_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}



Result
package app.mobiledevicesecurity;

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

public class Result extends Activity {

    private static Button playbtn;
    private static Button menubutton;
    int score;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);
        onclickPlayButtonListener();
        onclickMenuButtonListener();
        TextView textResult = (TextView) findViewById(R.id.textResult);
        Bundle b = getIntent().getExtras();
        score = b.getInt("score");
        textResult.setText("You scored" + " " + score + " for the quiz.");

    }

    public void getScore() {
        Intent intent = new Intent(Result.this,Scores.class);
        intent.putExtra("score",score);
        startActivity(intent);


    }
    public void onclickPlayButtonListener() {
        playbtn = (Button) findViewById(R.id.btn);
        playbtn.setonclickListener(
                new View.onclickListener() {
                    @Override
                    public void onclick(View v) {
                        Intent intent = new Intent("app.mobiledevicesecurity.Quiz");
                        startActivity(intent);
                    }
                }
        );
    }

    public void onclickMenuButtonListener() {
        menubutton = (Button) findViewById(R.id.menubtn);
        menubutton.setonclickListener(
                new View.onclickListener() {
                    @Override
                    public void onclick(View v) {
                        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                        startActivity(intent);
                    }
                }
        );
    }
}

Scores
package app.mobiledevicesecurity;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.content.Intent;

public class Scores extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scores);
        Result res = new Result();
        res.getScore();
        TextView txtScore1 = (TextView) findViewById(R.id.txtScore1);
        txtScore1.setText("Last quiz score:" + " " + res.score + ".");
        }

    @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_scores, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}


Logcat
09-11 15:23:36.506    1840-1840/app.mobiledevicesecurity E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: app.mobiledevicesecurity, PID: 1840
    java.lang.RuntimeException: Unable to start activity ComponentInfo{app.mobiledevicesecurity/app.mobiledevicesecurity.Scores}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
            at android.content.ContextWrapper.getPackageName(ContextWrapper.java:132)
            at android.content.ComponentName.<init>(ComponentName.java:77)
            at android.content.Intent.<init>(Intent.java:4160)
            at app.mobiledevicesecurity.Result.getScore(Result.java:30)
            at app.mobiledevicesecurity.Scores.onCreate(Scores.java:17)
            at android.app.Activity.performCreate(Activity.java:5990)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1