我刚刚创建了这个列表视图和一些需要在单击其中一个列表视图项时启动的活动。当我单击第一个Listview项目时,它应该启动"ABActivity",而不是启动应用程序崩溃的活动。有人能告诉我如何解决这个问题吗?
这是我的MainActivity文件:
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....
}
}
});
谢谢。
将这些活动添加到类似的Manifest中
<activity android:name=".ABActivity" />
<activity android:name=".AfrojackActivity" />