【问题标题】:Can't set values in second activity无法在第二个活动中设置值
【发布时间】:2015-07-18 00:46:42
【问题描述】:

所以我正在制作一个简单的应用程序,它只从用户那里获取一些信息,即姓名、地址等,并将其放在下一个活动的文本视图中,但是当我输入值并移动到下一个活动时,什么都没有显示在 TextViews 中。

这是我的第一个活动

public class MainActivity extends Activity {



public String Name;
public String Age; 
public String Address;
public String City;
public String phoneno;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main);
EditText name = (EditText) findViewById(R.id.editText1);
 Name = name.getText().toString();
EditText Amge = (EditText) findViewById(R.id.agee); 
 Age = Amge.getText().toString();
EditText Address2 = (EditText) findViewById(R.id.address);
 Address = Address2.getText().toString();
EditText City2 = (EditText) findViewById(R.id.city);
City = City2.getText().toString();

EditText phone2 = (EditText) findViewById(R.id.phone);
phoneno = phone2.getText().toString();

final ImageView D = (ImageView) findViewById(R.id.done);
D.setOnClickListener(new OnClickListener(){

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent intent = new Intent(MainActivity.this, second.class);

        startActivity(intent);
    }
    });
}

@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 class second extends MainActivity {
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.second);
 TextView N1 = (TextView) findViewById(R.id.name1);
 N1.setText(Name);

 TextView A1 = (TextView) findViewById(R.id.age1);
 A1.setText(Age);

 TextView Ad1 = (TextView) findViewById(R.id.address1);
 Ad1.setText(Address);

 TextView C1 = (TextView) findViewById(R.id.city1);
 C1.setText(City);

 TextView P1 = (TextView) findViewById(R.id.phone1);
 P1.setText(phoneno);}  
public static void main(String[] args) {
    // TODO Auto-generated method stub

}}

这是我的主要活动的 xml

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="horizontal"
android:background="@drawable/bg"
tools:context="com.example.randomtests.MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_weight="1"
    android:textSize="21dp"
    android:textColor="#ffffff"
    android:text="Please enter the required info"
    android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="15dp"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/textView1"
    android:src="@drawable/bar" />

<EditText
    android:id="@+id/address"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/agee"
    android:layout_below="@+id/agee"
    android:layout_marginTop="14dp"
    android:ems="10"
    android:hint="@string/address"
    android:inputType="textPostalAddress"
    android:textColor="#000000" />

<EditText
    android:id="@+id/city"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/address"
    android:layout_below="@+id/address"
    android:layout_marginTop="17dp"
    android:ems="10"
    android:hint="@string/city"
    android:textColor="#000000" />


<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/imageView1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="33dp"
    android:ems="10"
    android:gravity="top"
    android:textColor="#000000"
    android:hint="@string/namu"
    android:inputType="textPersonName" />

<EditText
    android:id="@+id/agee"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText1"
    android:layout_below="@+id/editText1"
    android:layout_marginTop="14dp"
    android:ems="10"
    android:hint="@string/age"
    android:inputType="phone"
    android:textColor="#000000" />


<EditText
    android:id="@+id/phone"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/city"
    android:layout_below="@+id/city"
    android:layout_marginTop="15dp"
    android:ems="10"
    android:hint="@string/phone"
    android:inputType="phone"
    android:textColor="#000000" />

<ImageView
    android:id="@+id/done"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/textView1"
    android:layout_below="@+id/phone"
    android:layout_marginRight="14dp"
    android:layout_marginTop="12dp"
    android:src="@drawable/button" />

这是我的第二个活动的 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"
 android:background="@drawable/bg" >
<TextView
    android:id="@+id/info1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
     android:textColor="#ffffff"
     android:textSize="33dp"
    android:layout_centerHorizontal="true"
    android:text="@string/yourinfo"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/name1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/info1"
    android:textColor="#ffffff"
    android:layout_marginLeft="19dp"
    android:layout_marginTop="26dp"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/age1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/name1"
    android:layout_below="@+id/name1"
    android:textColor="#ffffff"
    android:layout_marginTop="20dp"
    android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
    android:id="@+id/address1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/age1"
    android:layout_below="@+id/age1"
    android:textColor="#ffffff"
    android:layout_marginTop="20dp"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/city1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/address1"
    android:layout_below="@+id/address1"
    android:textColor="#ffffff"
    android:layout_marginTop="20dp"
    android:textAppearance="?android:attr/textAppearanceMedium" />

 <TextView
    android:id="@+id/phone1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/city1"
    android:layout_below="@+id/city1"
    android:textColor="#ffffff"
    android:layout_marginTop="20dp"

    android:textAppearance="?android:attr/textAppearanceMedium" />

【问题讨论】:

  • 请学习一些oop课程...在基类实例中设置非静态字段不会在派生类的新实例中设置em ...
  • 我已经编程了一段时间,但离开它已经 2 年了。我曾经做过类似的事情来传递值,但我不记得了。请解释我在做什么错误的代码。提前致谢

标签: java android xml android-layout


【解决方案1】:

在你声明你的 Intent 之后,尝试用这个发送你的数据

//this use key-value
intent.putExtra("USER_NAME" , "your string");
intent.putExtra("USER_AGE" , "your string");

等等

和其他Activity尝试使用

 Bundle extra = getIntent().getExtras();
 String userName = extra.getString("USER_NAME");
 String userAge = extra.getString("USER_AGE");

【讨论】:

  • 还是不行。 TextView 仍然不显示任何内容
  • @user2435339 在 setText 之前使用 getExtras 吗?
【解决方案2】:

当你在这里声明你的Intent 时:

Intent intent = new Intent(MainActivity.this, second.class);

尝试使用 Extras 根据被点击的视图发送一些数据

例如:

intent.putExtra(Intent.EXTRA_INTENT, **Your String Here**);

在接收意图的活动中,使用类似的东西:

 String data  = intent.getStringExtra(Intent.EXTRA_INTENT);

从意图中获取数据

【讨论】:

    猜你喜欢
    • 2018-05-20
    • 1970-01-01
    • 2016-04-18
    • 2012-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多