Reputation: 5
I've found a few replies to similar postings on here, but none of them have been able to solve my problem. I have a simple app with two activities. In EntryActivity, values are entered into EditTexts, then displayed in DisplayActivity. I'm fairly certain both activities are assigned correctly in the manifest, but it stops working between activities, giving the title error. I've tried moving several things around, but I've had no luck at all.
EntryActivity:
package com.natep.userentry;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioButton;
import android.widget.Button;
public class EntryActivity extends Activity {
//Instantiate variables
public String sex = "";
public String firstName = "";
public String lastName = "";
public String age = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_entry);
//Instantiate the EditTexts and assign listeners
EditText firstNameEditText = (EditText) findViewById(R.id.firstNameEditText);
firstNameEditText.addTextChangedListener(firstNameEditTextListener);
EditText lastNameEditText = (EditText) findViewById(R.id.lastNameEditText);
lastNameEditText.addTextChangedListener(lastNameEditTextListener);
EditText ageEditText = (EditText) findViewById(R.id.ageEditText);
ageEditText.addTextChangedListener(ageEditTextListener);
RadioButton radio_male = (RadioButton) findViewById(R.id.radio_male);
RadioButton radio_female = (RadioButton) findViewById(R.id.radio_female);
Button enterButton = (Button) findViewById(R.id.enterButton);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.entry, menu);
return true;
}
//Method for handling radio button input
public void radioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radio_male:
if (checked)
sex = "Male";
break;
case R.id.radio_female:
if (checked)
sex = "Female";
break;
}
}
//Handles the First Name EditText
private TextWatcher firstNameEditTextListener = new TextWatcher()
{
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
try
{
firstName = s.toString();
}
catch (NumberFormatException e)
{
firstName = "";
}
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0)
{
}
};
//Handles the Last Name EditText
private TextWatcher lastNameEditTextListener = new TextWatcher()
{
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
try
{
lastName = s.toString();
}
catch (NumberFormatException e)
{
lastName = "";
}
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0)
{
}
};
//Handles the Age EditText
private TextWatcher ageEditTextListener = new TextWatcher()
{
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
try
{
age = s.toString();
}
catch (NumberFormatException e)
{
age = "";
}
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0)
{
}
};
//Define method to create intent on Button click
public void enterData(View view) {
Intent intent = new Intent(this, DisplayActivity.class);
intent.putExtra("FIRST_NAME", firstName);
intent.putExtra("LAST_NAME", lastName);
intent.putExtra("USER_AGE", age);
intent.putExtra("USER_SEX", sex) ;
startActivity(intent);
}
}
DisplayActivity
package com.natep.userentry;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TextView;
public class DisplayActivity extends Activity {
//Instantiate the necessary TextViews
TextView firstTextView = (TextView) findViewById(R.id.firstTextView);
TextView lastTextView = (TextView) findViewById(R.id.lastTextView);
TextView ageDisplayTextView = (TextView) findViewById(R.id.ageTextView);
TextView sexDisplayTextView = (TextView) findViewById(R.id.sexDisplayTextView);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
//Receive the bundle from Entry activity
Intent intent = getIntent();
Bundle extras = intent.getExtras();
//Instantiate strings from Entry activity
String firstName = extras.getString("FIRST_NAME");
String lastName = extras.getString("LAST_NAME");
String age = extras.getString("USER_AGE");
String sex = extras.getString("USER_SEX");
//Populate the TextViews with strings from Bundle
firstTextView.setText(firstName);
lastTextView.setText(lastName);
ageDisplayTextView.setText(age);
sexDisplayTextView.setText(sex);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display, menu);
return true;
}
}
Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.natep.userentry"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.natep.userentry.EntryActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.natep.userentry.DisplayActivity"
android:label="@string/title_activity_display" >
</activity>
</application>
</manifest>
(Edit) Error log, as requested
05-03 16:32:14.226: D/dalvikvm(1464): GC_FOR_ALLOC freed 59K, 6% free 2894K/3048K, paused 40ms, total 41ms
05-03 16:32:14.226: I/dalvikvm-heap(1464): Grow heap (frag case) to 3.497MB for 614416- byte allocation
05-03 16:32:14.296: D/dalvikvm(1464): GC_FOR_ALLOC freed 2K, 5% free 3491K/3652K, paused 59ms, total 59ms
05-03 16:32:14.856: D/gralloc_goldfish(1464): Emulator without GPU emulation detected.
05-03 16:32:23.506: W/IInputConnectionWrapper(1464): endBatchEdit on inactive InputConnection
05-03 16:32:26.146: I/Choreographer(1464): Skipped 30 frames! The application may be doing too much work on its main thread.
05-03 16:32:31.886: D/AndroidRuntime(1464): Shutting down VM
05-03 16:32:31.886: W/dalvikvm(1464): threadid=1: thread exiting with uncaught e exception (group=0xb3a14ba8)
05-03 16:32:31.926: E/AndroidRuntime(1464): FATAL EXCEPTION: main
05-03 16:32:31.926: E/AndroidRuntime(1464): Process: com.natep.userentry, PID: 1464
05-03 16:32:31.926: E/AndroidRuntime(1464): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.natep.userentry/com.natep.userentry.DisplayActivity}: java.lang.NullPointerException
05-03 16:32:31.926: E/AndroidRuntime(1464): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
05-03 16:32:31.926: E/AndroidRuntime(1464): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-03 16:32:31.926: E/AndroidRuntime(1464): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-03 16:32:31.926: E/AndroidRuntime(1464): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-03 16:32:31.926: E/AndroidRuntime(1464): at android.os.Handler.dispatchMessage(Handler.java:102)
05-03 16:32:31.926: E/AndroidRuntime(1464): at android.os.Looper.loop(Looper.java:136)
05-03 16:32:31.926: E/AndroidRuntime(1464): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-03 16:32:31.926: E/AndroidRuntime(1464): at java.lang.reflect.Method.invokeNative(Native Method)
05-03 16:32:31.926: E/AndroidRuntime(1464): at java.lang.reflect.Method.invoke(Method.java:515)
05-03 16:32:31.926: E/AndroidRuntime(1464): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-03 16:32:31.926: E/AndroidRuntime(1464): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-03 16:32:31.926: E/AndroidRuntime(1464): at dalvik.system.NativeStart.main(Native Method)
05-03 16:32:31.926: E/AndroidRuntime(1464): Caused by: java.lang.NullPointerException
05-03 16:32:31.926: E/AndroidRuntime(1464): at android.app.Activity.findViewById(Activity.java:1884)
05-03 16:32:31.926: E/AndroidRuntime(1464): at com.natep.userentry.DisplayActivity.<init>(DisplayActivity.java:12)
05-03 16:32:31.926: E/AndroidRuntime(1464): at java.lang.Class.newInstanceImpl(Native Method)
05-03 16:32:31.926: E/AndroidRuntime(1464): at java.lang.Class.newInstance(Class.java:1208)
05-03 16:32:31.926: E/AndroidRuntime(1464): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
05-03 16:32:31.926: E/AndroidRuntime(1464): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
05-03 16:32:31.926: E/AndroidRuntime(1464): ... 11 more
05-03 16:32:35.216: I/Process(1464): Sending signal. PID: 1464 SIG: 9
Upvotes: 0
Views: 971
Reputation: 5058
You're getting NPE because you're trying to get instance of textviews even before it's created.
These four line should be inside onCreate()
method of DisplayActivity
:
TextView firstTextView = (TextView) findViewById(R.id.firstTextView);
TextView lastTextView = (TextView) findViewById(R.id.lastTextView);
TextView ageDisplayTextView = (TextView) findViewById(R.id.ageTextView);
TextView sexDisplayTextView = (TextView) findViewById(R.id.sexDisplayTextView);
Upvotes: 1
Reputation: 152817
Move the findViewById() calls from member variable initialization to onCreate() after setContentView(). Before onCreate() there's no Window and you get this NPE. Before setContentView() you just won't find anything.
Upvotes: 0