android中 利用intent传值的空指针异常问题

fty 2011-04-11 09:56:43
public class listview extends ListActivity {
String sign,name,latitude,longitude,description;
int signValue;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
Intent searchTolistview = getIntent();
signValue = searchTolistview.getIntExtra("sign", signValue);

dataBaseHelper hereami_helper = new dataBaseHelper(listview.this, "hereamiTest");
SQLiteDatabase hereami_sqlite = hereami_helper.getReadableDatabase();
Cursor cursor = hereami_sqlite.query("hereami_table", new String[] {"sign","name" },
"sign=?" , new String[]{""+signValue}, null, null, null);
ArrayList<HashMap<String, String>> hereami_list = new ArrayList<HashMap<String, String>>();

while (cursor.moveToNext()) {
name = cursor.getString(cursor.getColumnIndex("name"));
HashMap<String, String> map = new HashMap<String, String>();
map.put("name", name);
hereami_list.add(map);
}
SimpleAdapter hereami_listAdapter = new SimpleAdapter(this, hereami_list,
R.layout.listview_hashmap, new String[] { "name" },
new int[] {R.id.name });
setListAdapter(hereami_listAdapter);
}

protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
else if (name.equals("jiansheyinhang")) {
Toast.makeText(listview.this, name+" is checked",
Toast.LENGTH_LONG).show();
/*Intent listviewTodescription = new Intent();
listviewTodescription.putExtra("selected_name", "name");
listviewTodescription.setClass(listview.this, description.class);
listview.this.startActivity(listviewTodescription);*/

咱们主要来看最下边的代码(/*...*/中的) 如果点击的名字是jiansheyinhang的话 先输出个Toast 然后定义Intent 把所点击的name值当作放在selected_name中 传递过去 下边看看description.java

public class description extends Activity{

String selected_name;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.description);

Intent listviewTodescription = new Intent();
selected_name = listviewTodescription.getStringExtra("selected_name");
System.out.println(selected_name);
}

}

就是简单的取值 和输出 但是在输出的时候总是说我空指针异常 在输出语句 难道我的值没有取出来?!还是怎么的?

问题补充:

我写了 在开头了 怎么没了
这个程序主要是这样的意思 在listview.java中是查询数据库 把复合条件的放在hashmap中 再把hashmap放在list中 用户点击hashmap实现页面跳转到description.java进行进一步查询 其中显示的内容只有name,在description中我想按照所点击的名字查询对应name的数据库中description内容 那都是后话了 现在我把name 放在selected_name中 都传不过去

PS:我把intent 中传的name的“”去掉了 因为name本身就是String类型的 在description.java中的System.out.println中的selected_name变成了"selected_name" 他直接输出了selected_name这几个字 如果不加双引号 就空指针异常 那我的name到底传过来没有啊?!我输出了selected_name.length()也是空指针异常 看来没传过来 到底什么问题?!
...全文
1284 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
yongyuandeni 2011-04-11
  • 打赏
  • 举报
回复
在description这个类的oncreate()里面先获取Bundle,然后再获取数据值:

Bundle bundle = this.getIntent().getExtras();
String name = bundle.getString("selected_name");
fty 2011-04-11
  • 打赏
  • 举报
回复
啊!!!对啊!我怎么写成这样了!!笨死了!纠结了我一天!!谢谢你啊!!
dinjay 2011-04-11
  • 打赏
  • 举报
回复
Intent listviewTodescription = new Intent();
这句错了
换成
Intent listviewTodescription = getIntent();
应该就好了

80,349

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧