Reputation: 113
I'm new at coding.
So I have a switch where the app can choose the next actvity using the string nextActivityExtra. The intent.putExtra are necessary for the other activities and no activity have problem recieving them. So basically the code works, but only on the first case. When the app comes back at this "switch activity", it should get to the second case, and here it cannot load MainActivity (it loads a black screen and suddenly goes back to the "switch activity").
If I switch ExplicaioActivity for MainActivity or for any other activty it will work fine unless it's in the second case. I really cannot grasp what's happening here, I also tried the same code with If's with similar results.
public class EntreActivities extends AppCompatActivity {
ImageView iconView;
int count;
String levelExtra;
Intent intent;
String nextActivityExtra;
String rutaLevelNumber;
boolean isTaskCompleted;
Class myClass;
public void Rotate(View view) {
RotateAnimation rotateAnimation = new RotateAnimation(0, 30,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 1f);
rotateAnimation.setDuration(3000);
rotateAnimation.setRepeatMode(Animation.REVERSE);
rotateAnimation.setRepeatCount(Animation.INFINITE);
rotateAnimation.setFillAfter(true);
rotateAnimation.setStartOffset(1000);
iconView.setAnimation(rotateAnimation);
}
public void startAct() {
if (getIntent().getBooleanExtra("inTraining", false)) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
Toast.makeText(this, "TTTTtrain", Toast.LENGTH_SHORT).show();
} else {
switch (nextActivityExtra) {
case "ExplicaioActivity":
myClass = ExplicaioActivity.class;
Toast.makeText(EntreActivities.this, "here", Toast.LENGTH_SHORT).show();
intent = new Intent(getApplicationContext(), ExplicaioActivity.class);
intent.putExtra("Level", rutaLevelNumber);
intent.putExtra("inTraining", false);
startActivity(intent);
overridePendingTransition(R.anim.slide_in_up, R.anim.slide_out_up);
break;
case "MainActivity":
myClass = MainActivity.class;
Toast.makeText(EntreActivities.this, "here", Toast.LENGTH_SHORT).show();
intent.putExtra("Level", rutaLevelNumber);
intent.putExtra("inTraining", false);
intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_in_up, R.anim.slide_out_up);
break;
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_entre_activities);
iconView = findViewById(R.id.iconView);
iconView.setRotation(-15);
Intent intent = getIntent();
levelExtra = intent.getStringExtra("Level");
nextActivityExtra = intent.getStringExtra("NextActivity");
rutaLevelNumber = intent.getStringExtra("Level");
Rotate(iconView);
startAct();
}
}
This is the error log that I could take from AS:
2019-01-12 01:08:12.383 4282-4282/com.example.root.exercicis E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.root.exercicis, PID: 4282
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.root.exercicis/com.example.root.exercicis.EntreActivities}: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference
at com.example.root.exercicis.EntreActivities.startAct(EntreActivities.java:53)
at com.example.root.exercicis.EntreActivities.onCreate(EntreActivities.java:100)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Upvotes: 1
Views: 61
Reputation: 3234
The reason for this is because you're creating a new Intent that doesn't have the String extra "NextActivity".
Let me walk you through what's happening:
Intent intent = getIntent()
is run and intent now contains all data from the Intent that was sent to this Activity.nextActivityExtra = intent.getStringExtra("NextActivity")
is run and now, nextActivityExtra is now stored with either "ExplicaioActivity" or "MainActivity".startAct()
is run.intent = new Intent(getApplicationContext(), ExplicaioActivity.class);
is run, and it creates a NEW intent.intent.putExtra("Level")
and intent.putExtra("inTraining")
, but forget to put `intent.putExtra("NextActivity");onCreate()
is run.Intent intent = getIntent()
is run and intent now contains all data from the Intent that was sent to this Activity. But keep in mind that this Intent from the previous Activity is missing the Extra "NextActivity"nextActivityExtra = intent.getStringExtra("NextActivity")
is run, but since there's no extra for this, so null is stored in this variable instead.So to fix this, you simply have to add intent.putExtra("NextActivity", NEW_VALUE);
inside case "ExplicaioActivity" with NEW_VALUE being the new value you want to add as the extra value. `
However, for case "MainActivity", besides doing what I said before, you also need to rearrange a line of code.
Switch this:
intent.putExtra("Level", rutaLevelNumber);
intent.putExtra("inTraining", false);
intent = new Intent(getApplicationContext(), MainActivity.class);
to this:
intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("Level", rutaLevelNumber);
intent.putExtra("inTraining", false);
Upvotes: 1
Reputation: 995
you are trying to put extra before initialising the Intent
inside MainActivity
case.
Initialize Intent before putting extra like following
intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("Level", rutaLevelNumber);
intent.putExtra("inTraining", false);
Upvotes: 1