Question

I just created this listview and some activities that need to start when clicking one of the listview items. When I click the first Listview item it should start "ABActivity", instead of starting the activity the app crashes. Can some tell me how to fix this problem?

Here is my MainActivity file:

package pcsalt.example.customlistviewdemo;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;


public class MainActivity extends Activity  {

ListView lvDetail;
Context context = MainActivity.this;
ArrayList<ListData> myList = new ArrayList<ListData>();

String[] title = new String[] {
        "Above & Beyond", "Afrojack", "DJ 3", "DJ 4",
        "DJ 5", "DJ 6", "DJ 7", "DJ 8"
};
String[] desc  = new String[] {
        "56:58", "58:49", "60:00", "60:00",
        "60:00", "60:00", "60:00", "60:00"
};
int[]    img   = new int[]    {
        R.drawable.star3, R.drawable.star3, R.drawable.star3, R.drawable.star3,
        R.drawable.star3, R.drawable.star3, R.drawable.star3, R.drawable.star8
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    lvDetail = (ListView) findViewById(R.id.lvCustomList);
    // insert data into the list before setting the adapter
    // otherwise it will generate NullPointerException  - Obviously
    getDataInList();
    lvDetail.setAdapter(new MyBaseAdapter(context, myList));
}

private void getDataInList() {
    for(int i=0;i<8;i++) {
        // Create a new object for each list item
        ListData ld = new ListData();
        ld.setTitle(title[i]);
        ld.setDescription(desc[i]);
        ld.setImgResId(img[i]);
        // Add this object into the ArrayList myList
        myList.add(ld);
    }
    lvDetail.setOnItemClickListener(new OnItemClickListener() 
    {
        @Override
        public void onItemClick(AdapterView<?> a, View v, int position, long id) 
        {
             // In the following line "v" refers to the View returned by the `getView()` method; meaning the clicked View.
             TextView txtName = (TextView)v.findViewById(R.id.lvCustomList);
             String name = txtName.getText().toString();
             switch(name)
             {
                  case "Above & Beyond":
                      Intent intent = new Intent(MainActivity.this, ABActivity.class);
                      startActivity(intent);
                      break;

                  case "Afrojack":
                       Intent intent1 = new Intent(MainActivity.this, AfrojackActivity.class);
                       startActivity(intent1);
                       break;

                  //And so on and so forth....
             }

        }
    });

Thank you.

Was it helpful?

Solution

Add these activities to Manifest like

<activity android:name=".ABActivity" />
<activity android:name=".AfrojackActivity" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top