This question already has answers here:
How do I pass data between Activities in Android application?
(53 answers)
Closed 6 years ago.
I am trying to take text that was generated in one activity, that was concatenated, "MainActivity.java," and display it on screen using another activity, "DICS.java." I am having trouble figuring out how to do that.
MainActivity.java
package com.example.tristan.a1p1n1;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE = "com.example.tristan.a1p1n1.MESSAGE";
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
public void sendMessage(View view)
{
Intent intent = new Intent(this, DICS.class);
EditText editText = (EditText) findViewById(R.id.editText);
EditText editText2 = (EditText) findViewById(R.id.editText2);
String message = editText.getText().toString() + editText2.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
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="com.example.tristan.a1p1n1.MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Enter a String"
android:ems="10"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/editText" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Enter a String"
android:ems="10"
android:id="#+id/editText2"
android:layout_below="#+id/editText"
android:layout_alignRight="#+id/editText"
android:layout_alignEnd="#+id/editText"
android:layout_marginRight="11dp"
android:layout_marginEnd="11dp"
android:layout_marginTop="13dp" />
<Button
android:text="+"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="23dp"
android:id="#+id/button"
android:onClick="sendMessage" />
</RelativeLayout>
DICS.java
package com.example.tristan.a1p1n1;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
/**
* Created by Tristan on 2/11/2017.
*/
public class DICS extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dics);
}
}
dics.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="#+id/sendMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView2" />
</LinearLayout>
Intent intent = getIntent();
String dataGet= intent.getExtras().getString("EXTRA_MESSAGE");
Get the intent and then proceed to get the string from the intent
Intent intent = getIntent();
String stringFromIntent = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
Related
I am creating my first app which is when I press a button I simply move to the next page, but every time I press the button my app crashes (Ignore the username and password texts they don't do anything yet). I am trying to move from login to home page.
activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient"
tools:context=".Login">
<ImageView
android:id="#+id/imageView"
android:layout_width="120dp"
android:layout_height="150dp"
android:layout_marginTop="59dp"
app:srcCompat="#drawable/logo"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<EditText
android:id="#+id/username"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_below="#+id/imageView"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:background="#11000000"
android:drawableLeft="#drawable/ic_action_user"
android:ems="10"
android:hint="Username"
android:inputType="textPersonName"
android:textSize="16dp" />
<EditText
android:id="#+id/password"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_below="#id/username"
android:layout_alignStart="#id/username"
android:layout_alignLeft="#id/username"
android:layout_marginTop="38dp"
android:background="#11000000"
android:drawableLeft="#drawable/ic_action_pass"
android:ems="10"
android:hint="Password"
android:inputType="textPersonName"
android:textSize="16sp" />
<Button
android:id="#+id/login"
android:layout_width="321dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="207dp"
android:background="#color/colorAccent"
android:onClick="loginButton"
android:text="Login" />
<TextView
android:id="#+id/register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="139dp"
android:text="Register Here"
android:textColor="#fff"
android:textSize="14sp" />
<TextView
android:id="#+id/attempts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="74dp"
android:text="Incorrect Attempts: "
android:textColor="#fff"
android:textSize="14sp" />
</RelativeLayout>
Login.java
package com.example.chorehelpers;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
public class Login extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
public void onClick (View v){
switch (v.getId()){
case R.id.login:
loginButton(v);
break;
}
}
public void loginButton(View v) {
Intent i = new Intent (Login.this, HomePage.class);
startActivity(i);
}
}
activity_home_page.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient"
tools:context=".HomePage">
</RelativeLayout>
HomePage.java
package com.example.chorehelpers;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class HomePage extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
}
}
Please Help.
why are u using this method
public void onClick (View v){
switch (v.getId()){
case R.id.login:
loginButton(v);
break;
}
remove it and rerun the app , Hope it woks
**Please try to update Login.java class like this,**
package com.example.chorehelpers;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
public class Login extends AppCompatActivity {
private Button loginButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
loginButton = (Button) findViewById(R.id.login);
loginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent x = new Intent(LoginActivity.this, HomePageActivity.class);
startActivity(x);
}
});
}
}
I've read many answers and I cannot find where I'm going wrong with my code. I want to have 2 buttons on my first activity. When I click button1 or button2 I want text held within that specific string resource to appear on the textview of activity two. I am able to populate one of the strings, but it will not separate them depending on which button I click.
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="#+id/button_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="162dp"
android:layout_marginTop="150dp"
android:layout_marginEnd="161dp"
android:onClick="launchSecondActivity"
android:text="Passage 1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="162dp"
android:layout_marginTop="244dp"
android:layout_marginEnd="161dp"
android:onClick="launchSecondActivity"
android:text="Passage 2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="162dp"
android:layout_marginTop="368dp"
android:layout_marginEnd="161dp"
android:onClick="launchSecondActivity"
android:text="Passage 3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/button_main_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="180dp"
android:layout_marginTop="114dp"
android:layout_marginEnd="172dp"
android:layout_marginBottom="400dp"
android:text="#string/button_main_passage"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button_main" />
<TextView
android:id="#+id/button_second_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="180dp"
android:layout_marginTop="114dp"
android:layout_marginEnd="172dp"
android:layout_marginBottom="400dp"
android:text="#string/button_second_passage"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button_main" />
<TextView
android:id="#+id/button_third_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="180dp"
android:layout_marginTop="114dp"
android:layout_marginEnd="172dp"
android:layout_marginBottom="400dp"
android:text="#string/button_third_passage"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button_main" />
</androidx.constraintlayout.widget.ConstraintLayout>
Activity_passage.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".PassageActivity">
<ScrollView
android:id="#+id/scroll_passage"
android:layout_width="409dp"
android:layout_height="729dp"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/text_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView
android:id="#+id/text_message_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
strings.xml
<resources>
<string name="app_name">ThreeButtons</string>
<string name="activity2_name">Passage</string>
<string name="button_main_passage">First Passage First Passage First Passage First
Passage</string>
<string name="button_second_passage">Second Passage Second Passage Second Passage Second
Passage</string>
</resources>
MainActivity.java
package com.dev20.threebuttons;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView mButtonMainText;
private TextView mButtonSecondText;
private static final String LOG_TAG = MainActivity.class.getSimpleName();
public static final String EXTRA_MESSAGE = "com.example.android.threebuttons.extra.MESSAGE";
public static final String EXTRA_MESSAGE2 = "com.example.android.threebuttons.extra.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButtonMainText = findViewById(R.id.button_main_text);
mButtonSecondText = findViewById(R.id.button_second_text);
}
public void launchSecondActivity(View view) {
Log.d(LOG_TAG, "Button Clicked");
Intent intent = new Intent(this, PassageActivity.class);
String message = mButtonMainText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
Intent intent2 = new Intent(this, PassageActivity.class);
String message2 = mButtonSecondText.getText().toString();
intent2.putExtra(EXTRA_MESSAGE2, message2);
startActivity(intent);
}
}
PassageActivity.java
package com.dev20.threebuttons;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class PassageActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_passage);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = findViewById(R.id.text_message);
textView.setText(message);
Intent intent2 = getIntent();
String message2 = intent2.getStringExtra(MainActivity.EXTRA_MESSAGE2);
TextView textView2 = findViewById(R.id.text_message);
textView2.setText(message2);
}
}
I think You have to make two separate functions. Why You add 2 intents in one function? You only want to make one intent and pass one string. Also, you can add tag to button 1 = But1 and button 2 = But2. Just add to XML layout android:tag="But1" to both buttons.
'
public void launchSecondActivity(View view) {
String arg = null;
if (view.getTag().equals("But1"))
{
arg = mButtonMainText.getText().toString();
}
else if (view.getTag().equals("But2"))
{
arg = mButtonSecondText.getText().toString();
}
Intent intent = new Intent(this, PassageActivity.class);
intent.putExtra(EXTRA_MESSAGE, arg);
startActivity(intent);
}
I want to get the username from edit text and display it in the second Activity in TextView id-textView2. When I press enter after writing the name and click start button it goes to the second activity but the text is not being displayed . I tried executing other actions like startActivity() inside onEditorAction method it works after i press enter, but this code is not working
String name = nameText.getText().toString();
Intent p1 = new Intent(MainActivity.this, qPage1.class);
p1.putExtra("user_name",name);
Is it because i declared two times intent, inside onEditorAction() and inside starQuizz method in Activity A. i did this because of scope issues.
Activity A
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.content.Intent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.view.KeyEvent;
import android.widget.TextView.OnEditorActionListener;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText nameText = findViewById(R.id.nameText);
nameText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView nameText, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEND || event.getKeyCode() == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
String name = nameText.getText().toString();
Intent p1 = new Intent(MainActivity.this, qPage1.class);
p1.putExtra("user_name",name);
handled = true;
}
return handled;
}
});
}
//OnClick of a Start button
public void startQuizz(View view){
Intent p1 = new Intent(this, qPage1.class);
startActivity(p1);
}
}
Activity A xml layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/beautifulcolorgradientsbackgrounds091eternalconstance"
tools:context=".MainActivity">
<EditText
android:id="#+id/nameText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="181dp"
android:width="300dp"
android:hint="#string/name_input"
android:inputType="text"
android:imeOptions="actionSend"
android:imeActionId="10"/>
<Button
android:id="#+id/start_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="182dp"
android:onClick="startQuizz"
android:text="START" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="45dp"
android:fontFamily="#font/changa_one"
android:text="Are you up for the challenge?"
android:textAlignment="center"
android:textAllCaps="false"
android:textSize="30sp" />
</RelativeLayout>
Activity 2
package com.guesstasif.guesswhat;
import android.content.Intent;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.view.View;
import android.widget.TextView;
public class qPage1 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_q_page1);
final ProgressBar p1progress = findViewById(R.id.pg1progressBar);
Runnable r = new Runnable() {
#Override
public void run() {
int progressStatus = 0;
while (progressStatus<200){
p1progress.incrementProgressBy(1);
android.os.SystemClock.sleep(50);
progressStatus++;
}
RadioButton q1radiobutton3 = findViewById(R.id.q1radioButton3);
RadioButton q2radiobutton1 = findViewById(R.id.q2radioButton1);
Intent p2 = new Intent(qPage1.this, qPage2.class);
//name==============================================================
String name = getIntent().getStringExtra("user_name");
TextView textView2= findViewById(R.id.textView2);
textView2.setText(name);
//==================================================================
if(q1radiobutton3.isChecked() && q2radiobutton1.isChecked())
{
p2.putExtra("intVariableName", 2);
}
else if (q1radiobutton3.isChecked() || q2radiobutton1.isChecked())
{
p2.putExtra("intVariableName", 1);
}
startActivity(p2);
}
};
Thread progressThread =new Thread(r);
progressThread.start();
}
}
Activity 2 xml layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".qPage1">
<TextView
android:id="#+id/q1textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="Q1.What is the name of the 7th planet of ou Solar system?"
android:textAlignment="center"
android:textSize="24sp" />
<RadioGroup
android:id="#+id/q1radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/q1textView"
android:layout_centerHorizontal="true"
android:orientation="horizontal">
<RadioButton
android:id="#+id/q1radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Jupiter" />
<RadioButton
android:id="#+id/q1radioButton2"
android:layout_width="84dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Saturn" />
<RadioButton
android:id="#+id/q1radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Uranus" />
<RadioButton
android:id="#+id/q1radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Neptune" />
</RadioGroup>
//second Question//
<TextView
android:id="#+id/q2textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="173dp"
android:text="Q2.What is the name of the largest tree in the world?"
android:textAlignment="center"
android:textSize="24sp" />
<RadioGroup
android:id="#+id/q2radioGroup"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentStart="true"
android:layout_below="#+id/q2textView">
<RadioButton
android:id="#+id/q2radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="General Sherman" />
<RadioButton
android:id="#+id/q2radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Sequoia sempervirens" />
<RadioButton
android:id="#+id/q2radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Centurion" />
<RadioButton
android:id="#+id/q2radioButton4"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_weight="1"
android:text="Coast redwood" />
</RadioGroup>
<ProgressBar
android:id="#+id/pg1progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="33dp"
android:max="200" />
<TextView
android:id="#+id/scoreView"
android:layout_width="170dp"
android:layout_height="41dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="89dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="137dp"
android:textAlignment="center" />
</RelativeLayout>
The reason is you start a new Intent in here:
public void startQuizz(View view){
Intent p1 = new Intent(this, qPage1.class);
startActivity(p1);
}
Which does not pass the string and just starts a new Intent-Activity.
Also here:
Intent p1 = new Intent(MainActivity.this, qPage1.class);
p1.putExtra("user_name",name);
You missed to start Intent so:
Intent p1 = new Intent(MainActivity.this, qPage1.class);
p1.putExtra("user_name",name);
startActivity(p1);
And in the quiz method, start another Intent:
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
And finally, in the second Activity:
Bundle bundle = getIntent().getExtras();
String name = bundle.getString("user_name");
Also, you were using Thread instead of runOnUIThread(); and that might cause some UI effects.
Look at the startQuizz method - seems like you are not setting the name as an extra to the intent!
You should use a single function that starts the next activity both in the editor action and the button click listener.
It seems you are setting text of a TextView in a background thread.
The UI cannot be altered in a background thread.
Try wrapping the setText() part in runOnUiThread().
Check this out: How do we use runOnUiThread in Android?
I'll answer the most obvious question here. It seems there's a lot of code work being done, here also.
EditText edittext = (EditText) findViewById(R.id.something);
You must, and I cannot stress this enough, as of Level 30, still, you have to cast it as an EditText. In fact, you have to with everything, essentially. Java doesn't like structural propagating without being handed the stalk, not the beans (seeds). So, please one last thing:
String string = edittext.getText().toString()
That works every time in this combination!
This question already has answers here:
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
Closed 4 years ago.
I am trying to get an input from user and then covert it into double and divide it by 1000 and then set the value in a textview but i while running the app, the app closes and says Unfortunately the app has stopped.
Here's my MainActivity.java file :
package com.blogspot.techtutorialsajen.androiddevelopmentpractice;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity{
private Button btn1;
private EditText get;
private TextView result;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.btn1);
result = (TextView)findViewById(R.id.result);
get = (EditText)findViewById(R.id.get);
final double value = Double.parseDouble(get.getText().toString()) / 1000;
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
result.setText(String.valueOf(value));
}
});
}
}
And My activity_main.xml file :
?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<EditText
android:id="#+id/get"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:hint="#string/enter_a_number"
android:layout_marginTop="48dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:id="#+id/btn1"
android:text="#string/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/result"
android:layout_centerHorizontal="true"
android:layout_marginBottom="28dp" />
<TextView
android:id="#+id/result"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="20pt"
android:text="0"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Well, Your app is crashes because of java.lang.RuntimeException:java.lang.NumberFormatException: Invalid double: ""
that means you are trying to convert empty string to Double.
So, Because the EditText is empty your app is crashes and I have re-write your code so that you can copy-paste.
Here is your MainActivity.Java
package com.blogspot.techtutorialsajen.androiddevelopmentpractice;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity{
private Button btn1;
private EditText get;
private TextView result;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.btn1);
result = (TextView)findViewById(R.id.result);
get = (EditText)findViewById(R.id.get);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (get.getText().toString().isEmpty()) {
Toast.makeText(MainActivity .this, "Please input something", Toast.LENGTH_LONG).show();
} else {
final double value = Double.parseDouble(get.getText().toString()) / 1000;
result.setText(String.valueOf(value));
}
}
});
}
}
and here is your activity_main.XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<EditText
android:id="#+id/get"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:hint="#string/enter_a_number"
android:layout_marginTop="48dp"
android:inputType="number"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:id="#+id/btn1"
android:text="#string/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/result"
android:layout_centerHorizontal="true"
android:layout_marginBottom="28dp" />
<TextView
android:id="#+id/result"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="20pt"
android:text="0"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
I have added android:inputType="number" property to your EditText so that, now on, it will only take Number from the user.
From your xml file and activity code, it clearly says your edittext is empty while launching app.
And you are using this code in oncreate()
final double value = Double.parseDouble(get.getText().toString()) / 1000;
which throws java.lang.NumberFormatException: empty String
You can change your code like,
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(!get.getText().toString().isEmpty)
final double value = Double.parseDouble(get.getText().toString()) / 1000;
result.setText(String.valueOf(value));
}
});
}
I have a MainActivity with a ListView and an add Button. When the user clicks the add Button it takes you to another activity the CreateActivity for you to enter in data or strings into the editText boxes. After that you click the create Button and it adds the strings to the ListView it shows up, but every time I try to add another item to the ListView it just overrides the first item and doesn't add on to the ListView.
MainActivity.java with listView and add button
package com.example.brian.inventoryapp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static android.R.layout.simple_list_item_1;
public class MainActivity extends AppCompatActivity {
Button addButton;
ListView itemListView;
public static ArrayList<String> arrayList;
public static ArrayAdapter arrayAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle editTextData = getIntent().getExtras();
if(editTextData != null){
itemListView = (ListView)findViewById(R.id.itemListView);
String data = editTextData.getString("data");
arrayList = new ArrayList<String>();
arrayList.add(data);
arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, arrayList);
itemListView.setAdapter(arrayAdapter);
if(data != ""){
arrayAdapter.notifyDataSetChanged();
}
}
addButton = (Button) findViewById(R.id.addButton);
addButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, CreateActivity.class);
startActivity(intent);
}
});
}
}
activity_main xml with listView and add button
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
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="com.example.brian.inventoryapp.MainActivity"
android:weightSum="1">
<Button
android:text="Add"
android:layout_width="wrap_content"
android:layout_height="58dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/addButton" />
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/itemListView"
/>
</RelativeLayout>
CreateActivity.java with four editText boxes and create button
package com.example.brian.inventoryapp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import static com.example.brian.inventoryapp.R.id.editTextItem;
public class CreateActivity extends AppCompatActivity {
EditText editTextItem;
EditText editTextModel;
EditText editTextSerial;
EditText editTextID;
Button createButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create);
getIntent();
editTextItem = (EditText)findViewById(R.id.editTextItem);
editTextModel = (EditText)findViewById(R.id.editTextModel);
editTextSerial = (EditText)findViewById(R.id.editTextSerial);
editTextID = (EditText)findViewById(R.id.editTextID);
createButton = (Button) findViewById(R.id.createButton);
createButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
String item = "";
item = editTextItem.getText().toString().trim() +
editTextModel.getText().toString().trim() +
editTextSerial.getText().toString().trim() +
editTextID.getText().toString().trim();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("data", item);
startActivity(intent);
}
});
}
}
activity_create xml with the four editText boxes and the create button
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_create"
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="com.example.brian.inventoryapp.CreateActivity">
<TextView
android:text="Item Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/textView" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/editTextItem" />
<TextView
android:text="Model Number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:id="#+id/textView2"
android:layout_below="#+id/editTextItem"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_below="#+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/editTextModel" />
<TextView
android:text="Serial Number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editTextModel"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="18dp"
android:id="#+id/textView3" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_below="#+id/textView3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/editTextSerial" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#mipmap/ic_launcher"
android:layout_below="#+id/editTextID"
android:layout_centerHorizontal="true"
android:layout_marginTop="64dp"
android:id="#+id/imageButton" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/editTextID"
android:layout_below="#+id/editTextSerial"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="18dp" />
<TextView
android:text="ID Number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView4"
android:layout_below="#+id/editTextSerial"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:text="Create"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/createButton"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
every time you restart the MainActivity, it is be created, the onCreate method was executed .one method you can use the comment method:use startactivityforResult;
you can read this question how to manage startactivityforResult.the second method is to set MainActivity model to SingleInstance.
You are starting MainActivity over and over again, clearing all data previously loaded.
Try this solution
Main activity:
package com.example.brian.inventoryapp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static android.R.layout.simple_list_item_1;
public class MainActivity extends AppCompatActivity {
public static final int CREATE_REQUEST = 1;
private Button addButton;
private ListView itemListView;
private ArrayList<String> arrayList;
private ArrayAdapter<String> arrayAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
itemListView = (ListView) findViewById(R.id.itemListView);
addButton = (Button) findViewById(R.id.addButton);
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList);
itemListView.setAdapter(arrayAdapter);
addButton = (Button) findViewById(R.id.addButton);
addButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, CreateActivity.class);
startActivityForResult(intent, CREATE_REQUEST);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CREATE_REQUEST) {
if (resultCode == RESULT_OK) {
String item = data.getStringExtra("data");
arrayList.add(item);
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList);
itemListView.setAdapter(arrayAdapter);
}
}
}
}
In your CreateActivity change createButton listener to this one:
createButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
String item = "";
item = editTextItem.getText().toString().trim() +
editTextModel.getText().toString().trim() +
editTextSerial.getText().toString().trim() +
editTextID.getText().toString().trim();
Intent intent = new Intent();
intent.putExtra("data", item);
setResult(RESULT_OK, intent);
finish();
}
});
Yes it will assume as if the MainActivity is started for the first time so you should use startActivityForResult() method instead of startActivity().This will continue your MainActivity and do not override your list View.