我是Android开发的新手,并尝试创建我的第一个Android应用程序。我有一个简单的登录应用程序,但当我在模拟器/我自己的设备上运行此应用程序时,它给了我错误:
App意外停止了。发射器已经崩溃。
我试图弄清楚什么是错的,但没有任何想法。我已经对配置文件进行了更改,例如增加堆和ram大小,但它不起作用。我认为我的代码中存在一些错误,因为应用程序在模拟器和Android设备上都崩溃了。
这是我的代码:
LoginScreen1.java类
package com.example.dell.creditcard;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class LoginScreen1 extends Activity implements AdapterView.OnItemSelectedListener {
SharedPreferences sharedPreferences = getSharedPreferences("userLoginDetails",MODE_PRIVATE);
SharedPreferences.Editor edit= sharedPreferences.edit();
EditText editText1 = (EditText) findViewById(R.id.Edit1);
EditText editText2 = (EditText) findViewById(R.id.Edit2);
EditText editText3 = (EditText) findViewById(R.id.Edit3);
EditText editText4 = (EditText) findViewById(R.id.Edit4);
EditText editText5 = (EditText) findViewById(R.id.Edit5);
EditText editText6 = (EditText) findViewById(R.id.Edit6);
Spinner spinner = (Spinner) findViewById(R.id.Spinner);
Spinner cardType;
private String[] card = { "JCB", "Master", "Visa"};
boolean flag = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.LoginScreen1);
cardType = (Spinner) findViewById(R.id.Spinner);
cardType.setPrompt("Choose Card");
ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, card);
adapter_state
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
cardType.setAdapter(adapter_state);
cardType.setOnItemSelectedListener(this);
}
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
cardType.setSelection(position);
String selState = (String) cardType.getSelectedItem();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
@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 boolean isFullWidth(String s){
boolean bool = false;
int length = s.length();
for (int i = 0; i < length; i++) {
char c = s.charAt(i);
if ('\uFF01' <= c && c <= '\uFF60' || '\uFFE0' <= c && c <= '\uFFE6') {
continue;
}
bool = true;
break;
}
return bool;
}
public boolean isHalfWidth(String s){
boolean bool = false;
int length = s.length();
for (int i = 0; i < length; i++) {
char c = s.charAt(i);
if ('\u0000' <= c && c <= '\u00FF' || '\uFF61' <= c && c <= '\uFFDC' || '\uFFE8' <= c && c <= '\uFFEE') {
continue;
}
bool = true;
break;
}
return bool;
}
public void send(View view) {
if (editText1.getText().toString().equals("")) {
editText1.setError("Username is required");
flag = true;
}
if (editText2.getText().toString().equals("")) {
editText2.setError("Password is required");
flag = true;
}
if (editText2.getText().toString().length() != 4) {
editText2.setError("password should only contain 4 digits");
flag = true;
}
if (editText3.getText().toString().equals("")) {
editText3.setError("Confirm Password is required");
flag = true;
}
if (editText3.getText().toString().length() != 4) {
editText3.setError("confirm password should only contain 4 digits");
flag = true;
}
if (!editText2.getText().toString().equals(editText3.getText().toString())) {
editText3.setError("Password and Confirm Password must be same");
flag = true;
}
String s = editText4.getText().toString();
if(s == null){
editText4.setError("Name(Kanji) is required");
flag =true;
}
if(isFullWidth(s)){
editText4.setError("Characters must be Full-Width Characters");
flag =true;
}
s = editText5.getText().toString();
if(s == null){
editText4.setError("Name(Roman) is required");
flag =true;
}
if(isHalfWidth(s)){
editText4.setError("Characters must be Half-Width Characters");
flag =true;
}
if (!isEmailValid(editText6.getText().toString())){
editText6.setError("Email is not valid");
flag = true;
}
/*in.putExtra("com.example.dell.creditcard.m1",editText1.getText().toString());
in.putExtra("com.example.dell.creditcard.m2",editText2.getText().toString());
in.putExtra("com.example.dell.creditcard.m3",editText3.getText().toString());
in.putExtra("com.example.dell.creditcard.m4",editText4.getText().toString());
in.putExtra("com.example.dell.creditcard.m5",editText5.getText().toString());
in.putExtra("com.example.dell.creditcard.m6",editText6.getText().toString());
in.putExtra("com.example.dell.creditcard.m", spinner.getSelectedItem().toString());*/
edit.putString("com.example.prateeksharma.userName",editText1.getText().toString());
edit.putString("com.example.prateeksharma.password",editText2.getText().toString());
edit.putString("com.example.prateeksharma.confirmPassword",editText3.getText().toString());
edit.putString("com.example.prateeksharma.nameKanji",editText4.getText().toString());
edit.putString("com.example.prateeksharma.nameRoman",editText5.getText().toString());
edit.putString("com.example.prateeksharma.email",editText6.getText().toString());
edit.putString("com.example.prateeksharma.cardType", spinner.getSelectedItem().toString());
edit.apply();
if(flag) {
Intent in = new Intent(this, LoginScreen1.class);
flag= false;
startActivity(in);
finish();
}
else {
Intent in = new Intent(this, LoginScreen2.class);
startActivity(in);
}
}
@Override
protected void onResume() {
editText1.setText(sharedPreferences.getString("com.example.prateeksharma.userName",""));
editText2.setText(sharedPreferences.getString("com.example.prateeksharma.password",""));
editText3.setText(sharedPreferences.getString("com.example.prateeksharma.confirmPassword",""));
editText4.setText(sharedPreferences.getString("com.example.prateeksharma.nameKanji",""));
editText5.setText(sharedPreferences.getString("com.example.prateeksharma.nameRoman",""));
editText6.setText(sharedPreferences.getString("com.example.prateeksharma.email",""));
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, card);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
if (!sharedPreferences.getString("com.example.prateeksharma.cardType","").equals(null)) {
int spinnerPosition = adapter.getPosition(sharedPreferences.getString("com.example.prateeksharma.cardType",""));
spinner.setSelection(spinnerPosition);
spinnerPosition = 0;
}
super.onResume();
}
public static boolean isEmailValid(String email) {
boolean isValid = false;
String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
CharSequence inputStr = email;
Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inputStr);
if (matcher.matches()) {
isValid = true;
}
return isValid;
}
@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);
}
}
This class is a simple login class. I'm storing values in shared preference so that I can reset the values of the login form when user returns to this activity as I'm creating the new instance of activity every time.
我的xml文件是:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="fill_parent"
>
<!--Put form controls here-->
<TextView
android:id="@+id/TextViewTitle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/FromTitle"
android:textSize="20pt"
android:gravity="center">
</TextView>
<View
android:layout_width="fill_parent"
android:layout_height="30dp">
</View>
<TextView
android:id="@+id/Spin"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/cardType"
android:textSize="10pt">
</TextView>
<Spinner
android:id="@+id/Spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
<View
android:layout_width="fill_parent"
android:layout_height="30dp">
</View>
<TextView
android:id="@+id/username"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/username"
android:textSize="10pt">
</TextView>
<EditText
android:id="@+id/Edit1"
android:layout_height="wrap_content"
android:inputType="text"
android:layout_width="fill_parent">
</EditText>
<View
android:layout_width="fill_parent"
android:layout_height="30dp">
</View>
<TextView
android:id="@+id/password"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/password"
android:textSize="10pt">
</TextView>
<EditText
android:id="@+id/Edit2"
android:layout_height="wrap_content"
android:inputType="numberPassword"
android:layout_width="fill_parent">
</EditText>
<View
android:layout_width="fill_parent"
android:layout_height="30dp">
</View>
<TextView
android:id="@+id/confirmPassword"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/confirmpassword"
android:textSize="10pt">
</TextView>
<EditText
android:id="@+id/Edit3"
android:layout_height="wrap_content"
android:inputType="numberPassword"
android:layout_width="fill_parent">
</EditText>
<View
android:layout_width="fill_parent"
android:layout_height="30dp">
</View>
<TextView
android:id="@+id/nameKanji"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/nameKanji"
android:textSize="10pt">
</TextView>
<EditText
android:id="@+id/Edit4"
android:layout_height="wrap_content"
android:maxLength="10"
android:layout_width="fill_parent">
</EditText>
<View
android:layout_width="fill_parent"
android:layout_height="30dp">
</View>
<TextView
android:id="@+id/nameRoman"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/nameRoman"
android:textSize="10pt">
</TextView>
<EditText
android:id="@+id/Edit5"
android:layout_height="wrap_content"
android:inputType="text"
android:maxLength="20"
android:layout_width="fill_parent">
</EditText>
<View
android:layout_width="fill_parent"
android:layout_height="30dp">
</View>
<TextView
android:id="@+id/email"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/email"
android:textSize="10pt">
</TextView>
<EditText
android:id="@+id/Edit6"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:layout_width="fill_parent">
</EditText>
<View
android:layout_width="fill_parent"
android:layout_height="30dp">
</View>
<Button
android:id="@+id/Next"
android:layout_height="wrap_content"
android:text="@string/data"
android:onClick="send"
android:layout_width="fill_parent">
</Button>
</LinearLayout>
</ScrollView>
有人可以解释一下导致应用程序崩溃的错误吗?
答案 0 :(得分:5)
移动
SharedPreferences sharedPreferences = getSharedPreferences("userLoginDetails",MODE_PRIVATE);
SharedPreferences.Editor edit= sharedPreferences.edit();
EditText editText1 = (EditText) findViewById(R.id.Edit1);
EditText editText2 = (EditText) findViewById(R.id.Edit2);
EditText editText3 = (EditText) findViewById(R.id.Edit3);
EditText editText4 = (EditText) findViewById(R.id.Edit4);
EditText editText5 = (EditText) findViewById(R.id.Edit5);
EditText editText6 = (EditText) findViewById(R.id.Edit6);
Spinner spinner = (Spinner) findViewById(R.id.Spinner);
在onCreate(...)
之后的setContentView(....)
中
答案 1 :(得分:2)
写下你的活动:
public class ActivityTest extends Activity {
SharedPreferences sharedPreferences;
SharedPreferences.Editor edit;
EditText editText1;
EditText editText2;
EditText editText3;
EditText editText4;
EditText editText5;
EditText editText6;
Spinner spinner;
Spinner cardType;
private String[] card = { "JCB", "Master", "Visa" };
boolean flag = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContetentView(R.layout.activity_main);
editText1 = (EditText) findViewById(R.id.Edit1);
editText2 = (EditText) findViewById(R.id.Edit2);
editText3 = (EditText) findViewById(R.id.Edit3);
editText4 = (EditText) findViewById(R.id.Edit4);
editText5 = (EditText) findViewById(R.id.Edit5);
editText6 = (EditText) findViewById(R.id.Edit6);
spinner = (Spinner) findViewById(R.id.Spinner);
sharedPreferences = getSharedPreferences(
"userLoginDetails", MODE_PRIVATE);
edit = sharedPreferences.edit();
}
}