Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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:
Java
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
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
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
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);
    }
    } 
Posted

I already explained to you at Pass variabble from one activity to another and show on button click[^], that instead of reposting this question, you should update your original with any new information you have.
 
Share this answer
 
I dont have any new information. Added the logcat here. Cant you maybe help me on this with my code. Thank you

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?

Java
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?
 
Share this answer
 
v2
Comments
Richard Deeming 21-Sep-15 13:07pm    
This is not an answer. If you have a question or a comment, use the "Have a Question or Comment?" button underneath the relevant answer. DO NOT post it as a new solution, and DO NOT repost the same question over and over again!
Richard Deeming 21-Sep-15 14:16pm    
You've now marked your comment as the answer, despite the fact that you're still asking for help.

This is now a clear case of abuse.
Martin Park 22-Sep-15 3:45am    
I just want to know what is the possible answer to this because I do not know

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900