我正在尝试从列表视图中获取一个项目并将其传递给textview中的新活动,我最终会对其进行一些修改。
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
makeListView();
getClick();
}
private void makeListView() {
// TODO Auto-generated method stub
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.items, input);
ListView lista = (ListView) findViewById(R.id.listView1);
lista.setAdapter(adapter);
}
private void getClick() {
// TODO Auto-generated method stub
ListView lista = (ListView) findViewById(R.id.listView1);
lista.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View viewClicked, int pos, long id) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("test", input[pos].toString());
startActivity(intent);
}
});
}
SecondActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
Bundle extras = getIntent().getExtras();
String myString = extras.getString("test");
TextView text = (TextView) findViewById(R.id.textView1);
text.setText(myString);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
我收到错误:
java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.shoplist / com.example.shoplist.Second * Activity}:java.lang.IllegalArgumentException:找不到片段PlaceholderFragment的id 0x7f080000的视图
我该怎么办?