akkk akkk - 3 months ago 14
Android Question

New activity not opening through ListView Adapter

I want to open new Activity when an item is clicked on listview. But the app crashes when item is clicked. This line gives error

context.startActivity(new Intent(context, MusicPlayer.class));
inside the
getView()
method

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)

Answer

As I mentioned in the comment, your error message contains this:

Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

So, you need to declare the intent first and then set the flag on the intent so:

Intent intent = new Intent(context, MusicPlayer.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);

That should satisfy what the exception is at least.