Java 如何防止切换活动时出现黑屏?

Java 如何防止切换活动时出现黑屏?,java,android,Java,Android,我正在做一个项目并测试XML,但是,每当我尝试切换到下一个活动时,应用程序就会被困在黑屏上,什么也不做,我被迫退出 我尝试切换setContentView的布局,但没有成功,我检查了活动文件中的代码是否正常工作,现在我正在查看其他人的问题,我只是感到困惑 以下是我的主要活动: public class MainActivity extends android.app.Activity { ImageButton startButton; TextView textView; @Override

我正在做一个项目并测试XML,但是,每当我尝试切换到下一个活动时,应用程序就会被困在黑屏上,什么也不做,我被迫退出

我尝试切换setContentView的布局,但没有成功,我检查了活动文件中的代码是否正常工作,现在我正在查看其他人的问题,我只是感到困惑

以下是我的主要活动:

public class MainActivity extends android.app.Activity {

ImageButton startButton;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.content_main);

    startButton = (ImageButton)findViewById(R.id.startButton);
    textView = (TextView)findViewById(R.id.textView);
    textView.setTextColor(Color.WHITE);
    View backgroundView = findViewById(R.id.spoopySkellington);
    View root = backgroundView.getRootView();
    root.setBackgroundColor(getResources().getColor(android.R.color.black));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

public void startClick(View view)
{
    final Intent intent = new Intent(MainActivity.this, AdventureActivity.class);
    startActivity(intent);
}

@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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}


}
以下是我尝试切换到的活动

public class AdventureActivity extends android.app.Activity {

Character mainCharacter;
Character skeleton;
TextView dialogueView;
EditText textField;
Button confirmButton;
String field;
ImageView enemy;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.content_adventure);

    confirmButton = (Button)findViewById(R.id.confirmButton);
    dialogueView = (TextView)findViewById(R.id.dialogueView);
    textField = (EditText)findViewById(R.id.inputText);
    field = "";

    skeleton = new Character("Skeleton", 10, 1);
    String name = "";
    while (field.equals("")) {
        dialogueView.setText("What is your name?");
        name = field;
    }

    mainCharacter = new Character(name);

}

public void onClick(View view)
{
    field = textField.getText().toString();
    textField.setText("");
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}


}
最后,这里是我要切换到的活动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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.jason.textadventure.AdventureActivity"
tools:showIn="@layout/activity_adventure">

<TextView
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:id="@+id/dialogueView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="..."
    android:textSize="30dp"
    tools:layout_editor_absoluteY="126dp"
    tools:layout_editor_absoluteX="180dp"
    />

<EditText
    app:layout_constraintBottom_toBottomOf="@id/dialogueView"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:id="@+id/inputText"
    android:layout_width="261dp"
    android:layout_height="40dp"
    tools:layout_editor_absoluteY="196dp"
    tools:layout_editor_absoluteX="62dp"
    android:inputType="textNoSuggestions"
    />

<Button
    app:layout_constraintBottom_toBottomOf="@id/inputText"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="@id/dialogueView"
    android:id="@+id/confirmButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="CONFIRM"
    android:onClick="onClick"
    tools:layout_editor_absoluteY="184dp"
    tools:layout_editor_absoluteX="148dp" />

当调用onCreate()时,您的代码被困在这个循环中

while (field.equals("")) {
        dialogueView.setText("What is your name?");
        name = field;
    }

AdventureActivity
onCreate()
中去掉无限
while
循环。使用
字段
/
名称
执行任何需要的操作,即在
onClick()中实例化您的
字符