如何在android应用程序中加载初始视图?
我一直在开发这个应用程序,这是我上学期参加的一门课作业的一部分,但我不能完全让它工作。根据反馈,我的主要问题是“没有初始视图加载来启动游戏”,所以应用程序崩溃了。 这是我的活动课:如何在android应用程序中加载初始视图?,android,Android,我一直在开发这个应用程序,这是我上学期参加的一门课作业的一部分,但我不能完全让它工作。根据反馈,我的主要问题是“没有初始视图加载来启动游戏”,所以应用程序崩溃了。 这是我的活动课: public class GuessMasterActivity extends AppCompatActivity { private TextView entityName; private TextView ticketsum; private Button guessButton;
public class GuessMasterActivity extends AppCompatActivity {
private TextView entityName;
private TextView ticketsum;
private Button guessButton;
private EditText userIn;
private Button btnclearContent;
private String user_input;
private ImageView entityImage;
String answer;
private int numOfEntities;
private Entity[] entities;
private int numOfTickets;
private int[] tickets;
String entName;
int entityid =0;
int currentTicketWon = 0;
Politician trudeau = new Politician("Justin Trudeau", new Date("December", 25, 1971), "Male", "Liberal", 0.25);
Singer dion = new Singer("Celine Dion", new Date("March", 30, 1968), "Female", "La voix du bon Dieu", new Date("November", 6, 1981), 0.5);
Person myCreator = new Person("myCreator", new Date("September", 1, 2000), "Female", 1);
Country usa = new Country("United States", new Date("July", 4, 1776), "Washington D.C.", 0.1);
public GuessMasterActivity() {
numOfEntities = 0; //Initialize counter variables to zero
numOfTickets = 0;
entities = new Entity[100]; //Max number of entities possible in the game is 100
}
public void addEntity(Entity entity) { //Adds selected entity to the game
entities[numOfEntities++] = entity.clone(); //Invokes clone function to create a copy of selected entity and stores copy in entities array
}
public void playGame() { //Runs the game with selected entity
userIn.getText().clear();
int entityId = genRandomEntityId();
Entity entity = entities[entityId];
entityName.setText(entity.getName());
String answer = userIn.getText().toString(); //Reads user input
answer = answer.replace("\n", "").replace("\r", "");
if (answer.equals("quit")) { //Ends game if user inputs quit
System.exit(0);
}
Date date = new Date(answer);
if (date.precedes(entity.getBorn())) { //Evaluates user input
AlertDialog.Builder Incorrect = new AlertDialog.Builder(GuessMasterActivity.this);
Incorrect.setTitle("Incorrect");
Incorrect.setMessage("Try a later date");
Incorrect.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getBaseContext(),"Loading...",Toast.LENGTH_SHORT).show();
}
});
AlertDialog dialog = Incorrect.create();
dialog.show();
} else if (entity.getBorn().precedes(date)) {
AlertDialog.Builder Incorrect2 = new AlertDialog.Builder(GuessMasterActivity.this);
Incorrect2.setTitle("Incorrect");
Incorrect2.setMessage("Try an earlier date");
Incorrect2.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getBaseContext(),"Loading...",Toast.LENGTH_SHORT).show();
}
});
AlertDialog dialog = Incorrect2.create();
dialog.show();
} else {
currentTicketWon = entity.getAwardedTicketNumber();
this.numOfTickets = this.numOfTickets + entity.getAwardedTicketNumber(); //Updates total number of tickets for this game based on difficulty of selected entity
AlertDialog.Builder win = new AlertDialog.Builder(GuessMasterActivity.this);
win.setTitle("You Won");
win.setMessage("Bingo"+entity.closingMessage());
win.setNegativeButton("Continue", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getBaseContext(),"You won"+currentTicketWon+"tickets",Toast.LENGTH_SHORT).show();
}
});
AlertDialog dialog = win.create();
dialog.show();
ticketsum.setText(numOfTickets);
ContinueGame();
}
}
public void ImageSetter(){
ImageView imgView = findViewById(R.id.entityImage);
switch(entName){
case("Justin Trudeau"):
imgView.setImageResource(R.drawable.justint);
break;
case("Celine Dion"):
imgView.setImageResource(R.drawable.celidion);
break;
case("United States"):
imgView.setImageResource(R.drawable.usaflag);
break;
case("myCreator"):
imgView.setImageResource(R.drawable.pic);
break;
}
}
public void ContinueGame(){
entityid = genRandomEntityId();
Entity entity = entities[entityid];
entName = entity.getName();
ImageSetter();
entityName.setText(entName);
}
public int genRandomEntityId() { //Selects random integer in the range of 0-the number of entities in the game
Random randomNumber = new Random();
return randomNumber.nextInt(numOfEntities);
}
public void changeEntity(){
userIn.setText("");
playGame();
}
void welcomeToGame(Entity entity){
AlertDialog.Builder welcomealert = new AlertDialog.Builder(GuessMasterActivity.this);
welcomealert.setTitle("GuessMaster Game v3");
welcomealert.setMessage(entity.welcomeMessage());
welcomealert.setCancelable(false);
welcomealert.setNegativeButton("START_GAME", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getBaseContext(),"Game is Starting. . . Enjoy", Toast.LENGTH_SHORT).show();
}
});
AlertDialog dialog = welcomealert.create();
dialog.show();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addEntity(usa);
addEntity(dion);
addEntity(trudeau);
addEntity(myCreator);
setContentView(R.layout.activity_guess_master);
guessButton = (Button) findViewById(R.id.btnGuess);
userIn = (EditText)findViewById(R.id.guessInput);
ticketsum = (TextView)findViewById(R.id.ticket);
entityImage = (ImageView)findViewById(R.id.entityImage);
entityName = (TextView)findViewById(R.id.entityName);
btnclearContent = (Button) findViewById(R.id.btnClear);
btnclearContent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
changeEntity();
}
});
guessButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
playGame();
}
});
final GuessMasterActivity gm = new GuessMasterActivity();
}
}
以及XML布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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.example.david.guessmaster.GuessMasterActivity">
<TextView
android:id = "@+id/ticket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/entityImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id = "@+id/entityName"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id = "@+id/guessInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout>
<Button
android:id="@+id/btnGuess"
style="@style/Widget.AppCompat.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</TableLayout>
</LinearLayout>
我错过了什么/哪里出了问题?我觉得我一定是误解了应用程序的流程 如果应用程序崩溃,请发布崩溃日志。你对发生的事情的描述告诉我们什么。它不会因为“没有初始视图加载”而崩溃,也不会因为崩溃而显示任何视图。虽然我可以在你的代码中看到一些奇怪的地方(活动很少->从来没有构造函数,也不应该通过new关键字来创建)。由于某种原因,它似乎不再崩溃,但它在开始时不会加载任何图片。所有内容都应该在onCreate中而不是构造函数中吗?然后我就不需要最后的GuessMasterActivity gm=新的GuessMasterActivity行了,对吗?