Android 自定义ListView OnClick解析
我有一个自定义的listview,我成功地将数据解析成一个列表,但是当我尝试使用onclick时,应用程序崩溃,帮助 主要活动Android 自定义ListView OnClick解析,android,onclick,custom-lists,Android,Onclick,Custom Lists,我有一个自定义的listview,我成功地将数据解析成一个列表,但是当我尝试使用onclick时,应用程序崩溃,帮助 主要活动 public class CustomizedListView extends Activity { // All static variables static final String URL = "http://dl.dropbox.com/u/48258247/music.xml"; // XML node keys static final String KE
public class CustomizedListView extends Activity {
// All static variables
static final String URL = "http://dl.dropbox.com/u/48258247/music.xml";
// XML node keys
static final String KEY_SONG = "song"; // parent node
static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_ARTIST = "artist";
static final String KEY_DURATION = "duration";
static final String KEY_THUMB_URL = "thumb_url";
ListView list;
LazyAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.library);
ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML from URL
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_SONG);
// looping through all song nodes <song>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
// adding HashList to ArrayList
songsList.add(map);
}
list=(ListView)findViewById(R.id.list);
// Getting adapter by passing xml data ArrayList
adapter=new LazyAdapter(this, songsList);
list.setAdapter(adapter);
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String song = ((TextView) view.findViewById(R.id.title)).getText().toString();
String artist = ((TextView) view.findViewById(R.id.artist)).getText().toString();
String thumb_url = ((TextView) view.findViewById(R.id.duration)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItem.class);
in.putExtra(KEY_SONG, song);
in.putExtra(KEY_ARTIST, artist);
in.putExtra(KEY_THUMB_URL, thumb_url);
startActivity(in);
}
});
}
}
这是航海日志
日志:
-21 17:16:30.812: D/AndroidRuntime(281): Shutting down VM
06-21 17:16:30.812: W/dalvikvm(281): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-21 17:16:30.862: E/AndroidRuntime(281): FATAL EXCEPTION: main
06-21 17:16:30.862: E/AndroidRuntime(281): android.content.ActivityNotFoundException: Unable to find explicit activity class {org.scouts.android/com.example.androidhive.SingleMenuItem}; have you declared this activity in your AndroidManifest.xml?
06-21 17:16:30.862: E/AndroidRuntime(281): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
06-21 17:16:30.862: E/AndroidRuntime(281): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
06-21 17:16:30.862: E/AndroidRuntime(281): at android.app.Activity.startActivityFromChild(Activity.java:3057)
06-21 17:16:30.862: E/AndroidRuntime(281): at android.app.Activity.startActivityForResult(Activity.java:2837)
06-21 17:16:30.862: E/AndroidRuntime(281): at android.app.Activity.startActivity(Activity.java:2923)
06-21 17:16:30.862: E/AndroidRuntime(281): at com.example.androidhive.CustomizedListView$1.onItemClick(CustomizedListView.java:88)
06-21 17:16:30.862: E/AndroidRuntime(281): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
06-21 17:16:30.862: E/AndroidRuntime(281): at android.widget.ListView.performItemClick(ListView.java:3382)
06-21 17:16:30.862: E/AndroidRuntime(281): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
06-21 17:16:30.862: E/AndroidRuntime(281): at android.os.Handler.handleCallback(Handler.java:587)
06-21 17:16:30.862: E/AndroidRuntime(281): at android.os.Handler.dispatchMessage(Handler.java:92)
06-21 17:16:30.862: E/AndroidRuntime(281): at android.os.Looper.loop(Looper.java:123)
06-21 17:16:30.862: E/AndroidRuntime(281): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-21 17:16:30.862: E/AndroidRuntime(281): at java.lang.reflect.Method.invokeNative(Native Method)
06-21 17:16:30.862: E/AndroidRuntime(281): at java.lang.reflect.Method.invoke(Method.java:521)
06-21 17:16:30.862: E/AndroidRuntime(281): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-21 17:16:30.862: E/AndroidRuntime(281): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-21 17:16:30.862: E/AndroidRuntime(281): at dalvik.system.NativeStart.main(Native Method)
谢谢你的帮助:)我觉得这部分有问题
String song = ((TextView) view.findViewById(R.id.title)).getText().toString();
String artist = ((TextView) view.findViewById(R.id.artist)).getText().toString();
String thumb_url = ((TextView) view.findViewById(R.id.duration)).getText().toString();
根据位置从歌曲列表中获取值
谢谢我觉得这部分有问题
String song = ((TextView) view.findViewById(R.id.title)).getText().toString();
String artist = ((TextView) view.findViewById(R.id.artist)).getText().toString();
String thumb_url = ((TextView) view.findViewById(R.id.duration)).getText().toString();
根据位置从歌曲列表中获取值
感谢由于
SingleMenuItem.java
尚未在您的AndroidManifest.xml
文件中注册,因此发生了强制关闭。只需在
标签下面这样注册即可
<application
android:icon="@drawable/op"
android:label="@string/app_name" >
<activity
android:name=".CustomizedListView"
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=".SingleMenuItem" />
</application>
在您的项目中,有多个活动意味着您必须像上面那样注册。强制关闭是因为
SingleMenuItem.java
尚未在您的AndroidManifest.xml
文件中注册。只需在
标签下面这样注册即可
<application
android:icon="@drawable/op"
android:label="@string/app_name" >
<activity
android:name=".CustomizedListView"
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=".SingleMenuItem" />
</application>
在您的项目中,有多个活动意味着,您必须像上面那样进行注册。将清单文件中的第二个活动(SingleMenuItem.java)项作为
have you declared this activity in your AndroidManifest.xml?
将第二个活动(SingleMenuItem.java)条目作为
have you declared this activity in your AndroidManifest.xml?
请参阅logcat第一行:
{org.scouts.android/com.example.androidhive.SingleMenuItem}; have you declared this activity in your AndroidManifest.xml?
因此,将您的SingleMenuItem
活动添加到AndroidManifest
中,如下所示:
<acitivity android:name=".SingleMenuItem"/>
参见logcat第一行:
{org.scouts.android/com.example.androidhive.SingleMenuItem}; have you declared this activity in your AndroidManifest.xml?
因此,将您的SingleMenuItem
活动添加到AndroidManifest
中,如下所示:
<acitivity android:name=".SingleMenuItem"/>
首选CustomizedListView。此选项而不是getApplicationContext()。如果您需要解决方案,请接受答案,否则问题将移至未回答列表!!首选CustomizedListView.this而不是getApplicationContext()。如果您需要解决方案,请接受答案,否则问题将移至未回答列表!!我找到了答案:我忘了在android清单中删除之前添加文件夹路径:PI找到了答案:我忘了在android清单中删除之前添加文件夹路径:Pyeah我做了贴花,但忘了它在文件夹中,所以它在使用主文件夹:Pyeah我做了贴花,但忘了它在文件夹中,所以它是这样的使用主文件夹:P