I have nullpointer exception.
Function in MainActivity.
public void intentLoginUser(){
Intent aux = new Intent(MainActivity.this, LoginUserSplashScreen.class);
aux.putExtra("user", username);
aux.putStringArrayListExtra("notificationList", MainActivity.this.notificationsList);
aux.putStringArrayListExtra("friendList", MainActivity.this.friendList);
aux.putStringArrayListExtra("localizationList", MainActivity.this.localizationList);
MainActivity.this.startActivity(aux);
}
Function in another Activity, named LoginUserSplashScreen
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE); // Removes title bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // Removes notification bar
notificationList = getIntent().getStringArrayListExtra("notificationList");
friendList = getIntent().getStringArrayListExtra("friendList");
localizationList = getIntent().getStringArrayListExtra("localizationList");
myUsername = getIntent().getStringArrayListExtra("user");
Log.e("Erro Aqui YA?", myUsername.get(0));
setContentView(R.layout.activity_loginuser);}
What's wrong?
I have nullpointer in function onCreate, in Log.e(); ... I'm trying debugging that, but I can't.
EDIT - Nullpointer in function onCreate in line Log.e()
Because all lists are null (can't get anything from last activity).
Try
getIntent().getStringExtra("user");
instead of
getIntent().getStringArrayListExtra("user");
"user" was not ArrayList.
You have a mismatch in your put and get calls.
aux.putExtra("user", username);
myUsername = getIntent().getStringArrayListExtra("user");
Use aux.putStringArrayListExtra("user", username) with getIntent().getStringArrayListExtra("user") (assuming username is an ArrayList< String >) and use aux.putExtra("user", username) with getIntent().getStringExtra("user") (assuming username is a String).
In main activity you have assigned object with putExtra
aux.putExtra("user", username);
But in receiving point your getStringArrayListExtra
myUsername = getIntent().getStringArrayListExtra("user");
Log.e("Erro Aqui YA?", myUsername.get(0));
This is error code
aux.putExtra("user", username);
myUsername = getIntent().getStringArrayListExtra("user");
you can use bundle,this code is work for you
getIntent().getStringExtra("user");
or
Bundle bundle = new Bundle();
bundle.putString("user", username);
aux.putExtras(bundle);
getIntent().getExtras().getString("user");
I think the issue is that when you assign myUsername, you are saying that the variable with key "user" is a StringArrayList, but instead it is a String when you put it in the intent. The putExtra() method takes String types, not StringArrayLists.
My solution,
In MainActivity:
public ArrayList<String> usernameList = new ArrayList<String>();
public Queue<String> serverMessages = new LinkedList<String>();
public ArrayList<String> notificationsList = new ArrayList<String>();
public ArrayList<String> friendList = new ArrayList<String>();
public ArrayList<String> localizationList = new ArrayList<String>();
public void intentLoginUser(){
Intent aux = new Intent(MainActivity.this, LoginUserSplashScreen.class);
usernameList.add(username);
aux.putStringArrayListExtra("user", MainActivity.this.usernameList);
aux.putStringArrayListExtra("notificationList", MainActivity.this.notificationsList);
aux.putStringArrayListExtra("friendList", MainActivity.this.friendList);
aux.putStringArrayListExtra("localizationList", MainActivity.this.localizationList);
MainActivity.this.startActivity(aux);
}
In LoginUserSplashScreen
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE); // Removes title bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // Removes notification bar
notificationList = getIntent().getStringArrayListExtra("notificationList");
friendList = getIntent().getStringArrayListExtra("friendList");
localizationList = getIntent().getStringArrayListExtra("localizationList");
myUsername = getIntent().getStringArrayListExtra("user");
Log.e("Erro Aqui YA?", myUsername.get(0));
setContentView(R.layout.activity_loginuser);
// Start timer and launch main activity
IntentLauncher launcher = new IntentLauncher();
launcher.start();
}
And Log.e(); works and pass all data.
Related
hello there i'm a new beginner in android development. I tried several times to figure out my issue but i couldn't please help me.I created a simple login screen once the user enters the username and password I simply want to display it in the next activity. I used putExtra and getExtra but the values are null all the time.
This is my code
Intent i = new Intent(Data1.this , Data2.class);
i.putExtra("uname",username.getText().toString());
i.putExtra("pass",password.getText().toString());
Log.d("username",username.getText().toString());
Log.d("password",password.getText().toString());
startActivity(i);
data = (TextView)findViewById(R.id.txt);
Intent i = this.getIntent();
u = i.getStringExtra("username");
p = i.getStringExtra("password");
data.setText(u+" Successfully logged in User Name - "+ u + " Password - "+ p);
You've Passed the Data to the next Intent Correctly using PutExtra but in the new intent when you've used getStringExtra you've used another String variable which is completely different from what you've passed.Try this example it should work.You should use i.getStringExtra("uname"); and i.getStringExtra("pass"); instead of what you have passed.
public class Data1 extends AppCompatActivity {
EditText username;
EditText password;
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data1);
username = (EditText)findViewById(R.id.edusername);
password = (EditText)findViewById(R.id.edpassword);
btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(Data1.this , Data2.class);
i.putExtra("uname",username.getText().toString());
i.putExtra("pass",password.getText().toString());
Log.d("username",username.getText().toString());
Log.d("password",password.getText().toString());
startActivity(i);
}
});
}
}
public class Data2 extends AppCompatActivity {
TextView data;
String u;
String p;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data2);
data = (TextView)findViewById(R.id.txt);
// Intent i = getIntent();
Intent i = this.getIntent();
u = i.getStringExtra("uname");
p = i.getStringExtra("pass");
data.setText(u+" Successfully logged in User Name - "+ u + " Password - "+ p);
}
}
You a passing the value using
i.putExtra("uname",username.getText().toString());
i.putExtra("pass",password.getText().toString());
and getting value using
u = i.getStringExtra("username");
p = i.getStringExtra("password");
here you must be use same key as you passed in intent so you have to use "uname" instead of "username" and "pass" instead of "password"..
example :
FirstActivity.java
i.putExtra("key", "value");
secondActivity.java
i.getStringExtra("key");
You are getting null values because strings are not same in put and get method.And values are always stored in same key.So change your code in get method like as-
Intent i = this.getIntent();
u = i.getStringExtra("uname");
p = i.getStringExtra("pass");`
I have two String list to send to an activity from an adapter in android.
I have these two arrays in my adapter,
private final List<String> toppingPriceTop;
private final List<String> toppingDescriptionTop;
and in my adapter I have this button, this button click sending these details to the activity,
customize.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Intent next = new Intent(context, ActivityCustomize.class);
next.putExtra("description", descriptions.get(position));
next.putExtra("imageUrl", imageUrls.get(position));
next.putExtra("toppingDescriptionTop", toppingDescriptionTop.get(position));
next.putExtra("toppingPriceTop", toppingPriceTop.get(position));
context.startActivity(next);
((Activity) context).overridePendingTransition(
R.anim.slide_in_right, R.anim.slide_out_left);
}
In my activity I'm receiving these data as,
String [] toppingPriceTop = getIntent().getStringArrayExtra("toppingPriceTop");
String [] toppingDescriptionTop = getIntent().getStringArrayExtra("toppingDescriptionTop");
String imageUrl = getIntent().getStringExtra("imageUrl");
String description = getIntent().getStringExtra("description");
My problem is I'm getting values for imageUrl and description in the activity but for both the list arrays I'm getting null. an anyone tell me where I have gone wrong and how can I correct this please.
Any help will be appreciated.
do it like
next.putStringArrayListExtra("toppingDescriptionTop", toppingDescriptionTop);
next.putStringArrayListExtra("toppingPriceTop", toppingPriceTop);
You Should pass all List
and get it like
List<String> toppingPriceTop = getIntent().getStringArrayListExtra("toppingPriceTop");
List<String> toppingDescriptionTop = getIntent().getStringArrayListExtra("toppingDescriptionTop");
OR
do it like
Bundle b=new Bundle();
b.putStringArrayListExtra("toppingPriceTop ", toppingPriceTop);
b.putStringArrayListExtra("toppingDescriptionTop", toppingDescriptionTop);
next.putExtras(extras)
context.startActivity(next);
and get it like:
Bundle b=getIntent().getExtras();
ArrayList<String> toppingPriceTop =b.getStringArrayList("toppingPriceTop");
ArrayList<String> toppingDescriptionTop =b.getStringArrayList("toppingDescriptionTop");
I am sending data from one activity to another through intent. I am sending two different strings but getting same value for both variable on next activity.
Here is my code :
public class Quizzes extends ActionBarActivity {
protected static final String QUIZ_TITLE = null;
protected static final String COURSE = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quizzes);
listView = (ListView) findViewById(R.id.listview);
String[] values = new String[] { "Quiz # 2" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1, values);
listView.setAdapter(adapter);
// ListView Item Click Listener
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
final String item = (String) parent.getItemAtPosition(position);
Intent intent = new Intent(getApplicationContext(), QuizDetail.class);
intent.putExtra(QUIZ_TITLE, item);
final String course = (String)textview.getText();
intent.putExtra(COURSE, course);
startActivity(intent);
}
});
}
}
If you see i am passing two string intent object :
1. QUIZ_TITLE
2. COURSE
When i debugged the application, I can see values like
1. QUIZ_TITLE = "Quiz # 1"
2. COURSE = "Intro to Computing"
All fine until here, but when i am retrieving these string on other activity, I am getting value "Intro to Computing" for both, here is code from that class.
public class QuizDetail extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz_detail);
Intent intent = getIntent();
String quizTitle = intent.getStringExtra(Quizzes.QUIZ_TITLE);
TextView quizTitleTextView = (TextView) findViewById(R.id.quizTitle);
quizTitleTextView.setText(quizTitle+" : TESTING..");
String courseTitle = intent.getStringExtra(Quizzes.COURSE);
TextView courseTitleTextView = (TextView) findViewById(R.id.courseTitle);
courseTitleTextView.setText(courseTitle);
}
}
I am not sure why I am getting same value "Intro to computing" from Quizzes.QUIZ_TITLE and Quizzes.COURSE.
Any help would be highly appreciated.
Thanks..
Anjum
You are using bad the intent.putExtra(),
You need to put a key (you need to know) as first param, to get the object in the other activity like:
...
String item = ...;
intent.putExtra("COURSE", item);
...
And you get the extras with:
...
intent.getStringExtra("COURSE");
...
Edited !!!
There's a couple of things here that should be mentioned.
QUIZ_TITLE and COURSE are both null (I can't see where they're set)
When you add something to the Extras Bundle, you're placing values in to a dictionary. The key for this dictionary you're using, in this case, is null. This means the second time you're putting in to the dictionary, QUIZ_TITLE (null) is being replaced with the key COURSE (null).
If you change QUIZ_TITLE and COURSE to an actual String value, it should sort that problem.
The second thing to note, is that there's a difference between getExtraString and getExtras.getString. I have written about this here
Hope that helps.
Please try this
Intent intent = getIntent();
String quizTitle = intent.getExtras().getString(Quizzes.QUIZ_TITLE);
String courseTitle = iintent.getExtras().getString(Quizzes.COURSE);
Update:
Oh now i see it too:
protected static final String QUIZ_TITLE = null;
protected static final String COURSE = null;
is really fatal because using a null value for a key is not useful and even if it is possible you are setting your value for the key 'null' first and overwrite it then by setting the value for key 'null' again.
Change it to:
protected static final String QUIZ_TITLE = "extra_quiz_title"
protected static final String COURSE = "extra_course";
for example
My program is crashing when I click on a button to bring me to another page. My OnClickListener seems correct but I know I've missed something. Can anyone see where I have went wrong?
error:
08-20 13:40:30.622: E/AndroidRuntime(1026): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.LoginScr.Example/com.LoginScr.Example.CredentialsActivity}: java.lang.NullPointerException
from main activity used to call 2nd activity:
lsRegstr.setOnClickListener (new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(LoginScrExample.this, CredentialsActivity.class);
startActivity(i);
}
credentials Activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
rName = (EditText)findViewById(R.id.reg_uname);
rCode= (EditText)findViewById(R.id.reg_pswd);
bReg = (Button) findViewById(R.id.reg_button);
bReg.setOnClickListener(this);
bRtn = (Button) findViewById(R.id.rtn_button);
bRtn.setOnClickListener(this);
Bundle RegBundle = getIntent().getExtras();
bundleRegName = RegBundle.getString(rUsername);
bundleRegCode = RegBundle.getString(rPasscode);
rName.setText(bundleRegName);
rCode.setText(bundleRegCode);
}
#Override
public void onClick (View v) {
rUsername = rName.getText().toString();
rPasscode = rCode.getText().toString();
RegDetails regDetails = new RegDetails();
regDetails.setlName(bundleRegName);
regDetails.setlCode(bundleRegCode);
if(v.getId()==R.id.rtn_button){
finish();
}else if(v.getId()==R.id.reg_button){
insertCredentials(regDetails);
}
}
private void insertCredentials(RegDetails regDetails){
LoginDB androidOpenDBHelper = new LoginDB(this);
SQLiteDatabase sqliteDB = androidOpenDBHelper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(LoginDB.COLUMN_NAME_USERNAME, rUsername);
contentValues.put(LoginDB.COLUMN_NAME_PASSWORD, rPasscode);
String[] whereClauseArgument = new String[1];
whereClauseArgument[0] = regDetails.getlName();
System.out.println(" Credentials Saved!" + whereClauseArgument[0]);
sqliteDB.close();
finish();
}
}
There is very limited information(as stack trace) and too many possibilities of errors. You are getting NullPointerException it means some where in your code you are trying to access a null object. One genuine possibility is at this part of the code in the 2nd activity where you are trying to get extra data from the intent. As you have not added any extra data in the 1st activity you will be receiving null and then you are trying to get string out of the bundle. Try to put a check for string to be null at this point of the code.
Bundle RegBundle = getIntent().getExtras();
if(RegBundle != null) {
bundleRegName = RegBundle.getString(rUsername);
bundleRegCode = RegBundle.getString(rPasscode);
}
With that limited info, it could be a number of things. One of the most likely causes is not declaring the new activity in your androidmanifest.xml. Have you declared the activity you're trying to launch in your androidmanifest.xml?
I am implementing a fitness app. I have a tab layout for this app, the first tab shows the location(latitude, longitude), speed and other status, and the second tab shows a google map where the running route will be shown as a polyline in the map.
The first tab activity has a location manager that receive the new location.
My question is: how can I transfer the data received from the location manager in the first tab activity to the google map tab activity?
As I know, by using intent.putExtra() and startActivity() can transfer data between 2 activities, but by calling startActivity() I will just immediately go to the map activity right? But I want to update the polyline in the map and stay in the status tab.
Any hints?
Thanks in advance :)
Create One Global class and declare static varible and assign value to it and use another class.
And another way
Intent i =new Intent()setClass(this, ListViewerIncompleted.class);
i.putStringArrayListExtra(name, value);
Ok i solved this with that code:
in my Login Activity(1st Activity) i need to pass username and Password strings:
btnLogin.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
String userName = txtUsername.getText().toString();
String password = txtPass.getText().toString();
//instance for UserFunctions.
UserFunctions userFunction = new UserFunctions();
JSONObject json = userFunction.loginUser(userName, password);
//Check login response
try {
if(json.getString(KEY_REQUESTRESULT)!= null ){
txtErrorM.setText("");
//Log.e("Passed fine:", "first if condition");
String res = json.getString(KEY_REQUESTRESULT);//Obtaining the value 0 or 1 from the KEY loginstatus from JSONObject.
if(Integer.parseInt(res) == 1){
Intent iii = new Intent("com.mariposatraining.courses.MariposaTrainingActivity");
Bundle bundle1 = new Bundle();
Bundle bundle2 = new Bundle();
bundle1.putString("UN", userName);
bundle2.putString("PW", password);
iii.putExtras(bundle1);
iii.putExtras(bundle2);
//iii.putExtra("userName", userName);
//iii.putExtra("Password", password);
startActivity(iii);
finish();
//Log.e("OBJECTOO", json.toString());
}
i send this Strings to the TAB HANDLER CLASS and then the activity that manages this information:
public class MariposaTrainingActivity extends TabActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
TabHost tabHost = getTabHost();
TabHost.TabSpec spec ; //recurse to the property tabs
Intent intent; //intent for open tabs
//created intent for open the class tab1
intent = new Intent().setClass(this, ListViewerIncompleted.class); //List 2 Incompleted
spec = tabHost.newTabSpec("ListViewerIncompleted").setIndicator("INCOMPLETED").setContent(intent);
tabHost.addTab(spec);
Bundle bundle1 = getIntent().getExtras();
String userName=bundle1.getString("UN");
Bundle bundle2 = getIntent().getExtras();
String password=bundle2.getString("PW");
Bundle bundle3 = new Bundle();
Bundle bundle4 = new Bundle();
bundle3.putString("UN", userName);
bundle4.putString("PW", password);
intent.putExtras(bundle3);
intent.putExtras(bundle4);}}
in the Class to use this info:
Bundle bundle3 = getIntent().getExtras();
String userName=bundle3.getString("UN"); //Getting the userName
Bundle bundle4 = getIntent().getExtras();
String password=bundle4.getString("PW");
Hope this give you ideas...
Use public static variables in your destination Activity to have immediate access to required values from source Activity.