ArrayList<String> arrayList = new ArrayList<>(); arrayList.add("one"); arrayList.add("two"); listView = (ListView)findViewById(R.id.list_view); ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, arrayList); listView.setAdapter(adapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent intent = new Intent(MainActivity.this, SecondActivity.class); intent.putExtra("county_code", "temp"); startActivity(intent); finish(); } });
本人在MainActivity中创建一个ListView,然后想在点击ListView中的一项,然后跳转到一个Activity中,以上是本人写的代码,但是不知道为什么跳转失败,并且本人也已经在AndroidManifest.xml中声明了Activity,麻烦有人知道原因的话,不知道能不能解答下,谢谢了。
解决方案
10
看下日志分析原因哇
5
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add(“one”);
arrayList.add(“two”);
listView = (ListView)findViewById(R.id.list_view);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, arrayList);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra(“county_code”, “temp”);
startActivity(intent);
finish();
} }
});
//本人可以跳转,但是当你退出程序后再重新打开程序的时候,程序会奔溃
arrayList.add(“one”);
arrayList.add(“two”);
listView = (ListView)findViewById(R.id.list_view);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, arrayList);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra(“county_code”, “temp”);
startActivity(intent);
finish();
} }
});
//本人可以跳转,但是当你退出程序后再重新打开程序的时候,程序会奔溃
5
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add(“one”);
arrayList.add(“two”);
listView = (ListView)findViewById(R.id.list_view);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, arrayList);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra(“county_code”, “temp”);
startActivity(intent);
finish();
} }
});
//本人的可以完成跳转,你一定是在别的地方弄错了。
arrayList.add(“one”);
arrayList.add(“two”);
listView = (ListView)findViewById(R.id.list_view);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, arrayList);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra(“county_code”, “temp”);
startActivity(intent);
finish();
} }
});
//本人的可以完成跳转,你一定是在别的地方弄错了。
5
log贴出来看看
5
本人运行了一下,没有可以跳转。你的日志发出来看看?
5
代码没什么问题 看下log
5
目测跳转到的activity 接收数据 位置报空指针
5
代码没什么问题 看下log