为什么我的Android应用程序停止时,我设置TextView的文本值

时间:2014-05-06 10:33:13

标签: android android-layout android-edittext textview

我有两种布局。第一个:

<LinearLayout       xmlns:android="http://schemas.android.com/apk/res/android"
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                <EditText
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:id="@+id/edit_text_val">
                </EditText>
                <Button
                        android:id="@+id/continue"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="Continue"
                        android:onClick="newConfirm"/>
</LinearLayout>

和第二个:

<LinearLayout       xmlns:android="http://schemas.android.com/apk/res/android"
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:id="@+id/text_view_val">
                </TextView>
</LinearLayout>

当我单击第一个布局的按钮时,我转到第二个布局,并希望设置为TextView的文本值 在第一个布局的EditText中输入的字符串。我在newConfirm方法中这样做:

public void newConfirm(View view){
        EditText et = (EditText)findViewById(R.id.edit_text_val);
        TextView tv = (TextView)findViewById(R.id.text_view_val);
        tv.setText(et.getText().toString());
    } 

但是当我点击按钮时我的应用程序停止工作...... 不幸的是,MobileApp已停止

任何想法??

4 个答案:

答案 0 :(得分:1)

这不起作用,因为文本视图采用不同的布局。您需要按意图将数据传递给下一个活动,然后在那里显示文本。请参阅此example

答案 1 :(得分:1)

您无法直接从第一个活动设置EditText的值。

您应该将EditTextIntent的值从第一个活动传递到第二个活动,然后在第二个活动中从意图中获取并设置为TextView

试试如下:

public void newConfirm(View view){
        EditText et = (EditText)findViewById(R.id.edit_text_val);

         Intent intent =new Intent(FistLayout.this,Secondlayout.class);
           intent.putExtra("value",et.getText().toString());
           startActivity(intent);
    } 

在您的第二个活动中,在onCreate

中写下以下代码
    TextView tv = (TextView)findViewById(R.id.text_view_val);
    String value=getIntent().getString("value");
    tv.setText(value);

答案 2 :(得分:0)

您的EditText和TextView驻留在两个不同的布局中。那么如何在单个类中使用findViewbyId?在第二个布局中通过Intent传递Edittext值:

 Intent i=new Intent(FistLayout.this,Secondlayout.class);
  i.putExtra("key",ed.getText().toString());
  startActivity(i);

在第二个布局中:

 String str=getIntent().getExtras.getString("key");
  textView.setText(str);

答案 3 :(得分:0)

非常简单,只需在你想要下一个活动的onclick功能下定义它

在OnCREATE

下定义Onclick
  YourButton.setOnClickListener(new View.OnClickListener() 
    {
            @Override
            public void onClick(View v) 
            {
                 next();
            }

        });

然后在您的活动中的任何位置编写此功能

    public void next()
   {

  String strMessage = YourTEXT.getText().toString());

// so on you can directly set values
// you have to just set values in string                      

  Intent i = new Intent(getApplicationContext(), nextactivity.class)   
  i.putExtra("new_variable_name",strMessage);


  startActivity(i);  

}

然后在您的下一个活动中通过

检索它
Bundle extras = getIntent().getExtras();
String var="";

      if (extras != null)
      {

        String value= extras.getString("new_variable_name");       
        value= var;
        yourtext2.setText(var);

      }