我遇到了一个在切换活动时关闭我的应用的问题。我并不是说这是一个“修复我的代码”的问题,但是如果它看起来那样,让我知道我能做些什么来解决这个问题。我将解释该程序的功能,以及它应该做什么,希望StackExchange的优秀人员可以帮助我找到解决方法。
目的:该代码应该是我设计的一个简单的游戏,这样我就可以熟悉android studio(我已经知道一些Java,并且想要使用它)。游戏将用户置于启动画面上(我不认为代码是相关的,但如果你希望我包含它,我会),然后一旦用户点击,他们就会被带到“GameActivity”(代码)下面)。我的代码播放“准备好,设置,开始”,然后游戏开始。屏幕以随机间隔在绿色,黄色和红色之间变化。如果玩家在屏幕为绿色时点击,则获得1分。如果屏幕是黄色,他们赚取双倍。点击红色屏幕将失去游戏。随着时间的推移,颜色会以更短且更不可预测的间隔切换。
实际发生的事情:只要我切换活动,就会收到一条错误消息。当我只有“介绍”代码时,我能够切换活动和应用程序正常运行,但在添加其余代码之后,问题就出现了。在Android Studio中,我没有任何警告,任何类型的错误或建议(在android工作室帮助您修复代码的右侧)。我不认为它是清单或XML的问题,因为它们在早期阶段工作正常。
package com.nateolson.taptap;
import android.graphics.Color;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import java.util.concurrent.ThreadLocalRandom;
public class GameActivity extends AppCompatActivity {
final Handler introHandler = new Handler();
boolean gameActive = false;
final View layout = findViewById(R.id.layout);
//for main method only
int playerPoints = 0;
long timeMinMillis = 1000;
long timeMaxMillis = 3000;
long randTime;
long switchReferenceTime = System.currentTimeMillis();
String colorStage = "green";
//for main method only
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
final TextView readyMessageView = (TextView) findViewById(R.id.readyTV);
final TextView getSetMessageView = (TextView) findViewById(R.id.setTV);
getSetMessageView.setVisibility(View.INVISIBLE);
final TextView goMessageView = (TextView) findViewById(R.id.goTV);
goMessageView.setVisibility(View.INVISIBLE);
layout.setBackgroundColor(Color.RED);
Runnable switchToYellow = new Runnable() {
@Override
public void run() {
readyMessageView.setVisibility(View.GONE);
layout.setBackgroundColor(Color.YELLOW);
getSetMessageView.setVisibility(View.VISIBLE);
}
};
Runnable switchToGreen = new Runnable() {
@Override
public void run() {
getSetMessageView.setVisibility(View.GONE);
layout.setBackgroundColor(Color.GREEN);
goMessageView.setVisibility(View.VISIBLE);
}
};
Runnable gameRunnable = new Runnable() {
public void run() {
gameActive = true;
}
};
introHandler.postDelayed(switchToYellow, 1500);
introHandler.postDelayed(switchToGreen, 3000);
introHandler.postDelayed(gameRunnable, 4500);
main();
}
public void main() {
while(gameActive) {
randTime = ThreadLocalRandom.current().nextLong(timeMinMillis, timeMaxMillis);
if (System.currentTimeMillis() - switchReferenceTime >= randTime) {
switch (colorStage) {
case "green":
colorStage = "yellow";
break;
case "yellow":
colorStage = "red";
break;
case "red":
colorStage = "green";
break;
}
switchReferenceTime = System.currentTimeMillis();
}
switch (colorStage) {
case "green": layout.setBackgroundColor(Color.GREEN);
case "yellow": layout.setBackgroundColor(Color.YELLOW);
case "red": layout.setBackgroundColor(Color.RED);
}
timeMinMillis -= .03 * timeMinMillis;
timeMaxMillis -= .02 * timeMaxMillis;
layout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (colorStage) {
case "green": playerPoints++;
case "yellow": playerPoints += 2;
case "red":
playerPoints = 0;
gameActive = false;
}
return false;
}
});
}
上一个活动的Java(启动画面):
package com.nateolson.taptap;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onSplashPageClick(View view) {
Intent intent = new Intent(this, GameActivity.class);
startActivity(intent);
finish();
}
}
GameActivity XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layout"
tools:context="com.nateolson.taptap.GameActivity">
<TextView
android:id="@+id/readyTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="monospace"
android:text="Ready?"
android:textAlignment="center"
android:textAllCaps="false"
android:textAppearance="@style/TextAppearance.AppCompat.Display2"
android:textColor="@android:color/background_light"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/setTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="monospace"
android:text="Set... "
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Display2"
android:textColor="@android:color/background_light"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/goTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="monospace"
android:text="GO!"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Display2"
android:textColor="@android:color/background_light"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
MainActivity的XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.nateolson.taptap.MainActivity"
android:gravity="center"
android:background="#679933"
android:orientation="vertical"
android:onClick="onSplashPageClick"
>
<ImageView
android:layout_height="212dp"
android:layout_width="288dp"
android:src="@mipmap/splash_screen_logo"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="31dp" />
<TextView
android:fontFamily="sans-serif-thin"
android:textSize="50dp"
android:textColor="#274D00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="T a p T a p"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_constraintTop_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintHorizontal_bias="0.426"
app:layout_constraintVertical_bias="0.537" />
</android.support.constraint.ConstraintLayout>
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nateolson.taptap">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".GameActivity"
android:parentActivityName=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.GAME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
我不是要求分发,我不只是希望有人为我修改我的代码。我想找出一个解决方案,然后了解我做错了什么。我也试过类似帖子中提到的修复,但无济于事。这与“遗憾的MyApp已停止”问题不同,因为我提供了自己的代码,该代码尚未被该线程中建议的任何解决方案修复。对于任何特定情况,这个问题似乎都非常通用且不具体。
谢谢,
Nate Olson
答案 0 :(得分:3)
final View layout = findViewById(R.id.layout);
你不应该在函数之外调用findViewById,并且必须在调用setContentView()之后调用它。
其次,你不应该在UI线程上做一段时间(true)。这将阻止整个屏幕,这意味着您无法点击任何内容,并会在几秒钟后导致您的应用程序崩溃。 (如果您想了解更多信息,请搜索“应用程序无响应”) 您应该在后台线程上运行while(true)。 (虽然这也会导致UI出现一些问题,但我会让你自己解决这个问题)