Why the EditText widget ends up being null? - java

I am trying to get the input from the EditText fields and store it into an SQLite database. But the Fields are null even after I enter text into it.
I did a similar thing earlier and it ended up just fine but I am confused what is causing the problem here.
package com.example.mridul.eventmanager;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class CreateEvent extends AppCompatActivity {
EditText edit,edit2,edit3,edit4;
TextView t1;
ImageView img;
Button bpost;
public static final int PICK_IMAGE=1;
Uri imageuri;
Event in;
DatabaseHelper dc = new DatabaseHelper(this);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_event);
edit = (EditText) findViewById(R.id.editText7);
edit2 = (EditText) findViewById(R.id.editText8);
edit3 = (EditText) findViewById(R.id.editText9);
edit4 = (EditText) findViewById(R.id.editText10);
t1 = (TextView) findViewById(R.id.textView6);
img = (ImageView) findViewById(R.id.imageView);
bpost = (Button) findViewById(R.id.button3);
t1.setOnClickListener(new View.OnClickListener(){
public void onClick(View vim){
operationGallery();
}
});
bpost.setOnClickListener(new View.OnClickListener(){
public void onClick(View vPost){
in.sEventName(edit.getText().toString()); //Problem here
in.sEventDesc(edit2.getText().toString());
in.sEventVenue(edit3.getText().toString());
in.sType(edit4.getText().toString());
in.sPic(imageuri.toString());
dc.addEvent(in);
Toast.makeText(getApplicationContext(), "Data Saved", Toast.LENGTH_SHORT).show();
Intent gBack = new Intent(CreateEvent.this, LoginActivity.class);
CreateEvent.this.startActivity(gBack);
}
});
} public void operationGallery(){
Intent im = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(im , PICK_IMAGE);
}
public void onActivityResult(int requestcode, int resultcode, Intent data){
super.onActivityResult(requestcode, resultcode, data);
if((resultcode == RESULT_OK)&&(requestcode == PICK_IMAGE)){
imageuri = data.getData();
img.setImageURI(imageuri);
}
}
}
The following is the error which I am getting after executing the code

You have just forgot to initiate Event object "in"

Your Event object in is uninitialized.

Related

calling startActivityForResult isn't passing intents back properly

In my code, I have a class for a recipe and then another class for each step. The recipe class holds an arraylist of the steps in that recipe.
I have 3 processes being called one after the other:
MainActivity calls AddEditRecipe using startActivityResult
within AddEditRecipe I have another startActivityResult which calls AddStep
within AddStep I am adding a new instance of step to the list in the recipe.
It successfully adds the object to the list before finish() is called, but when I check the size of the list when I have gone to the 2nd activity it has reverted back to 0, even though I've managed to create buttons with the data I collected from the AddStep functions.
Basically I don't know what to do make it so it everything correctly persists.
Also a heads up, I am still fairly new too java so there's probably lots of other things wrong, any random advice is greatly appreciated.
MainActivity
import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
Button newRecipe;
Button editRecipe;
Button deleteRecipe;
LinearLayout recipeLayout;
List<NewRecipe> listOfRecipes;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listOfRecipes = new ArrayList<>();
recipeLayout = (LinearLayout) findViewById(R.id.RecipeListLayout);
newRecipe = (Button) findViewById(R.id.NewRecipeButton);
newRecipe.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, AddRecipePopUp.class);
startActivityForResult(i,998);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode==999 && resultCode == RESULT_OK){
NewRecipe rec = (NewRecipe)data.getParcelableExtra("curr_recipe");
Log.d("Number of Steps",Integer.toString(rec.listOfSteps.size()));
UpdateScreen(rec);
}
if(requestCode== 998 && resultCode == RESULT_OK){
NewRecipe recipe =(NewRecipe)data.getParcelableExtra("curr_recipe");
listOfRecipes.add(recipe);
Intent i = new Intent(MainActivity.this,AddEditRecipe.class);
i.putExtra("curr_recipe", recipe);
startActivityForResult(i,999);
}
}
void UpdateScreen(final NewRecipe recipe){
Button button = new Button(this);
button.setText(recipe.name);
button.setHeight(120);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
NewRecipe recipe1 = recipe;
Button thisBut = (Button)view;
String butName = thisBut.getText().toString().toLowerCase();
for(int j = 0;j<listOfRecipes.size();j++){
String curr = listOfRecipes.get(j).name.toString().toLowerCase();
Log.d("Button ",thisBut.getText().toString());
Log.d("Current List ",listOfRecipes.get(j).name.toString().toLowerCase());
Log.d("Check bool statement", Boolean.toString(butName ==curr));
}
//recipe1 = listOfRecipes.get(0);
Intent i = new Intent(MainActivity.this, AddEditRecipe.class);
i.putExtra("curr_recipe",recipe1);
startActivityForResult(i,997);
}
});
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
recipeLayout.addView(button,lp);
}
}
AddEditRecipe
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class AddEditRecipe extends Activity {
Button home;
Button add;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_edit_recipe);
Intent i = getIntent();
final NewRecipe this_recipe = i.getParcelableExtra("curr_recipe");
Log.d("Number of Steps",Integer.toString(this_recipe.listOfSteps.size()));
LinearLayout recipeLayout = (LinearLayout) findViewById(R.id.RecipeListLayout);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
for(int j = 0; j<this_recipe.listOfSteps.size();j++){
Button button = new Button(this);
button.setText(this_recipe.listOfSteps.get(j).processName);
button.setHeight(120);
recipeLayout.addView(button,lp);
}
home = (Button) findViewById(R.id.HomeButton);
home.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(AddEditRecipe.this, MainActivity.class);
i.putExtra("curr_recipe", this_recipe);
setResult(RESULT_OK,i);
Log.d("Number of Steps",Integer.toString(this_recipe.listOfSteps.size()));
finish();
}
});
add = (Button) findViewById(R.id.AddStepButton);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(AddEditRecipe.this, AddStep.class);
i.putExtra("curr_recipe", this_recipe);
startActivityForResult(i,999);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode==999 && resultCode == RESULT_OK){
NewRecipe curr_rec = (NewRecipe)data.getParcelableExtra("curr_recipe");
Button button = new Button(this);
button.setText(curr_rec.listOfSteps.get(curr_rec.listOfSteps.size()-1).processName + " " + curr_rec.listOfSteps.get(curr_rec.listOfSteps.size()-1).seconds );
button.setHeight(120);
LinearLayout lay = (LinearLayout) findViewById(R.id.RecipeListLayout) ;
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lay.addView(button,lp);
}
}
}
AddStep
import android.app.Activity;
import android.content.Intent;
import android.graphics.Canvas;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class AddStep extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_step);
Intent i = getIntent();
final NewRecipe this_recipe = (NewRecipe) i.getParcelableExtra("curr_recipe");
Button finish = (Button) findViewById(R.id.AddStepButton);
finish.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
EditText name = (EditText) findViewById(R.id.nameEditText);
EditText hours = (EditText) findViewById(R.id.HoursEditText);
EditText minutes = (EditText) findViewById(R.id.MinutesEditText);
EditText seconds = (EditText) findViewById(R.id.SecondsEditText);
this_recipe.AddStep((ConvertToSeconds(Integer.parseInt(hours.getText().toString()),Integer.parseInt(minutes.getText().toString()),Integer.parseInt(seconds.getText().toString()))),name.getText().toString());
Intent j = new Intent(AddStep.this, AddEditRecipe.class);
j.putExtra("curr_recipe", this_recipe);
Log.d("Number of Steps",Integer.toString(this_recipe.listOfSteps.size()));
setResult(RESULT_OK,j);
finish();
}
});
}
int ConvertToSeconds(int h, int m, int s){
int ans = 0;
for(int i = 0; i<h;i++){
ans+=3600;
}
for(int i = 0; i<m;i++){
ans+=60;
}
for(int i = 0; i<s;i++){
ans++;
}
return ans;
}
}

Adding new ListView item and get value of item from another activity

I am trying to make a "News App" with Android Studio. I have on my main activity a ListView and a floating button. On my second activity I got two TextViews, two EditViews and one Button. What I'd like to do is the following:
First I press on the floating Button from my main Activity. Then the second Activity should pop up and there I write on the two EditText's and then press on the button. After that I should go back to the main activity and there, a new Item should be added to the ListView. The new item should be filled with the text from the second activity's EditTexts.
This is my main activity:
package news;
import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Get subject value from addpost Activity
final String new_value = getIntent().getStringExtra("newSubject");
String [] new_subject = new String []
{new_value};
FloatingActionButton add_post_button = findViewById(R.id.post_btn);
final ListView post_view = findViewById(R.id.news_feed);
//Create Adapter to add the new subject to listview
if (new_subject != null) {
ArrayAdapter newslist_adapter = new ArrayAdapter(
MainActivity.this,
android.R.layout.simple_expandable_list_item_1, new_subject);
post_view.setAdapter(newslist_adapter);
}
add_post_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, addpost_activity.class);
MainActivity.this.startActivity(intent);
}
});
}
}
This is the second activity(addpost_activity):
package news;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.content.Intent;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
public class addpost_activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addpost_activity);
Button post_button = findViewById(R.id.post_btn);
final EditText subject_text = findViewById(R.id.subject_edit);
post_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(subject_text.getText() != null) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("newSubject", subject_text.getText().toString());
startActivity(intent);
}
}
});
}
}
My problem is now that the app crashes every time I start it. I also get this message from my logs:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
Solved:
Thanks to Munir, the problem was solved.
Here are my activities
MainActivity:
package news;
import android.app.Activity;
import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ArrayAdapter newslist_adapter;
ArrayList<String> new_subject = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FloatingActionButton add_post_button = findViewById(R.id.post_btn);
//Create Adapter to add the new subject to listview
add_post_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, addpost_activity.class);
startActivityForResult(intent, 1);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
final ListView post_view = findViewById(R.id.news_feed);
if (requestCode == 1) {
if(resultCode == Activity.RESULT_OK){
String new_value = data.getStringExtra("newSubject");
new_subject.add(new_value);
newslist_adapter = new ArrayAdapter(
MainActivity.this,
android.R.layout.simple_expandable_list_item_1, new_subject);
post_view.setAdapter(newslist_adapter);
}
}
}
}
This is my second Activity(addpost_activity):
package news;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.content.Intent;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
public class addpost_activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addpost_activity);
Button post_button = findViewById(R.id.post_btn);
final EditText subject_text = findViewById(R.id.subject_edit);
post_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(subject_text.getText() != null) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("newSubject", subject_text.getText().toString());
setResult(Activity.RESULT_OK, intent);
finish();
}
}
});
}
}
For that purpose I suggest using Fragments instead of two Activities. The MainActivity then holds all your data.
See Fragments guide.
From your MainActivity call the addpost_activity using startActivityForResult() method Instead of startActivity();
In your addpost_activity set the data which you want to return back to FirstActivity
post_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(subject_text.getText() != null) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("newSubject", subject_text.getText().toString());
setResult(Activity.RESULT_OK,intent );
finish();
}
}
});
And in the MainActivity Access the result by overriding onActivityResult()
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == Activity.RESULT_OK){
String result=data.getStringExtra("newSubject");
//Add this value to your adapter and call notifyDataSetChanged();
}
}
}
use ArrayList insted of String [] if you want to keep old entry in list change your code like below.
Main Activity
declare this variable as global
ArrayAdapter newslist_adapter;
ArrayList<String> new_subject = new ArrayList<>();
set Adapter like
newslist_adapter = new ArrayAdapter(
MainActivity.this,
android.R.layout.simple_expandable_list_item_1, new_subject);
post_view.setAdapter(newslist_adapter);
change startActivity to startActivityForResult
add_post_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, addpost_activity.class);
startActivityForResult(intent,100);
}
});
Override this method in onActivity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 100) {
String result=data.getStringExtra("newSubject");
new_subject.add(result)
//Add this value to your adapter and call newslist_adapter.notifyDataSetChanged();
}
}
And In your addpost_activity set the data which you want to return back
post_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(subject_text.getText() != null) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("newSubject", subject_text.getText().toString());
setResult(100,intent );
finish();
}
}
});
In MainActivity.java, you should override a function named "onActivityRecsult" this method will be called when you come back from second activity to first activity.

SecondActivity is not receiving the value - Intent related

import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
TextView t1,t2;
Button generate1;
MediaPlayer pickupgold, pickupplatinum;
SharedPreferences mySharedPreferences;
private static Button button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
pickupgold = MediaPlayer.create(this, R.raw.pickup_gold);
pickupplatinum = MediaPlayer.create(this, R.raw.pickup_platinum);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
OnClickButtonListener();
final ImageView imageView = (ImageView) findViewById(R.id.imageView);
final ImageView imageView2 = (ImageView) findViewById(R.id.imageView2);
t1 = (TextView) findViewById(R.id.textView3);
t2 = (TextView) findViewById(R.id.textView8);
generate1 = (Button) findViewById(R.id.button);
generate1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TextView userTextEntry = (TextView) findViewById(R.id.textView3);
String userData = userTextEntry.getText().toString();
int num2 = mySharedPreferences.getInt("INT_KEY1", Integer.parseInt(userData));
int num3 = mySharedPreferences.getInt("INT_KEY2", -1);
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("parameter1", num2);
Random rand = new Random();
int num1 = rand.nextInt(100);
if (num1 % 2 == 0) {
pickupgold.start();
num2 += 1;
SharedPreferences.Editor editor = mySharedPreferences.edit();
editor.putInt("INT_KEY1", num2);
editor.apply();
t1.setText(String.format("%d", num2));
imageView.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, android.R.anim.slide_in_left));
Toast.makeText(getApplicationContext(), R.string.foundgold2, Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(), R.string.notfoundgold, Toast.LENGTH_LONG).show();
} if (num1 == 1){
pickupplatinum.start();
num3 += 1;
SharedPreferences.Editor editor = mySharedPreferences.edit();
editor.putInt("INT_KEY2", num3);
editor.apply();
t2.setText(String.format("%d", num3));
imageView2.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, android.R.anim.slide_in_left));
Toast.makeText(getApplicationContext(), R.string.foundplatinum1, Toast.LENGTH_LONG).show();
}
}
});
}
public void OnClickButtonListener() {
button2 = (Button)findViewById(R.id.button2);
button2.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("com.example.tans.goldminer1.SecondActivity");
startActivity(intent);
}
}
);
}
This is MainActivity.java
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Intent intent = getIntent();
int goldamountval = intent.getIntExtra("parameter1", 0);
TextView goldamount = (TextView) findViewById(R.id.goldamount);
goldamount.setText("" + goldamountval);
}
and This is SecondActivity.java
I think the int value should be loaded from MainActivity.java but
When I go to SecondActivity, It does not seem to appear, Just showing 0.
What's the reason and how can i solve it? and Sorry, My Code may look like confused. :<
Try this on SecondActivity
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("parameter1", num2);
startActivity(intent );
You need to start the SecondActivity after the first two lines :
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("parameter1", num2);
startActivity(intent);
Intent intent = new Intent(context, YourActivityClass.class);
intent.putExtra(KEY, );
startActivity(intent);
Seocnd Activity-
Intent intent = getIntent();
if (null != intent) {
String stringData= intent.getStringExtra(KEY);
int numberData = intent.getIntExtra(KEY, defaultValue);
boolean booleanData = intent.getBooleanExtra(KEY, defaultValue);
char charData = intent.getCharExtra(KEY, defaultValue);
}

java.lang.RuntimeException: Performing stop of activity that is not resumed:

I am getting a runtime exception when I am calling NavDrawerActivity from LoginScreen activity.
Error
java.lang.RuntimeException: Performing stop of activity that is not
resumed:
{com.example.owner.loginapp/com.example.owner.loginapp.NavDrawerActivity}
How to fix this error?
Here is my loginScreen code
package com.example.owner.loginapp;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import android.content.SharedPreferences;
public class LoginScreen extends AppCompatActivity {
ImageView image;
ImageView user_img;
ImageView pass_img;
Button log_in;
Button reg;
EditText editText_Username;
EditText editText_Pass;
UserSessionManager session;
private SharedPreferences sharedPreferences;
private static final String PREFER_NAME = "Reg";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
session = new UserSessionManager(getApplicationContext());
image = (ImageView) findViewById(R.id.imageView);
image.setImageResource(R.drawable.smile);
user_img = (ImageView) findViewById(R.id.user_img);
user_img.setImageResource(R.drawable.user_img);
pass_img = (ImageView) findViewById(R.id.pass_img);
pass_img.setImageResource(R.drawable.img_pass);
log_in = (Button) findViewById(R.id.btn_Login);
reg = (Button) findViewById(R.id.btn_Reg);
editText_Username = (EditText) findViewById(R.id.editText_UserName);
editText_Pass = (EditText) findViewById(R.id.editText_Password);
Bitmap icon = BitmapFactory.decodeResource(getResources(),
image.setImageBitmap(icon);
Toast.makeText(getApplicationContext(),
"User Login Status: " + session.isUserLoggedIn(),
Toast.LENGTH_LONG).show();
sharedPreferences = getSharedPreferences(PREFER_NAME, Context.MODE_PRIVATE);
log_in.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String username = editText_Username.getText().toString();
String password = editText_Pass.getText().toString();
if (username.trim().length() > 0 && password.trim().length() > 0) {
String uName = null;
String uPassword = null;
if (sharedPreferences.contains("Name")) {
uName = sharedPreferences.getString("Name", "");
}
if (sharedPreferences.contains("Password")) {
uPassword = sharedPreferences.getString("Password", "");
}
if (username.equals(uName) && password.equals(uPassword)) {
session.createUserLoginSession(uName, uPassword);
Intent i = new Intent(getApplicationContext(), NavDrawerActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
} else {
Toast.makeText(getApplicationContext(),
"Username/Password is incorrect",
Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(getApplicationContext(),
"Please enter username and password",
Toast.LENGTH_LONG).show();
}
}
});
reg.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.register);
Intent i = new Intent(getApplicationContext(), Reg.class);
startActivity(i);
}
});
}
}
I copied your code and tried to instantiate a Reg Activity class that I designed and everything worked fine. It is possible that you are doing something wrong in the 1onResumeoronCreate` of Reg class.
Post that class too for more information.

Switching activities/passing data between activities

So using suggestions from the last question I asked, I figured out how to call BarCodeScanner, and return the value to a field. so now, I have a toast that says "successful scan" and then I want to pass the result to a new activity. when I comment out my intent, everything works (minus the passing of data/switching of screen, obviously) but when I run my project as is, it FC's... no errors reported by eclipse in code or XML. Any insights?
package com.mhe.test.scan;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button myScanButton = (Button) findViewById(R.id.myScanButton);
totalbox = (EditText) findViewById(R.id.tBox);
myScanButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
startActivityForResult(intent, 0);
}
});
}
private EditText totalbox;
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
final String contents = intent.getStringExtra("SCAN_RESULT");
if ( totalbox != null )
totalbox.setText(contents);
Context context = getApplicationContext();
CharSequence text = "Successful Scan";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
Intent i = new Intent(main.this, Result.class);
i.putExtra("SNARP", "SCAN_RESULT");
startActivityForResult(i, 0);
} else if (resultCode == RESULT_CANCELED) {
if ( totalbox != null )
totalbox.setText("bummer");
}
}
}
}
And then to handle the data being passed, in the new activity:
package com.mhe.test.scan;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Result extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
Intent i = getIntent();
Bundle b = i.getExtras();
String foosh = b.getString("SNARP");
EditText box1 = (EditText) findViewById(R.id.tBox1);
box1.setText(foosh);
Try sending a Bundle object when calling the new intent.
Intent i = new Intent(main.this, Result.class);
Bundle b = new Bundle();
b.putString("SNARP", "SCAN_RESULT")
i.putExtras(b);
Try getting string in the child Activity this way.
public class Result extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
Intent i = getIntent();
String foosh = i.getStringExtra("SNARP");

Categories

Resources