不要播放声音和app力关闭

时间:2014-04-01 10:21:40

标签: android

在下面的代码应该工作但是当点击项目列表视图而不是播放声音,应用程序崩溃并强制关闭。 在您看来,我应该在代码中添加或删除什么? 我有什么东西添加到list_item.xml吗?

这是我的代码:

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

}
}

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

和我的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

1 个答案:

答案 0 :(得分:1)

试试这个..

==更改为.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(); } }); }
{{1}}