【发布时间】:2016-09-05 20:03:04
【问题描述】:
我想在列表视图上单击项目时打开新活动。但是单击项目时应用程序崩溃。这一行在getView() 方法中给出了错误context.startActivity(new Intent(context, MusicPlayer.class));
public class ListViewAdapter extends ArrayAdapter {
List list = new ArrayList();
private Context context;
public ListViewAdapter(Context context, int resource) {
super(context, resource);
this.context = context;
}
static class DataHandler {
TextView title;
CheckBox checkBox;
}
@Override
public void add(Object object) {
super.add(object);
list.add(object);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return this.list.get(position);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row;
row = convertView;
final DataHandler handler;
if (convertView == null) {
final SharedPreferences sharedPreferences = getContext().getSharedPreferences("MyBookmark", Context.MODE_PRIVATE);
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.row, parent, false);
handler = new DataHandler();
handler.title = (TextView) row.findViewById(R.id.textView);
handler.checkBox = (CheckBox) row.findViewById(R.id.checkBox);
Boolean check = sharedPreferences.getBoolean(String.valueOf(position), false);
Boolean opened = sharedPreferences.getBoolean(String.valueOf(position) + "selected", false);//if opened = true listview
if (opened == true) {
//row.setBackgroundColor(Color.parseColor("#DCDCDC"));
handler.title.setTextColor(Color.parseColor("#696969"));
}
handler.checkBox.setChecked(check);
handler.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(String.valueOf(position), handler.checkBox.isChecked());
editor.commit();
}
});
row.setTag(handler);
} else {
handler = (DataHandler) row.getTag();
}
DataProvider dataProvider;
dataProvider = (DataProvider) this.getItem(position);
handler.title.setText(dataProvider.get_title());
row.setClickable(true);
row.setFocusable(true);
row.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), handler.title.getText(), Toast.LENGTH_SHORT).show();
handler.title.setTextColor(Color.parseColor("#696969"));
SharedPreferences sharedPreferences = getContext().getSharedPreferences("MyBookmark", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(String.valueOf(position) + "selected", true);
editor.commit();
context.startActivity(new Intent(context, MusicPlayer.class));
}
});
return row;
}
}
Logcat
FATAL EXCEPTION: main Process: com.radioaudio.motivationalaudios, PID: 5307
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
at android.app.ContextImpl.startActivity(ContextImpl.java:672)
at android.app.ContextImpl.startActivity(ContextImpl.java:659)
at android.content.ContextWrapper.startActivity(ContextWrapper.java:331)
at com.radioaudio.motivationalaudios.ListViewAdapter$2.onClick(ListViewAdapter.java:105)
【问题讨论】:
-
尝试在 context.startActivity(new Intent(context, MusicPlayer.class)) 中复制上下文;使用 getApplicationContext()?
-
您的错误消息说您需要在开始新活动之前设置一个标志。
-
@nitinsh99 不起作用
-
@KoltenSturgill 那么解决方案是什么?
-
在 ListView 上使用
setOnItemClickListener。不要从适配器执行此操作
标签: android listview android-adapter