Search code examples
androidandroid-intentscoring

Not able to transfer Textview carrying game score from one activity(stage) to the next activity(stage)


so I'm a beginner and i'm trying to make a quiz game without database and multiple activities. I want to get the scoring system as a textview in all the activities and it should change according to right or wrong answer. I've been trying to figure it out for over a week now and I only think I wasted that time. Here is a sample code.

This is my MainActivity.java

  public class MainActivity extends AppCompatActivity {

 int score = 0;
  TextView scored;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    scored = (TextView) findViewById(R.id.score);
    scored.setText(String.valueOf(score));

}
public void rightanswer(View view){

    score = score + 5;
   scored.setText(String.valueOf(score));
    Intent intent = new Intent(this, Page1.class);
    intent.putExtra("score",String.valueOf(score));
    startActivity(intent);
    }
}

This is my activity_main

     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
>
<LinearLayout
    android:layout_width="50dp"
    android:layout_height="80dp"
    android:layout_weight="1"
    android:background="#05083e"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Score"
        android:id="@+id/tvLabelScore"
        android:textSize="18sp"
        android:textStyle="bold"
        android:textColor="#f6f6f6"
        android:layout_marginTop="5dp"
        android:layout_gravity="center_horizontal" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:id="@+id/score"
        android:textSize="18sp"
        android:textStyle="bold"
        android:textColor="#e5e5e5"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button2"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"

    android:onClick="rightanswer"
    />



</RelativeLayout>

and then my Page1.java

    public class Page1 extends AppCompatActivity {




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_page6);

    TextView textresult = (TextView) findViewById(R.id.textResult);

    Intent mintent = getIntent();
    int score = mintent.getIntExtra("score",0);
    textresult.setText(String.valueOf(score)); // **I get null point exception error on this line.**

}

 }

and lastly my activity_page1

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
>
<LinearLayout
    android:layout_width="50dp"
    android:layout_height="80dp"
    android:layout_weight="1"
    android:background="#05083e"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Score"
        android:id="@+id/tvLabelScore"
        android:textSize="18sp"
        android:textStyle="bold"
        android:textColor="#f6f6f6"
        android:layout_marginTop="5dp"
        android:layout_gravity="center_horizontal" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:id="@+id/textResult"
        android:textSize="18sp"
        android:textStyle="bold"
        android:textColor="#e5e5e5"
        android:layout_gravity="center_horizontal" />
</LinearLayout>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button2"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"

    android:onClick="rightanswer"
    />


 </RelativeLayout>

Can you guys tell me what i'm doing wrong?...I typed all this code from different answers on the internet...and its all wrong I think....please help out if possible.

edit: The error that i'm getting

    FATAL EXCEPTION: main
   Process: com.sample.testost, PID: 14544
                                                                           java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sample.testost/com.appsworthyourtime.testost.Page6}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                               at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                               at android.os.Looper.loop(Looper.java:148)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                            Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                                               at com.sample.testost.Page1.onCreate(Page1.java:20)
                                                                               at android.app.Activity.performCreate(Activity.java:6237)
                                                                               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                               at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                               at android.os.Looper.loop(Looper.java:148) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

Solution

  • In your MainActivity you set a String value, where in the Page1 class you want to receive an Integer.

    So

    • either change the code in your MainActivity to intent.putExtra("score", score);
    • or in your Page1 to String score = mintent.getStringExtra("score");.

    Additionally, you seem to set the wrong layout file in the Page1 Activity. Maybe you meant setContentView(R.layout.activity_page1);, instead of _page6?