user3349697
user3349697

Reputation: 35

Can't change the text of a TextView with string passed through intent

I'm doing a very simple login screen where the user inputs their username and password, clicks the login button, and then the inputs are displayed on the next screen.

I get a NullPointerException every time I try to change the textview values. I've been googling for a long time and come up with nothing, and it has to be something simple that I'm just completely missing.

Following is my code:

public class LoggedIn extends Activity{
    String username = "";
    String password = "";

    TextView name;
    TextView pwd;

    Button infoDisp;

    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.logged_in);

        Intent intent = getIntent();
        name = (TextView)findViewById(R.id.username);
        pwd = (TextView)findViewById(R.id.password);

        username = intent.getStringExtra("nameInfo");
        password = intent.getStringExtra("passInfo");

        name.setText(username);
        pwd.setText(password);

    }
}

Edit: I changed the bundle to an intent above, also here's the rest of the code (The first activity)

public class LoginActivity extends Activity {

    Button loginBtn;
    Button registerBtn;
    EditText username;
    EditText pword;
    static String name;
    static String pass;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        username = (EditText) this.findViewById(R.id.username);
        pword = (EditText) this.findViewById(R.id.password);
        loginBtn = (Button) this.findViewById(R.id.loginBtn);

        loginBtn.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0){
                Intent intent = new Intent(LoginActivity.this, LoggedIn.class);
                name = username.getText().toString();
                pass = pword.getText().toString();
                intent.putExtra("nameInfo", name);
                intent.putExtra("passInfo", pass);
                startActivity(intent);
            }
        });

    }

}

I have no error checking for if the value is null on purpose because this was supposed to be just a quick run and done thing. I figure as long as I input something in each EditText, then the strings can't be null and I won't have a problem... right?

Stacktrace:

FATAL EXCEPTION: main
Process: com.example.loginscreen, PID: 1677
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.loginscreen/com.example.loginscreen.LoggedIn}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
    at android.app.ActivityThread.access$700(ActivityThread.java:135)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4998)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
    at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
    at com.example.loginscreen.LoggedIn.onCreate(LoggedIn.java:30)
    at android.app.Activity.performCreate(Activity.java:5243)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
    ... 11 more

Aaaand xml files: activity_login:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
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=".LoginActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:text="Login:"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="38dp"
    android:text="Password:"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
    android:id="@+id/username"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView2"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="20dp"
    android:layout_toRightOf="@+id/textView1"
    android:ems="10" />

<EditText
    android:id="@+id/password"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/textView2"
    android:layout_alignParentRight="true"
    android:layout_toRightOf="@+id/textView2"
    android:ems="10"
    android:inputType="textPassword" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/loginBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView2"
    android:layout_below="@+id/textView2"
    android:layout_marginTop="26dp"
    android:text="Login" />

<Button
    android:id="@+id/registerBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/loginBtn"
    android:layout_alignBottom="@+id/loginBtn"
    android:layout_alignLeft="@+id/textView3"
    android:layout_marginLeft="46dp"
    android:text="Register" />

<CheckBox
    android:id="@+id/remPass"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/loginBtn"
    android:layout_below="@+id/loginBtn"
    android:layout_marginTop="19dp"
    android:text="Remember Password" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/remPass"
    android:layout_marginTop="28dp"
    android:layout_toRightOf="@+id/textView1"
    android:text="Forgot Password" />

logged_in:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
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=".LoginActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="Logged In!"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="56dp"
    android:text="Username: "
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView2"
    android:layout_below="@+id/textView2"
    android:layout_marginTop="56dp"
    android:text="Password: "
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/recUsername"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/textView2"
    android:layout_marginLeft="33dp"
    android:layout_toRightOf="@+id/textView2"
    android:editable="true"
    android:text=" "
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/recPassword"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/textView3"
    android:layout_alignRight="@+id/textView4"
    android:editable="true"
    android:text=" "
    android:textAppearance="?android:attr/textAppearanceLarge" />

<Button
    android:id="@+id/infoBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_alignRight="@+id/textView1"
    android:layout_below="@+id/textView3"
    android:layout_marginTop="62dp"
    android:text="Press to Display Info" />

Upvotes: 3

Views: 797

Answers (6)

Hamid Shatu
Hamid Shatu

Reputation: 9700

Change this...

Bundle bundle = getIntent().getExtras();

to...

Intent bundle = getIntent();

Then use this to retrieve extras....

username = bundle.getStringExtra("nameInfo");
password = bundle.getStringExtra("passInfo");

Update:

You are trying to get TextViews using those ids which does not exist in the current layout xml...that is, you are using wrong id to retrieve your Textviews. So, change those ids in these following lines...

username = (TextView) this.findViewById(R.id.username);
pword = (TextView) this.findViewById(R.id.password);

to

username = (TextView) this.findViewById(R.id.recUsername);
pword = (TextView) this.findViewById(R.id.recPassword);

Upvotes: 0

Raghunandan
Raghunandan

Reputation: 133560

Change

 name = (TextView)findViewById(R.id.username);
 pwd = (TextView)findViewById(R.id.password);

to

 name = (TextView)findViewById(R.id.recUsername);
 pwd = (TextView)findViewById(R.id.recPassword);

In LoggedIn.java

Your NUllPointerExceptipon is ccoz you referencing wrong ids for your views. findViewById looks for a view in the current infalted layout. Since it does not find one. Your initialization fails leading to NullPointerExcpetion.

Upvotes: 1

Piyush
Piyush

Reputation: 18923

The problem is because you are finding id for TextView s are wrong.. so just change from

    username = (TextView) this.findViewById(R.id.username);
    pword = (TextView) this.findViewById(R.id.password);

to

    username = (TextView) this.findViewById(R.id.recUsername);
    pword = (TextView) this.findViewById(R.id.recPassword);

Upvotes: 2

Vamshi
Vamshi

Reputation: 1495

In your LoggedIn class the ids are wrong

name = (TextView)findViewById(R.id.username);
pwd = (TextView)findViewById(R.id.password);

change the above ids to defined ids in the logged_in.xml layout and try again

Upvotes: 0

M D
M D

Reputation: 47817

You should replace this in your LoggedIn Activity. You have wrong ids when initialization of views logged_in.xml

    username = (EditText) this.findViewById(R.id.username);
    pword = (EditText) this.findViewById(R.id.password);
    loginBtn = (Button) this.findViewById(R.id.loginBtn);

With

    TextView username = (TextView)findViewById(R.id.recUsername);
    TextView pword = (TextView)findViewById(R.id.recPassword);
    loginBtn = (Button)findViewById(R.id.infoBtn);

Upvotes: 1

Nirbhay Mishra
Nirbhay Mishra

Reputation: 1648

I think there is a problem with following line

     Intent intent = new Intent(LoginActivity.this, LoggedIn.class);

change it to

     Intent intent = new Intent(this, LoggedIn.class);

or

 Intent intent = new Intent(getApplicationContext(), LoggedIn.class);

Upvotes: 0

Related Questions