MoaLaiSkirulais MoaLaiSkirulais - 5 months ago 11
Android Question

Trying to bind a sound to button click on a ListView, app breaks on start

I am trying to trigger an mp3 sound to buttons clicks inside a listView. The app compiles ok and generate the APK. But when starts breaks.

This is the activity:

MainActivity.java

public class MainActivity extends Activity {

MediaPlayer player;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//string data
String[] myStringArray={
"ANGUS PREMIUM DELUXE",
"ANGUS PREMIUM BACON",
"CUARTO DE LIBRA CON QUESO",
"DOBLE CUARTO DE LIBRA CON QUESO",
"McNÍFICA",
"TRIPLE MAC",
"BIG MAC",
"McFIESTA",
"HAMBURGUESA CON QUESO",
"HAMBURGUESA"
};

//adapter
ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(
this,
// android.R.layout.simple_list_item_1,
// R.layout.mylayout,
R.layout.mylayout2,
R.id.btnAddItem2,
myStringArray);

//asociate
ListView myList =
(ListView) findViewById(R.id.listView);
assert myList != null;
myList.setAdapter(myAdapter);

//sound
Button btnAddItem = (Button) findViewById(R.id.btnAddItem2);

btnAddItem.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
//player = MediaPlayer.create(MainActivity.this, R.raw.snd_add_payment);
//player.start();
}
});

}
}


This is the layout that contain the repetitive row:

mylayout2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >

<Button
android:id="@+id/btnAddItem2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="test" />
</LinearLayout>


log with the crash

--------- beginning of crash
06-19 23:53:30.030 2921-2921/moalaiskirulais.com.arrayadapter E/AndroidRuntime: FATAL EXCEPTION: main
Process: moalaiskirulais.com.arrayadapter, PID: 2921
java.lang.RuntimeException: Unable to start activity ComponentInfo{moalaiskirulais.com.arrayadapter/moalaiskirulais.com.arrayadapter.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at moalaiskirulais.com.arrayadapter.MainActivity.onCreate(MainActivity.java:68)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
06-19 23:53:30.075 1565-2093/system_process W/ActivityManager: Force finishing activity moalaiskirulais.com.arrayadapter/.MainActivity

Answer

You don't have the button btnAddItem2 in your activity_main.xml.
Add this to activity_main.xml:

<include layout="@layout/mylayout2" />
Comments