user3765439
user3765439

Reputation:

NullPointerException while switching to an activity

I've got one problem: I get a NullPointerException if I try to switch into a class with this code:

aw1.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {


                if(Saver.getStage()==Saver.getGroup()/*&&Saver.getPoints()!=0*/)
                {   Saver.setPoints(Saver.getPoints()+20);
                    Intent myIntent = new Intent(view.getContext(), Finish.class);
                    startActivityForResult(myIntent, 0);}
                if(Stages.check(Saver.getStage())==1)
                {Saver.setPoints(Saver.getPoints()+20);}

                if(Saver.getStage()<8)          
                {Saver.setStage(Saver.getStage()+1);}
                else    
                {Saver.setStage(0);}



                Intent intent = new Intent();
                setResult(RESULT_OK, intent);
                finish();


            }

        });

I want to switch to Finish.class

The code of Finish.java:

package com.example.romrallye;

import com.example.romrallye.MainActivity;
import com.example.romrallye.R;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
import android.content.Intent;

public class Finish extends MainActivity {

private TextView punkte;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.finish_main);
    punkte = (TextView) findViewById(R.id.txtpunkte);
    punkte.setText(Saver.getPoints());



     Button next = (Button) findViewById(R.id.bt_back4);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent intent = new Intent();
                setResult(RESULT_OK, intent);
                finish();
            }

        });}



@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.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();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

And here the error from the LogCat:

enter image description here

Upvotes: 0

Views: 44

Answers (1)

Simon Marquis
Simon Marquis

Reputation: 7516

You can't use setText(int) if the value is not a resource.
Use

punkte.setText(String.valueOf(Saver.getPoints()));

instead of

punkte.setText(Saver.getPoints());

Upvotes: 3

Related Questions