【问题标题】:New activity not opening through ListView Adapter新活动未通过 ListView 适配器打开
【发布时间】: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


【解决方案1】:

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

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

所以,你需要先声明意图,然后在意图上设置标志:

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

至少应该满足异常的情况。

【讨论】:

    【解决方案2】:

    将click事件分配给activity中的ListView,而不是adapter中的view。

    【讨论】:

      【解决方案3】:

      也许这会有所帮助:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-04-13
        • 1970-01-01
        • 1970-01-01
        • 2013-09-10
        • 1970-01-01
        • 2015-08-04
        • 1970-01-01
        相关资源
        最近更新 更多