我是编码新手。
所以我有一个开关,应用程序可以使用字符串nextActivityExtra选择下一个活动。 intent.putExtra对于其他活动是必需的,并且没有活动在接收它们方面有问题。因此,基本上该代码有效,但仅在第一种情况下有效。当应用程序以这种“切换活动”返回时,它应该进入第二种情况,此时它无法加载MainActivity(它会加载黑屏并突然返回到“切换活动”)。
如果我将ExplicaioActivity切换为MainActivity或其他任何活动,则除非第二种情况,否则它将正常工作。我真的不明白这里发生了什么,我也用If尝试了相同的代码,结果相似。
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();
}
}
这是我可以从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)
答案 0 :(得分:1)
您正在尝试在Intent
情况下初始化MainActivity
之前添加其他内容。
初始化Intent,然后再添加类似内容
intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("Level", rutaLevelNumber);
intent.putExtra("inTraining", false);
答案 1 :(得分:1)
之所以这样做,是因为您正在创建一个没有字符串额外的“ NextActivity”的新Intent。
让我向您介绍正在发生的事情:
Intent intent = getIntent()
已运行,并且意图现在包含了从意图发送到此活动的所有数据。nextActivityExtra = intent.getStringExtra("NextActivity")
已运行,现在, nextActivityExtra 现在存储在“ ExplicaioActivity” 或“ MainActivity” 中。 li>
startAct()
已运行。intent = new Intent(getApplicationContext(), ExplicaioActivity.class);
运行,并创建 NEW 意图。intent.putExtra("Level")
和intent.putExtra("inTraining")
,但忘记放置`intent.putExtra(“ NextActivity”); onCreate()
已运行。Intent intent = getIntent()
已运行,并且意图现在包含了从意图发送到该活动的所有数据。 但是请记住,上一个活动的此Intent缺少额外的“ NextActivity” nextActivityExtra = intent.getStringExtra("NextActivity")
已运行,但是由于没有多余的内容,因此 null 存储在此变量中。因此,要解决此问题,只需在案例“ ExplicaioActivity” 内添加intent.putExtra("NextActivity", NEW_VALUE);
,并以NEW_VALUE作为您要添加的新值作为额外值。 `
但是,对于案例“ MainActivity” ,除了按照我之前所说的那样,您还需要重新排列一行代码。
切换此:
intent.putExtra("Level", rutaLevelNumber);
intent.putExtra("inTraining", false);
intent = new Intent(getApplicationContext(), MainActivity.class);
对此:
intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("Level", rutaLevelNumber);
intent.putExtra("inTraining", false);