我正在研究项目并测试XML,但是,每当我尝试切换到下一个活动时,应用程序就会卡在黑屏上并且什么都不做,我不得不退出。
我尝试切换setContentView的布局并且不起作用,我检查确保活动文件中的代码正常工作,现在我正在查看其他问题,我只是感到困惑。
这是我的MainActivity:
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" />
答案 0 :(得分:0)
当调用onCreate()时,你的代码就会陷入这个循环
while (field.equals("")) {
dialogueView.setText("What is your name?");
name = field;
}