0

我有一个自定义列表视图,我成功地将数据解析为一个列表,但是当我尝试使用 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 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);

        }
    });     
}   
}

SingleMenuItemActivity

package com.example.androidhive;

import org.scouts.android.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class SingleMenuItem  extends Activity {

// XML node keys
static final String KEY_SONG = "song";
static final String KEY_ARTIST = "artist";
static final String KEY_THUMB_URL = "thumb_url";
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.libraryonclick);

    // getting intent data
    Intent in = getIntent();

    // Get XML values from previous intent
    String song = in.getStringExtra(KEY_SONG);
    String artist = in.getStringExtra(KEY_ARTIST);
    String thumb_url = in.getStringExtra(KEY_THUMB_URL);

    // Displaying all values on the screen
    TextView lblSong = (TextView) findViewById(R.id.textView1);
    TextView lblArtist = (TextView) findViewById(R.id.textView2);
    TextView lblThumb_Url = (TextView) findViewById(R.id.textView3);

    lblSong.setText(song);
    lblArtist.setText(artist);
    lblThumb_Url.setText(thumb_url);
}
}

这是日志猫

日志猫:

-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)

感谢您的帮助 :)

4

4 回答 4

2

由于SingleMenuItem.java尚未在您的AndroidManifest.xml文件中注册,因此发生了强制关闭。只需在您的<application>标签下方注册即可

    <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>

在您的项目中有多个活动意味着,您必须像上面一样注册。

于 2012-06-21T07:21:20.833 回答
2

将清单文件中的第二个活动 (SingleMenuItem.java) 条目作为

 have you declared this activity   in your AndroidManifest.xml?
于 2012-06-21T07:21:52.693 回答
2

查看 logcat 第一行:

{org.scouts.android/com.example.androidhive.SingleMenuItem}; have you declared this activity   in your AndroidManifest.xml?

因此,将您的SingleMenuItem活动添加AndroidManifest为:

<acitivity android:name=".SingleMenuItem"/>
于 2012-06-21T07:22:11.043 回答
0

我认为这部分有问题。

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();

songsList根据位置取值。

谢谢

于 2012-06-21T07:16:16.153 回答