我正在开发专为Android 3.0开发的应用程序,我想在文本视图中使用一个按钮首先设置文本,然后将屏幕从screen1更改为screen2 ...
到目前为止,这是我的代码(导致崩溃):
Button mainNext = (Button) findViewById(R.id.Recipe1);
mainNext.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
final TextView mTextView = (TextView) findViewById(R.id.textView3);
mTextView.setText("Some Text");
Intent i = new Intent();
i.setClassName("com.bluStudios.Recipes4U.ics", "com.bluStudios.Recipes4U.ics.recipedisplayscreen");
startActivity(i);
}
});
任何想法??? 提前致谢:P
编辑:
第一屏的代码:
public class Recipe_Button_List extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recipe_button_list);
Button mainNext = (Button) findViewById(R.id.Recipe1);
mainNext.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
final TextView mTextView = (TextView) findViewById(R.id.textView3);
mTextView.setText("Some Text");
Intent i= new Intent(Recipe_Button_List.this, recipedisplayscreen.class);
startActivity(i);
}
});
}
}
第二屏幕代码:
public class recipedisplayscreen extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recipedisplayscreen);
ActionBar actionBar = getActionBar();
setTitle(R.string.title);
actionBar.setDisplayHomeAsUpEnabled(true);}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// App icon in action bar clicked; go home
Intent intent = new Intent(this, MainScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.recipe_menu1, menu);
return true;
}
}
答案 0 :(得分:2)
使用它:
Button mainNext = (Button) findViewById(R.id.Recipe1);
mainNext.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
final TextView mTextView = (TextView) findViewById(R.id.textView3);
mTextView.setText("Some Text");
Intent i= new Intent(YOUR_CURRENT_ACTIVTY_NAME.this, recipedisplayscreen.class);
///or you can use Intent i= new Intent(getBaseContext(),recipedisplayscreen.class);
startActivity(i);
}
});
并且必须recipedisplayscreen
AndroidManifest.xml
活动
答案 1 :(得分:1)
I think the your main layout do not contain any textView with id (R.id.textView3).
Also the right way is to declare the TextView outside the click listener..