user3325172
user3325172

Reputation: 47

dont play sound and app force closed

in the following code should worked but instead of play sound when clicked on items listview,app crashed and force closed. In your opinion what should i add or remove from code? Do I have something add to list_item.xml?

this is my code:

public class soundTest extends Activity implements OnItemClickListener {
  private ListView lv1;
  private String lv_arr[]={"test 1","test 2","test 3","test 4","test 5"};

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    lv1=(ListView)findViewById(R.id.ListView01);
    lv1.setAdapter(new ArrayAdapter<String>(this,R.layout.list_item, lv_arr));

    lv1.setOnItemClickListener(this);
  }
    @Override
      public void onItemClick(AdapterView<?> parent, View view,int position, long id) {

        if (lv1.getItemAtPosition(position)=="test 1") {
          MediaPlayer mp = MediaPlayer.create(getApplicationContext(),R.raw.nadealikabir);
          mp.start();
          mp.setOnCompletionListener(new OnCompletionListener() {
            public void onCompletion(MediaPlayer mp) {
              mp.release();
            }
          });
        }

        if (lv1.getItemAtPosition(position)=="test 2") {
          MediaPlayer mp = MediaPlayer.create(getApplicationContext(),R.raw.karimi);
          mp.start();
          mp.setOnCompletionListener(new OnCompletionListener() {
            public void onCompletion(MediaPlayer mp) {
              mp.release();
            }
          });
        }

}
}

and main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main" >
<ListView
    android:id="@+id/ListView01"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" >
</ListView>
</RelativeLayout>

list_item.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >


</RelativeLayout>

and my logcat:

04-02 14:32:02.795: E/AndroidRuntime(1204): FATAL EXCEPTION: main
04-02 14:32:02.795: E/AndroidRuntime(1204): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.soundlistview/com.example.soundlistview.Main}: java.lang.ClassNotFoundException: com.example.soundlistview.Main in loader dalvik.system.PathClassLoader[/data/app/com.example.soundlistview-2.apk]
04-02 14:32:02.795: E/AndroidRuntime(1204):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
04-02 14:32:02.795: E/AndroidRuntime(1204):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-02 14:32:02.795: E/AndroidRuntime(1204):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-02 14:32:02.795: E/AndroidRuntime(1204):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-02 14:32:02.795: E/AndroidRuntime(1204):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-02 14:32:02.795: E/AndroidRuntime(1204):     at android.os.Looper.loop(Looper.java:130)
04-02 14:32:02.795: E/AndroidRuntime(1204):     at android.app.ActivityThread.main(ActivityThread.java:3683)
04-02 14:32:02.795: E/AndroidRuntime(1204):     at java.lang.reflect.Method.invokeNative(Native Method)
04-02 14:32:02.795: E/AndroidRuntime(1204):     at java.lang.reflect.Method.invoke(Method.java:507)
04-02 14:32:02.795: E/AndroidRuntime(1204):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-02 14:32:02.795: E/AndroidRuntime(1204):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-02 14:32:02.795: E/AndroidRuntime(1204):     at dalvik.system.NativeStart.main(Native Method)
04-02 14:32:02.795: E/AndroidRuntime(1204): Caused by: java.lang.ClassNotFoundException: com.example.soundlistview.Main in loader dalvik.system.PathClassLoader[/data/app/com.example.soundlistview-2.apk]
04-02 14:32:02.795: E/AndroidRuntime(1204):     at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
04-02 14:32:02.795: E/AndroidRuntime(1204):     at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
04-02 14:32:02.795: E/AndroidRuntime(1204):     at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
04-02 14:32:02.795: E/AndroidRuntime(1204):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
04-02 14:32:02.795: E/AndroidRuntime(1204):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
04-02 14:32:02.795: E/AndroidRuntime(1204):     ... 11 more

Upvotes: 0

Views: 37

Answers (1)

Hariharan
Hariharan

Reputation: 24853

Try this..

Change instead of == to .equels

if (lv1.getItemAtPosition(position).equels("test 1")) {
      MediaPlayer mp = MediaPlayer.create(getApplicationContext(),R.raw.nadealikabir);
      mp.start();
      mp.setOnCompletionListener(new OnCompletionListener() {
        public void onCompletion(MediaPlayer mp) {
          mp.release();
        }
      });
    }

Upvotes: 1

Related Questions