新活动无法通过ListView适配器打开

时间:2016-09-05 20:03:04

标签: android listview android-adapter

我想在listview上点击某个项目时打开新的Activity。但是当点击项目时应用程序崩溃了。此行在context.startActivity(new Intent(context, MusicPlayer.class));方法

中提供错误getView()
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)

3 个答案:

答案 0 :(得分:1)

正如我在评论中提到的,您的错误消息包含:

  

从Activity上下文外部调用startActivity()需要FLAG_ACTIVITY_NEW_TASK标志。这真的是你想要的吗?

因此,您需要先声明intent,然后在intent上设置标志:

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

这应该至少满足异常。

答案 1 :(得分:0)

将click事件分配给活动中的ListView,而不是适配器中的视图。

答案 2 :(得分:0)

也许这会有所帮助:

Intent myIntent = new Intent(v.getContext(), MusicPlayer.class);
v.getContext().startActivity(myIntent);