我在Android中使用ListView
创建此类
public class HBSListView extends ListActivity;
当我点击列表中的项目时,我想转到下一个ListActivity
,显示上一个列表中点击项目的相关详细信息。
lv.setOnItemClickListener( new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
@SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
Intent a = new Intent(iTeam.Ufinder.Application.HBS.HBSDetailView.class.getName());
a.putExtra("store_id", o.get("id"));
startActivity(a);
// When I use above code it is not working. I want to pass ID also.
// This works but i do not know how to pass ID this way.
// startActivity(new Intent("iTeam.Ufinder.Application.HBSDETAILVIEW"));
}
});
public class HBSDetailView extends ListActivity
这是我要移动的课程。
Mainfest文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="iTeam.Ufinder.Application"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Startup"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".main"
android:label="@string/app_name">
<intent-filter>
<action android:name="iTeam.Ufinder.Application.CLEARSCREEN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="iTeam.Ufinder.Application.MANAGEMENT.Management"
android:label="@string/app_name">
<intent-filter>
<action android:name="iTeam.Ufinder.Application.MANAGEMENT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="iTeam.Ufinder.Application.HBS.HBSListView"
android:label="@string/app_name">
<intent-filter>
<action android:name="iTeam.Ufinder.Application.HBSLISTVIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="iTeam.Ufinder.Application.HBS.HBSDetailView"
android:label="@string/app_name">
<intent-filter>
<action android:name="iTeam.Ufinder.Application.HBSDETAILVIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
这是Exception
:
04-15 13:04:43.901:ERROR / AndroidRuntime(1417): java.lang.RuntimeException:无法实例化活动 ComponentInfo {iTeam.Ufinder.Application / iTeam.Ufinder.Application.HBS.HBSDetailView}: 显示java.lang.NullPointerException
答案 0 :(得分:1)
当您像这样创建Intent时
Intent a = new Intent(iTeam.Ufinder.Application.HBS.HBSDetailView.class.getName());
您使用类名的操作名称创建它。你需要像这样创建Intent:
Intent a = new Intent(HBSListView.this, iTeam.Ufinder.Application.HBS.HBSDetailView.class);
区别在于呼叫签名。第一个是String
类型的一个参数。它代表使用指定的操作创建Intent
。第二个是关于Context
和Class
个参数。它用于创建Intent
以在指定的上下文中调用指定的类。
同时检查o
不为空。
修改强>
好吧,如果你想以这种方式开展活动......
Intent a = new Intent("iTeam.Ufinder.Application.HBSDETAILVIEW");
a.putExtra("store_id", o.get("id"));
startActivity(a);
上面的代码必须按照您的预期工作....
答案 1 :(得分:0)
Bundle bundle = new Bundle();
ListAdapter adapter = (new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list1));
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View v,int position, long id) {
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
if (position == 0) {
bundle.putString("store_id", o.get("id"));
Intent inten = new Intent("Activity name which is you must define in manifest file");
inten.putExtras(bundle);
startActivity(inten);
}
if(position==1) {
//write your code for when second item of listview clicked
}
.....
}
});
将您的Activity
添加到清单文件中:
<activity
android:name=".className"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.test.ACTIVITY" />
//use this name when you want to run this activty
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>