尝试将声音绑定到ListView上的按钮单击,应用程序在启动时中断

时间:2016-06-20 00:46:25

标签: android listview

我试图触发一个mp3声音来按下listView中的按钮。该应用程序编译好并生成APK。但是当开始休息时。

这是活动:

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

    }
}

这是包含重复行的布局:

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>

登录崩溃

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

2 个答案:

答案 0 :(得分:1)

您的btnAddItem2中没有按钮activity_main.xml 将其添加到activity_main.xml

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

答案 1 :(得分:1)

我认为cricket_007是对的。如果要控制列表中每个项目的视图,则必须创建如下自定义类:

<强> CustomArrayAdapter

public class CustomArrayAdapter extends ArrayAdapter<String> {
    private ArrayList<String> yourList; // for set data to list.
    private LayoutInflater inflater = null; // for access your views.
    private int layoutResId;

    public CustomArrayAdapter(Context c, int resId, ArrayList<String> list) {
        super(c, resId);
        layoutResId = resId;
        yourList = list;
        this.inflater = LayoutInflater.from(c);
    }

    @Override
    public int getCount() {
        return yourList.size();
    }

    public void setData(ArrayList<String> list) {
        yourList = list;
    }

    @Override
    public view getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        View view = convertView;
        if (view == null) {
            view = this.inflater.inflate(layoutResId, null); // inflate your custom layout for list.
            holder = new ViewHolder();
            view.setTag(holder);

        } else {
            holder = (ViewHolder)view.getTag();
        }

        holder.button = (Button)view.findViewById(R.id.btnAddItem2);
        holder.button.setText(yourList.get(position).getName());
        holder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // make your button event.
            }
        });

        return view;
    }

    static class ViewHolder {
        private Button button;
    }
}

然后,尝试将此适配器设置为列表视图。

<强> MainActivity

    // custom adapter
    ArrayAdapter<String> myAdapter = new CustomArrayAdapter<String>(
            MainActivity.this,
            R.layout.mylayout2,
            myStringArray);

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