如何附加到String并通过Android中的意图发送?

时间:2014-10-10 17:41:46

标签: java android string android-intent

我正在尝试向字符串添加值,然后将其发送到下一个活动。这是代码中唯一相关的部分。 adding()是按钮的ID,它将值附加到String msg并且sendMessage()完全发送消息。当我只发送一个虚拟值时,即使我一次添加1,2,3,4,5之类的内容而不是添加内容,也会传递消息。但是当我尝试添加,追加然后发送时,它给了我一个"不幸的是停止"错误。

我尝试了几次调整,但仍然是同样的错误。如何解决这个问题?

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);
}



编辑:活动代码 -

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);
    }
    }

2 个答案:

答案 0 :(得分:0)

不确定我是否正确理解您的问题

  1. 将您的findViewById移至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);
    }
    
  2. 你的添加方法中的
  3. 就像这样做

    public void adding(View view){
        msg = msg + "," + et1.getText().toString();
        final String showMessage = msg;
        tv1.setText(showMessage);
    
        et1.setText("");
    }
    
  4. 你的sendMessage中的
  5. public void sendMessage(View view){
        Intent intent=new Intent(this,Next.class);
        msg+=",";
        final String msgThatWillSend = msg;
        intent.putExtra("message", msgThatWillSend);
        startActivity(intent);
    }
    
  6. 还没有测试,但它应该是可行的

答案 1 :(得分:0)

试试这个:

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>