我正在尝试传递一个字符串作为额外的,但由于某种原因它只是没有经历。这是我的代码:
public void setChordMajor(View v){
chordType = "major";
Intent intent = new Intent(this, ChordView.class);
intent.putExtra("CHORD", chordType);
startActivity(intent);
}
CHORD定义为:
public final static String CHORD = "com.example.musiciansreference.EXTRA_CHORD";
接下来的活动会收到它:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chord_view);
Intent intent = getIntent();
chordType = intent.getStringExtra(Chords.CHORD);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(chordType);
setContentView(textView);
}
只显示一个空白屏幕。不知怎的,字符串没有被发送!
编辑:我认为问题在于第二个活动显示和弦的能力......这里是完整的代码:import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class ChordView extends ActionBarActivity {
String chordType = "null";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chord_view);
Intent intent = getIntent();
chordType = intent.getStringExtra(Chords.CHORD);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(chordType);
setContentView(textView);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.chord_view, 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);
}
}
答案 0 :(得分:3)
intent.putExtra(Chords.CHORD,chordType);而不是intent.putExtra(“CHORD”,chordType);
答案 1 :(得分:0)
以这种方式试试 在xml布局中添加TextView元素,并从Oncreate方法中的ChordView引用它 TextView tv =(TextView)findviewbyId(R.id.tv); Tv.setText(chordType);看看空白屏幕是否仍然存在