Hello, I am a novice programmer. And I decided to make a project in which examples will be created on the choice of complexity. I created a class and created a key and constants in it, and in activity, I switch to another activity, depending on the pressed button, create an example. But for some reason, when choosing the complexity of the application crashes. Logs:

FATAL EXCEPTION: main E AndroidRuntime Process: com.konter.random, PID: 14168 E AndroidRuntime java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.konter.random/com.konter.random.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Intent.getIntExtra(java.lang.String, int)' on a null object reference E AndroidRuntime at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269) E AndroidRuntime at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420) E AndroidRuntime at android.app.ActivityThread.access$900(ActivityThread.java:154) E AndroidRuntime at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) E AndroidRuntime at android.os.Handler.dispatchMessage(Handler.java:102) E AndroidRuntime at android.os.Looper.loop(Looper.java:135) E AndroidRuntime at android.app.ActivityThread.main(ActivityThread.java:5294) E AndroidRuntime at java.lang.reflect.Method.invoke(Native Method) E AndroidRuntime at java.lang.reflect.Method.invoke(Method.java:372) E AndroidRuntime at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) E AndroidRuntime at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699) E AndroidRuntime at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:102) E AndroidRuntime Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Intent.getIntExtra(java.lang.String, int)' on a null object reference E AndroidRuntime at com.konter.random.MainActivity.<init>(MainActivity.java:120) E AndroidRuntime at java.lang.reflect.Constructor.newInstance(Native Method) E AndroidRuntime at java.lang.Class.newInstance(Class.java:1606) E AndroidRuntime at android.app.Instrumentation.newActivity(Instrumentation.java:1066) E AndroidRuntime at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2259) E AndroidRuntime ... 11 more 

HomeActivity (first activation):

 public class HomeActivity extends Activity { Button play,easyBuutton,mediumButton,hardButton; LinearLayout lin; LinearLayout lin2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.home); play=(Button)findViewById(R.id.homeButton); easyBuutton=(Button)findViewById(R.id.easyButton); mediumButton=(Button)findViewById(R.id.mediumButton); hardButton=(Button)findViewById(R.id.hardButton); lin=(LinearLayout)findViewById(R.id. outAnim); lin2=(LinearLayout)findViewById(R.id.inAnim); final Animation fallingAnimation = AnimationUtils.loadAnimation(this,R.anim.one); final Animation fallingAnimation1 = AnimationUtils.loadAnimation(this,R.anim.two); fallingAnimation.setAnimationListener(new Animation.AnimationListener(){ @Override public void onAnimationStart(Animation p1) { // TODO: Implement this method } @Override public void onAnimationEnd(Animation p1) { lin.setVisibility(View.GONE); lin2.setVisibility(View.VISIBLE); lin2.startAnimation(fallingAnimation1); } @Override public void onAnimationRepeat(Animation p1) { // TODO: Implement this method } }); play.setOnClickListener(new OnClickListener(){ @Override public void onClick(View p1) { lin.startAnimation(fallingAnimation); } }); OnClickListener complication= new OnClickListener(){ @Override public void onClick(View v) { switch(v.getId()){ case R.id.easyButton: perehod(Level.EASY_START,Level.EASY_END); break; case R.id.mediumButton: perehod(Level.MEDIUM_START,Level.MEDIUM_END); break; case R.id.hardButton: perehod(Level.HARD_START,Level.HARD_END); break; } } }; easyBuutton.setOnClickListener(complication); mediumButton.setOnClickListener(complication); hardButton.setOnClickListener(complication); } void perehod(int start,int end){ Intent intent = new Intent(HomeActivity.this, MainActivity.class); intent.putExtra(Level.LEVEL_START,start); intent.putExtra(Level.LEVEL_END,end); startActivity(intent) } } 

MainActivity (second activation in which the random number is selected, subtraction and comparison):

 public class MainActivity extends Activity { //int a=0,b=100 int c,z; int s=16000; final int start = getIntent().getIntExtra(Level.LEVEL_START,0); final int end = getIntent().getIntExtra(Level.LEVEL_END, 1); TextView mainTextView, mainTextView1,time; Button one,two,three,four,five,six,seven,eight,nine,zero,delit,minus,inspection; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); regist(); soz_primer(); new CountDownTimer(16000, 1000) { public void onTick(long millisUntilFinished) { //тут пиши то, что будет выполняться каждую секунду time.setText(""+millisUntilFinished/1000); } public void onFinish() { //тут пиши то, что должно произойти когда таймер закончит работу time.setText("0"); } }.start(); final OnClickListener button=new OnClickListener(){ @Override public void onClick(View v) { String text=((Button)v).getText().toString(); mainTextView1.append(""+text); } }; one.setOnClickListener(button); two.setOnClickListener(button); three.setOnClickListener(button); four.setOnClickListener(button); five.setOnClickListener(button); six.setOnClickListener(button); seven.setOnClickListener(button); eight.setOnClickListener(button); nine.setOnClickListener(button); zero.setOnClickListener(button); minus.setOnClickListener(button); delit.setOnClickListener(new OnClickListener(){ @Override public void onClick(View p1) { mainTextView1.setText(""); } }); inspection.setOnClickListener(new OnClickListener(){ @Override public void onClick(View p1) { z=Integer.parseInt(mainTextView1.getText().toString()); if(c==z){ mainTextView1.setText(""); soz_primer(); int v=Integer.parseInt(time.getText().toString()); } else finish(); } }); } void regist(){ one=(Button)findViewById(R.id.one); two=(Button)findViewById(R.id.two); three=(Button)findViewById(R.id.three); four=(Button)findViewById(R.id.four); five=(Button)findViewById(R.id.five); six=(Button)findViewById(R.id.six); seven=(Button)findViewById(R.id.seven); eight=(Button)findViewById(R.id.eight); nine=(Button)findViewById(R.id.nine); zero=(Button)findViewById(R.id.zero); delit=(Button)findViewById(R.id.delit); minus=(Button)findViewById(R.id.minus); time=(TextView)findViewById(R.id.time); mainTextView=(TextView)findViewById(R.id.mainTextView); mainTextView1=(TextView)findViewById(R.id.mainTextView1); inspection=(Button)findViewById(R.id.inspection); } void soz_primer(){ //int random_number = start + (int) (Math.random() * end); //int random_number1 = start+ (int) (Math.random() * end); int random_number=new Random().nextInt(start)+end; int random_number1=new Random().nextInt(start)+end; mainTextView.setText(""+random_number); int i=(int)(Math.random()*2); if(i==0){ c=random_number+random_number1; mainTextView.append("+"); }else mainTextView.append("-"); mainTextView.append(""+random_number1+"="); c=random_number-random_number1; } } 

Class with keys and constants:

 public class Level { public static String LEVEL_START="level_start"; public static String LEVEL_END="level_end"; public static int EASY_START=0; public static int EASY_END=100; public static int MEDIUM_START=100; public static int MEDIUM_END=1000; public static int HARD_START=900; public static int HARD_END=10000; } 
  • In the manifesto, the second activity is registered? - Kirill Malyshev

2 answers 2

 void perehod(int start,int end){ Intent intent = new Intent(HomeActivity.this, MainActivity.class); startActivity(intent); intent.putExtra(Level.LEVEL_START,start); intent.putExtra(Level.LEVEL_END,end); } 

Does it bother you that you first activate activations with empty extras, and only after that do you try to stuff something into them?

It will be right

 void perehod(int start,int end){ Intent intent = new Intent(this, MainActivity.class); intent.putExtra(Level.LEVEL_START,start); intent.putExtra(Level.LEVEL_END,end); startActivity(intent); } 

And these things in onCreate move

 start = getIntent().getIntExtra(Level.LEVEL_START,0); end = getIntent().getIntExtra(Level.LEVEL_END, 1); 

The point is that class variables are initialized at the moment of class instantiation, at the moment when getIntent () cannot return anything else but null.

  • It still crashes - Andrei Mashchenko
  • Damn, get ahead! ;) - Flippy
  • HomeActivity.this - это ересь какая то. - why heresy? - post_zeew
  • @post_zeew Well, of course he got excited with heresy, but why is it in the class method? - rjhdby
  • @rjhdby; In this case, it is not necessary, but I would not call it heresy. - post_zeew

Take out the initialization of variables in onCreate .

 int start, end... onCreate() { ... Intent i = getIntent(); start = i.getIntExtra(Level.LEVEL_START, 0); end = i.getIntExtra(Level.LEVEL_END, 0); ... } 

@rjhdby also correctly noted in his answer, but the second moment