I am trying to add values to a string and then send it to the next activity. Here is the only relevant part of the code. adding()
is the id of the button which appends values to the String msg
and sendMessage()
sends the msg altogether. When I just send a dummy value, the message is being passed, even when I add something like 1,2,3,4,5
at once instead of adding, it goes. But when I try to add, append and then send, it gives me an "Unfortunately stopped" error.
I tried couple of tweaks, but still the same error. How to fix this?
public void adding(View view){
EditText et1=(EditText) findViewById(R.id.editText1);
msg=msg+","+et1.getText().toString();
TextView tv1=(TextView) findViewById(R.id.textView1);
tv1.setText(msg);
et1.setText("");
}
public void sendMessage(View view){
Intent intent=new Intent(this,Next.class);
msg+=",";
intent.putExtra("message", msg);
startActivity(intent);
}
EDIT: Activity Code-
package com.example.newlist;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
String msg="";
int count=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void adding(View view){
EditText et1=(EditText) findViewById(R.id.editText1);
msg=msg+","+et1.getText().toString();
TextView tv1=(TextView) findViewById(R.id.textView1);
tv1.setText(msg);
et1.setText("");
}
public void sendMessage(View view){
Intent intent=new Intent(this,Next.class);
msg+=",";
intent.putExtra("message", msg);
startActivity(intent);
}
}
Not sure I understand your question correctly
move your findViewById to onCreate
EditText et1; TextView tv1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et1=(EditText) findViewById(R.id.editText1); tv1=(TextView) findViewById(R.id.textView1); }
in your adding method do it like this
public void adding(View view){ msg = msg + "," + et1.getText().toString(); final String showMessage = msg; tv1.setText(showMessage); et1.setText(""); }
in your sendMessage
public void sendMessage(View view){ Intent intent=new Intent(this,Next.class); msg+=","; final String msgThatWillSend = msg; intent.putExtra("message", msgThatWillSend); startActivity(intent); }
didn't test yet, but it should be work
try this :
ActivityA.java
public class ActivityA extends Activity{
StringBuilder msg=new StringBuilder();
private EditText et1;
private TextView tv1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
et1=(EditText)findViewById(R.id.et1);
tv1=(TextView)findViewById(R.id.tv1);
}
public void adding(View view){
msg.append(" "+et1.getText().toString());
tv1.setText(msg);
et1.setText("");
}
public void sendMessage(View view){
Intent intent=new Intent(this,Next.class);
intent.putExtra("message", msg.toString());
startActivity(intent);
}
}
Next.java
public class Next extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.next);
TextView tv = (TextView)findViewById(R.id.textView1);
tv.setText(getIntent().getStringExtra("message"));
}
}
test.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text=""
android:textSize="12sp" />
<EditText
android:id="@+id/et1"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:text="" />
<Button
android:id="@+id/btnadd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="adding"
android:text="adding"
/>
<Button
android:id="@+id/btnsent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sendMessage"
android:text="SendMessage" />
</LinearLayout>
next.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="msg"
android:textSize="25sp" />
</LinearLayout>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.