我遇到了一个关于如何在一个活动中显示从用户获取的数据以显示在另一个活动中的教程,我试图提出自己的版本(根据我非常有限的知识)。
这是第一个通过以下方式接受数据的活动的代码
package kk.screen;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class ScreenActivity extends Activity {
/** Called when the activity is first created. */
EditText inputName;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void btnclick(View view) {
inputName = (EditText)findViewById(R.id.name);
Intent nextscreen = new Intent(this, newscreen.class);
//Sending value to the next activity
nextscreen.putExtra("name",inputName.getText().toString());
startActivity(nextscreen);
}
}
这是下一个 Activity 的代码,它应该在单击 id 为 "btnclick" 的按钮时被激活:
package kk.screen;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class newscreen extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newscreen);
TextView txtName = (TextView) findViewById(R.id.txtName);
Intent i=getIntent();
//Receiving the data
String name = i.getStringExtra("name");
//Display received data
txtName.setText(name);
}
}
问题是,单击按钮后,应用程序崩溃了,我又回到了主屏幕。
可能是什么问题呢??我应该使用 OnClickListener() 吗?(这似乎是我的方法和教程方法之间的主要区别)