2012-06-21 117 views
0

我有一個自定義列表視圖,我成功地將數據解析到列表中,但是當我嘗試使用onclick應用程序崩潰時,幫助?自定義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 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); 
} 
} 

這裏是logcat的

的logcat:

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

三江源幫助:)

+1

發佈您的logcat – Praveenkumar

+0

我該怎麼做? – SquiresSquire

+0

寧願CustomizedListView.this而不是getApplicationContext() –

回答

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> 

在具有多個活動項目意味着,你已經註冊,像上面。在清單文件

0

我覺得這個部分有問題。從songsList根據位置

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

取值。

感謝

+0

我該怎麼做? – SquiresSquire

+0

我找到了答案:我忘了在android清單中的decleration前面添加文件夾路徑:P – SquiresSquire

2

把第二個活動(SingleMenuItem.java)條目

have you declared this activity in your AndroidManifest.xml? 
2

見logcat的第一行:

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

所以AndroidManifest添加SingleMenuItem活動爲:

<acitivity android:name=".SingleMenuItem"/> 
+0

是的,我做了Decalre但忘記了它是在一個文件夾中,所以它使用主文件夾:P – SquiresSquire