我做了一个非常简单的应用程序但由于某种原因它只是崩溃。第一页是一个简单的登录屏幕,但是当我点击登录时它就崩溃了。奇怪的是,甚至没有任何繁重的代码可以让它做任何奇怪的事情;到目前为止,我只是在活动之间旅行。
主要活动
package com.example.philip.lottery1;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class LoginActivity extends AppCompatActivity {
Button loginButton;
EditText userNameField;
EditText passwordField;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
loginButton = (Button) findViewById(R.id.loginButton);
userNameField = (EditText) findViewById(R.id.userNameField);
passwordField = (EditText) findViewById(R.id.passwordField);
final String userName = userNameField.getText().toString();
String password = passwordField.getText().toString();
loginButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), CreateTicket.class);
intent.putExtra("ClerkID", userName);
startActivity(intent);
}
});
}
}
它导致的活动
package com.example.philip.lottery1;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.Random;
public class CreateTicket extends AppCompatActivity {
private Button randomButton;
private Button OKButton;
private Button searchButton;
private EditText[] lottoNumberFields;
private int[] lottoNumbers;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_ticket);
lottoNumberFields = new EditText[5];
lottoNumberFields[1] = (EditText)findViewById(R.id.num1);
lottoNumberFields[2] = (EditText)findViewById(R.id.num2);
lottoNumberFields[3] = (EditText)findViewById(R.id.num3);
lottoNumberFields[4] = (EditText)findViewById(R.id.num4);
lottoNumberFields[5] = (EditText)findViewById(R.id.num5);
randomButton = (Button) findViewById(R.id.randomButton);
OKButton = (Button) findViewById(R.id.OKButton);
searchButton = (Button) findViewById(R.id.searchButton);
randomButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
randommize();
}
});
OKButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
createTicket();
}
});
searchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
search();
}
});
}
private void search()
{
//insert code here
Intent intent = new Intent (getBaseContext(),TicketActivity.class);
startActivity(intent);
}
private void randommize()
{
for (int i = 0 ; i < 5 ; i ++)
{
Random random = new Random();
int num = random.nextInt(49) + 1;
lottoNumberFields[i].setText(num+"");
}
}
private void createTicket()
{
for (int i = 0; i < 5; i++)
{
lottoNumbers[i] = Integer.parseInt(lottoNumberFields[i].getText().toString());
}
Intent intent = new Intent(getBaseContext(), FinalActivity.class);
intent.putExtra("lottoNumbers",lottoNumbers);
startActivity(intent);
}
}
答案 0 :(得分:0)
您的lottoNumberFields
数组的长度为5,但是它的索引值为1到5,而不是0到4.更改代码如下:
lottoNumberFields = new EditText[5];
lottoNumberFields[0] = (EditText)findViewById(R.id.num1);
lottoNumberFields[1] = (EditText)findViewById(R.id.num2);
lottoNumberFields[2] = (EditText)findViewById(R.id.num3);
lottoNumberFields[3] = (EditText)findViewById(R.id.num4);
lottoNumberFields[4] = (EditText)findViewById(R.id.num5);