Intent : To start an activity, unable to start the second activity - java

The application aims to write a message at the first activity, and after clicking on a button, this message appears in the second activity.
But After installing the application on smartphone, when i click on the button, a message appears saying " application is stopped". Help me please." i USED ECLIPSE for developping this application"
The first activity code:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void login (View view) {
EditText user = (EditText) findViewById(R.id.user);
EditText password = (EditText) findViewById(R.id.password);
Intent myintent= new Intent (getApplicationContext(),Main2Activity.class); //permet de basculer de cette interface à la 2éme aprés avoir éxécuter "startActivity(myintent)";
Bundle b=new Bundle();
b.putString("username", user.getText().toString());
b.putString("password", password.getText().toString());
myintent.putExtras(b);
startActivity(myintent);
}
The second activity:
public class Main2Activity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
TextView mes=(TextView)findViewById(R.id.mes);
Bundle b=getIntent().getExtras();
string user=(string) b.get("username");
string pass=(string) b.get("password");
mes.setText("user:"+user +"password:"+pass);
}

Try a simpler way to pass data as a String,without using bundle.
MainActivity.java
Intent i= new Intent(MainActivity.this,Main2Activity .class);
i.putExtra("username",user.getText().toString());
i.putExtra("password",password.getText().toString());
startActivity(i);
and in Main2Activity .java
String name=getIntent().getExtras().getString("username");
txtView.setText(name);
String pass=getIntent().getExtras().getString("password");
txtView2.setText(pass);

Change this line
Intent myintent= new Intent (getApplicationContext(), Main2Activity.class)
to
Intent myintent= new Intent (this, Main2Activity.class)
And see if it solves the problem :)

On Your Second Activity(Main2Activity) File:
The Line After
Bundle b=getIntent().getExtras();
you have written (string) instead of (String).
So please change it...if u have written it mistakenly.
and #AugustoCarmo answer is also correct.
Try That Also.

Related

issue with Java Intent

i try to make an intent which is used to go to a 2nd activity. however when i try to click on it, it makes my application crash.
here's the code:
public class MainActivity extends AppCompatActivity {
private Button mButtonCommande;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButtonCommande=findViewById(R.id.buttonAccessCommande);
mButtonCommande.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openCommandeActivity();
}
});
}
public void openCommandeActivity() {//cette méthode me sert à envoyer vers l'activité commande
Intent intent = new Intent(this, CommandeActivity.class );
startActivity(intent);
}
idk where it keeps crashing and i need help.
Make sure you declared the target activity in AndroidManifest.xml.
In your AndroidManifest.xml, add the following inside the <application> element:
<activity android:name=".CommandeActivity"/>

Why does Android Studio keep asking for a "Expression Expected"?

Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSighin = (Button) findViewById(R.id.btnSignIn);
btnSignUp = (Button) findViewById(R.id.btnSignUp);
btnSignUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent j = new Intent(MainActivity.this, SignUp.class);
startActivity(SignUp);
}
});
btnSighin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View view) {
Intent k = new Intent(MainActivity.this, SignIn.class);
startActivity(SignIn);
}
});
}
I am working on an app that allows customers to book appointments and in order to do so, they need to sign up or sign in. I have activities made for both of them and I can't run it without getting the "Expression Expected" and I'm new to Android Studio and have no idea what to do. Any help?
You are missing some information. Anyway, i see that you have mistakes in lines
startActivity(SignUp);
and
startActivity(SignIn);
The correct way should be startActivity(j) and startActivity(k) respectively
You have to pass the intent object to the start activity method not the signup or signin class
So it should be startActivity(i) and startActivity(j)

Passing textview value across activities

I am trying to pass a value to a text view into a different activity but only when the button in the activity is clicked.
Here is the activity I am trying to set the text...
public static Button yes;
public static final String TEST_KEY = "test";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question19);
yes = (Button) findViewById(R.id.finalYes);
yes.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent myIntent = new Intent(v.getContext(), Question13Activity.class);
startActivity(myIntent);
Intent i = new Intent(Question12Activity.this, HighRiskActivity.class);
i.putExtra(TEST_KEY, "SOME STRINGSSS");
}
});
Here is the HighRiskActivity that is the destination for updating the textview's value...
TextView t;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.high_risk);
t = (TextView) findViewById(R.id.abusedOrNah);
Bundle extras = getIntent().getExtras();
if(extras != null)
{
String value = extras.getString(TEST_KEY);
t.setText(value);
}
My problem is that no text is printing at the desired activity, am I passing in the wrong data?? Any help would be amazing, this is the last step to the app that I am creating :D
UPDATE
I do not want the Question12Activity to be directed to the HighRiskActivity when the button is clicked. I want it to go to the next activity but still be able to pass the text onto the HighRiskActivity once the button is clicked. Sorry for the confusion, hopefully that makes more sense :)
Try to pass data this way:
Intent i = new Intent(this, SecondActivity.class);
Bundle bundle = new Bundle();
//Add your data to bundle
bundle.putString(“test”, "Data you want to pass");
//Add the bundle to the intent
i.putExtras(bundle);
startActivity(i);
Now in your SecondActivity class you retrieve data as below:
Bundle bundle = getIntent().getExtras();
//get the data…
String stuff = bundle.getString(“test”);
I don't know what exactly you are trying to achieve by starting the first activity with the statements below
Intent myIntent = new Intent(v.getContext(), Question13Activity.class);
startActivity(myIntent);
But if the Activity you want to start is the HighRiskActivity then the following should fix your issue:
Get rid of the statements related to the Question13Activity mentioned above.
and add the call to start the actual HighRiskActivity like below:
Intent i = new Intent(Question12Activity.this, HighRiskActivity.class);
i.putExtra(TEST_KEY, "SOME STRINGSSS");
startActivity(i); //This line is important to start the new Activity.
So, I guess your major issue here is you didn't start the activity with the startActivity(i); call.
In Activity A you are using the internal intent bundle that is not public to you and private to the intent. In the Activity B you are asking the intent to instead look for a bundle that you provided yourself.
Try something like this in your Activity A
Intent intent = new Intent(context, YourActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("EXTRA1", "string data");
intent.putExtra(TEST_KEY, bundle);
startIntent(intent);
Here is a good writeup on the subject (see answer) Advantages of using Bundle instead of direct Intent putExtra() in Android
You dont need to define two intents in order to connect two Activities. Put the following inside onClick method
Intent i = new Intent(Question12Activity.this, HighRiskActivity.class);
i.putExtra(TEST_KEY, "SOME STRINGSSS");
startActivity(i);

Setting TextView in Another Class

I have researched many problems like mine on the internet and none seem to be doing what I'm trying to do. What I am trying to do is get a textview that is currently blank in my high_risk.xml file, and make it have text based on if a button is clicked in another class. Here is what I have so far...
Question13Activity(if the yes button is clicked I want to be able to set the text in the HighRisk Activity)
yes = (Button) findViewById(R.id.finalYes);
yes.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent intent = getIntent();
String text = intent.getStringExtra("New Text");
t = (TextView) findViewById(R.id.abusedOrNah);
t.setText(text);
Intent myIntent = new Intent(v.getContext(), Question13Activity.class);
startActivity(myIntent);
}
});
This is how I have the static variable t defined in my highrisk activity class...
HighRiskActivity(this is where I want the text to be set and displayed)
public static TextView t;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.high_risk);
Intent myIntent = new Intent(HighRiskActivity.this, Question12Activity.class);
myIntent.putExtra("Text", "New Text");
startActivity(myIntent);
Every time I try and access the contents of t in another class and change it's text, it always returns null. Any way I can fix this from happening? Any help would be greatly appreciated :)
You can use Bounds, to get the the data to the new Activity and set the text from there:
Intent myIntent = new Intent(v.getContext(), Question13Activity.class);
myIntent.putExtra("<KEY-NAME>", "<TEXT>");
startActivity(myIntent);
And in the Question13Activity.class:
Intent intent = getIntent();
String text= intent.getStringExtra("<KEY-NAME>");
t = (TextView) findViewById(R.id.abusedOrNah);
t.setText(text);
UPDATE:
Use it so:
HighRiskActivity.class:
yes = (Button) findViewById(R.id.finalYes);
yes.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent myIntent = new Intent(v.getContext(), Question13Activity.class);
myIntent.putExtra("TestKey", "My new Text");
startActivity(myIntent);
}
});
Question13Activity.class:
TextView t;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.high_risk);
Intent intent = getIntent();
String text= intent.getStringExtra("TestKey");
t = (TextView) findViewById(R.id.abusedOrNah);
t.setText(text);
In order to setText() from another class (for example, from your Custom class), you need to specify the activity on which you'd like to do that.
Unless a class does have a reference of your context, you cannot call 'findViewById()'
To do that, you use Context as a parameter for custom class constructor.
Just after you have set the context will you be able to call
TextView txtView = (TextView) ((Activity)context).findViewById(R.id.text);
txtView.setText("foo");
}

Starting new activity in the Android SDK

So I have this code in my main activity to start a new one:
public class MainActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.GoButton).setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent myIntent = new Intent(MainActivity.this, NewActivity.class);
MainActivity.this.startActivity(myIntent);
//finish();
}
});
}
}
My new activity extends ListActivity and when I call this code by pressing the button it crashes the application. However if I make the MainActivity extend ListActivity then it works great (although I have to replace the button with a List!). Does anyone know why this happens, and how can I make it work using the code above?
Thanks
Have you added the manifest entry

Categories