kira423
kira423

Reputation: 427

getExtras() returning null

This is the code I have.

In my main .java file.

public void showQuiz (View view){
        Username = (EditText) findViewById(R.id.Username);
        String userId = Username.getText().toString();
        String deviceId = Secure.getString(this.getContentResolver(),
                Secure.ANDROID_ID);
       //this intent is used to open other activity wich contains another webView
         Intent intent = new Intent(this, Quiz.class);

         Bundle bun = new Bundle();
         bun.putString("userid",userId);
         bun.putString("deviceid", deviceId);

         intent.putExtras(bun);
         startActivity(intent); 
    }

This is what is in the Activity's .java file.

package com.earn.egpt;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class Quiz extends Activity {
    private static final String url = "http://www.m.s-w-family.com/?subid=";
    private static final String url2 = "&deid=";

     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_quiz);
            Bundle extras=getIntent().getExtras();
            String userId=extras.getString("userId");
            String deviceId=extras.getString("deviceId");
         WebView webview;
        webview = (WebView) findViewById(R.id.webView1);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.loadUrl(url+userId+url2+deviceId);
        }
    }

for some reason it is passing the strings as null, but I have no idea why. I am extremely new to this kind of programming so I don't even know where to being. I have looked at other problems similar to mine and tried the fixes for them, but the values are still null.

Upvotes: 0

Views: 3016

Answers (2)

Tim
Tim

Reputation: 35933

Watch the capitalization of your variables. You're inserting "userid" and retrieving "userId".

Upvotes: 5

Sam
Sam

Reputation: 86948

You are using an extra Bundle by accident. Put the extras directly into the Intent instead:

intent.putExtra("userid", userId);
intent.putExtra("deviceid", deviceId);

startActivity(intent); 

Now your getString() methods will return what you want in Quiz.

Upvotes: 2

Related Questions