エミュレーターでアプリを実行しようとすると、LogCat から多くのエラーが発生します。周りを見回した後、私の主なエラーは NullPointerException エラーであると思います。これは、ソースからユーザー名の配列を呼び出し、フォームに投稿されたユーザー名と比較することから来ていると思います。
package com.example.myapplication;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends Activity {
Context context = getApplicationContext();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void hitSubmit(View button){
final Spinner feedbackSpinner = (Spinner) findViewById(R.id.SpinnerFeedbackType);
String feedbackType = feedbackSpinner.getSelectedItem().toString();
if(feedbackType.equals("Please Select")){
Toast.makeText(this, "Please select a country.", Toast.LENGTH_SHORT).show();
return;
}
final EditText fnField = (EditText) findViewById(R.id.FN);
String FN = fnField.getText().toString();
if(FN.equals("")){
Toast.makeText(this, "Please enter your first name.", Toast.LENGTH_SHORT).show();
return;
}
final EditText lnField = (EditText) findViewById(R.id.LN);
String LN = lnField.getText().toString();
if(LN.equals("")){
Toast.makeText(this, "Please enter your last name.", Toast.LENGTH_SHORT).show();
return;
}
final EditText phoneField = (EditText) findViewById(R.id.PN);
String PN = phoneField.getText().toString();
if(PN.equals("")){
Toast.makeText(this, "Please enter your phone number.", Toast.LENGTH_SHORT).show();
return;
}
final EditText Email1Field = (EditText) findViewById(R.id.EA);
String EA1 = Email1Field.getText().toString();
if(EA1.equals("")){
Toast.makeText(this, "Please enter an email address.", Toast.LENGTH_SHORT).show();
return;
}
final EditText Email2Field = (EditText) findViewById(R.id.EA2);
String EA2 = Email2Field.getText().toString();
if(!EA2.equals(EA1)){
Toast.makeText(this, "Your email address' do not match", Toast.LENGTH_SHORT).show();
return;
}
final EditText userField = (EditText) findViewById(R.id.User);
String user = userField.getText().toString();
if(!user.equals("")){
Toast.makeText(this, "Please enter a username", Toast.LENGTH_SHORT).show();
return;
}
Resources res = getResources();
String[] usernameArray = res.getStringArray(R.array.usernames);
int userint = usernameArray.length;
for(int x=0;x<userint;x++){
if(user.equals(usernameArray[x])){
Toast.makeText(this, "Username already exists.", Toast.LENGTH_SHORT).show();
return;
}
}
RadioButton rb1, rb2;
rb1 = (RadioButton) findViewById(R.id.button1);
rb2 = (RadioButton) findViewById(R.id.button2);
if(!rb1.isChecked() && !rb2.isChecked()){
Toast.makeText(this, "Please select a gender.", Toast.LENGTH_SHORT).show();
return;
}
finish();
}
public void hitClear(View button){
finish();
startActivity(getIntent());
}
}
--xml コード
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Homework 1</string>
<string name="FN">First Name</string>
<string name="LN">Last Name</string>
<string name="PN">Phone Number</string>
<string name="EA">Email Address</string>
<string name="REEA">Re-enter Email</string>
<string name="User">Username</string>
<string name="Pw">Password</string>
<string name="Gender">Gender</string>
<string name="Country">Country</string>
<string name="action_settings">.</string>
<string name="clear">Clear</string>
<string name="submit">Submit</string>
<string name="female">Female</string>
<string name="male">Male</string>
<string name="country_prompt">Choose a country</string>
<string name="countrytype1">Malaysia</string>
<string name="countrytype2">United States</string>
<string name="countrytype3">Indonesia</string>
<string name="countrytype4">France</string>
<string name="countrytype5">Italy</string>
<string name="countrytype6">Singapore</string>
<string name="countrytype7">New Zealand</string>
<string name="countrytype8">India</string>
<string-array name="country_arrays">
<item>Please Select</item>
<item>Malaysia</item>
<item>United States</item>
<item>Indonesia</item>
<item>France</item>
<item>Italy</item>
<item>Singapore</item>
<item>New Zealand</item>
<item>India</item>
</string-array>
<string-array name="usernames">
<item>Bryan</item>
<item>John</item>
<item>Matt</item>
<item>Mike</item>
</string-array>
</resources>
ありがとう。
編集: activity_main.xml の追加
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/FN"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:hint="@string/FN"
android:inputType="textCapWords"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/LN"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:hint="@string/LN"
android:inputType="textCapWords"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/PN"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:hint="@string/PN"
android:inputType="phone"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/EA"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:inputType="textEmailAddress"
android:hint="@string/EA"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/EA2"
android:layout_width="fill_parent"
android:inputType="textEmailAddress"
android:layout_height="49dp"
android:hint="@string/REEA"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/User"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:hint="@string/User"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/Pw"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:hint="@string/Pw"
android:inputType="textPassword"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="53dp"
android:layout_weight="1.00"
android:onClick="onRadioButtonClicked"
android:text="@string/female" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1.00"
android:onClick="onRadioButtonClicked"
android:text="@string/male" />
</RadioGroup>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView9"
android:layout_width="0dip"
android:layout_height="49dp"
android:layout_weight="1"
android:text="@string/Country"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="@+id/SpinnerFeedbackType"
android:layout_width="210dp"
android:layout_height="41dp"
android:entries="@array/country_arrays"
android:prompt="@string/country_prompt" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:onClick="hitSubmit"
android:text="@string/clear" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"
android:onClick="hitClear"
android:text="@string/submit" />
</LinearLayout>
</LinearLayout>