14 Replies - 1176 Views - Last Post: 22 September 2015 - 08:24 AM

#1 parkie02   User is offline

  • New D.I.C Head

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

Send variable data to other activity android

Posted 21 September 2015 - 04:29 AM

I have a quiz app. When the quiz is completed the result activity appears. It contains a score variable. The score must be sent to the scores activity where the score for the last quiz is shown. It all works fine but when clicking on the scores button on the main activity an error occur.

Here is the logcat when clicking on the Scores button:
09-21 13:19:52.680    4413-4413/app.mobiledevicesecurity E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: app.mobiledevicesecurity, PID: 4413
    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:33)
            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)


MainActivity.java
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);
    Bundle extras = getIntent().getExtras();
    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.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);
                }
            }
    );
}
}

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);
        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);
    }
    } 


Is This A Good Question/Topic? 0
  • +

Replies To: Send variable data to other activity android

#2 ChrisNt   User is offline

  • cute bug

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

Re: Send variable data to other activity android

Posted 21 September 2015 - 06:00 AM

You still have this problem..; You should try debugging the application or if you dont know how to debug you can comment out the lines that you think that are the cause of the error.

This post has been edited by ChrisNt: 21 September 2015 - 06:00 AM

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: Send variable data to other activity android

Posted 21 September 2015 - 06:10 AM

In the result activity the problem might be in lines 30 - 40.
And in scores activity it might be in lines 16 - 20.

I do not really know. I think it is a logic mistake that I am making. The score in the result activity must be sent to the scores activity and be shown in a text there.
Was This Post Helpful? 0
  • +
  • -

#4 ChrisNt   User is offline

  • cute bug

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

Re: Send variable data to other activity android

Posted 21 September 2015 - 06:17 AM

Try to remove (put in comment) those lines and see if the error appears again.

This post has been edited by ChrisNt: 21 September 2015 - 06:17 AM

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: Send variable data to other activity android

Posted 21 September 2015 - 10:06 AM

In the scores activity I have commented out the following and then there is no error, bit how do I get the score in the scores activity?

 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 + ".");

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: Send variable data to other activity android

Posted 21 September 2015 - 10:39 AM

In the scores activity I have commented out the following and then there is no error, bit how do I get the score in the scores activity?

 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 + ".");


So the problem is as res.getScore();

Any suggestions?
Was This Post Helpful? 0
  • +
  • -

#7 ChrisNt   User is offline

  • cute bug

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

Re: Send variable data to other activity android

Posted 21 September 2015 - 12:28 PM

In Scores Activity firstly you get the Bundle in which you did put the value of score

Bundle bundle = getIntent().getExtras();


and then you get the value from the Bundle by passing the key in the parameter which in your case is score.

int score = bundle.getInt("score");

This post has been edited by ChrisNt: 21 September 2015 - 12:33 PM

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: Send variable data to other activity android

Posted 21 September 2015 - 12:32 PM

View PostChrisNt, on 21 September 2015 - 12:28 PM, said:

In Scores Activity firstly you get the Bundle in which you did put the extras

Bundle bundle = getIntent().getExtras();


and then you get the value from the Bundle by passing the key in the parameter which in your case is score.

int score = bundle.getInt("score");


Is it not correct? What should it be?
Was This Post Helpful? 0
  • +
  • -

#9 ChrisNt   User is offline

  • cute bug

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

Re: Send variable data to other activity android

Posted 21 September 2015 - 12:38 PM

No , its not correct.Although you do not seem to understand what are you doing ..try to replace your code with the above in class Scores.

protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_scores);
      Result res = new Result();
      res.getScore();

      Bundle bundle = getIntent().getExtras();
      int score = bundle.getInt("score");

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

}

Was This Post Helpful? 0
  • +
  • -

#10 parkie02   User is offline

  • New D.I.C Head

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

Re: Send variable data to other activity android

Posted 21 September 2015 - 12:45 PM

Changed it as you said still get the error.

Logcat:
09-21 21:43:35.809    1872-1872/app.mobiledevicesecurity E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: app.mobiledevicesecurity, PID: 1872
    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:33)
            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)



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();

        Bundle bundle = getIntent().getExtras();
        int score = bundle.getInt("score");

        TextView txtScore1 = (TextView) findViewById(R.id.txtScore1);
        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);
    }
}

Was This Post Helpful? 0
  • +
  • -

#11 ChrisNt   User is offline

  • cute bug

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

Re: Send variable data to other activity android

Posted 21 September 2015 - 12:57 PM

Quote

It all works fine but when clicking on the scores button on the main activity an error occur.

Where is the score button?
You should start the intent and pass the extras where the score button is.

This post has been edited by ChrisNt: 21 September 2015 - 01:01 PM

Was This Post Helpful? 0
  • +
  • -

#12 parkie02   User is offline

  • New D.I.C Head

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

Re: Send variable data to other activity android

Posted 21 September 2015 - 12:59 PM

View PostChrisNt, on 21 September 2015 - 12:57 PM, said:

Quote

It all works fine but when clicking on the scores button on the main activity an error occur.

Where is the score button?


The score button is on the MainActivity
Was This Post Helpful? 0
  • +
  • -

#13 ChrisNt   User is offline

  • cute bug

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

Re: Send variable data to other activity android

Posted 21 September 2015 - 01:11 PM

If i have understand correctly what you are trying to do ..i think you should use SharedPreferences.With sharedPreferences you can access a value from different activities..is that what are you trying to achieve?
Was This Post Helpful? 0
  • +
  • -

#14 parkie02   User is offline

  • New D.I.C Head

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

Re: Send variable data to other activity android

Posted 21 September 2015 - 02:58 PM

What must happen is when the quiz is completed the result activity appear. This is working correctly From the result activity the score variable must be sent to the Scores activity. When there is clicked on the scores button on the Main Menu the Scores activity must open showing the score for the last quiz. Can you maybe show me how to use the sharedPreferences on my code
Was This Post Helpful? 0
  • +
  • -

#15 ChrisNt   User is offline

  • cute bug

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

Re: Send variable data to other activity android

Posted 22 September 2015 - 08:24 AM

I did think a bit about it .. the solution i would suggest is to create a class which extends the Application class, that class will hold the score of your game.When a class extends Application its accesible from all the activities.

public class Score extends Application {

    private int score;

    public int getScore() {

        return score;
    }

    public void setScore(int score) {

        this.score = score;
    }
}



!!!
In your manifest file add this tag under the existing application tag to register the Score class
android:name=".Score" 

( You may need to use the namespace + class name if the above does not work ex. com.app.myapp.Score )



Now you can access the score field and change it from any activity by calling:
Score score = (Score) getApplicationContext();
int score = score.getScore();

// to set the score u just have to call the setter 
score.setScore( 500);

This post has been edited by ChrisNt: 22 September 2015 - 08:37 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1