Reputation: 1148
I get a LOT of errors from LogCat when I try running my app on an emulator. After looking around I think my main error is a the NullPointerException error which I assume is coming from calling the array of usernames from source and comparing them with the username posted on the form.
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());
}
}
--the xml code
<?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>
Thanks.
EDIT: Adding 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>
Upvotes: 1
Views: 165
Reputation: 977
Joining to all comments, you can also move the line
context = getApplicationContext();
to the onCreate method
i.e.
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getApplicationContext();
}
Upvotes: 0
Reputation: 6096
1st of all why you need getapplication() context in activity, if you can use "this" or MainActivity.class .If want to use Context context = getApplicationContext(); then put it in Oncreate() because it may contain the null value and can cause null point exception wherever used.But keep in mind which/how/where context to be used. Context object depends on where it came from originally.
Below is a table of the common places an application will receive a Context, and in each case what it is useful for: .
For details about context check outWhat is context
Upvotes: 0
Reputation: 5234
You don't use Context context = getApplicationContext(); in first line of your activity. it's wrong. If you need get context, you could use itself.
Just use this or this.getBaseContext()
Also, if you checked the error message it shown you special line where occurs error. ex. at com.example.test.MyActivity.(MyActivity.java:47) shows line 47
Upvotes: 1