繁体   English   中英

Android FLAG_ACTIVITY_NEW_TASK无效

[英]Android FLAG_ACTIVITY_NEW_TASK not working

我在自定义onClick侦听器中调用Intent上的startActivity()时出现问题。 代码编译得很好,但是当它在执行中到达那一点时,我得到一个nullPointerException。

private static class ProverbAdapter extends ArrayAdapter<String> {
    MainActivity ma;
    public ProverbAdapter(Context context, int layout, int resId, String[] items) {
        super(context, layout, resId, items);
        this.ma = new MainActivity();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        Context context = getContext();
        if(row == null) {
            row = LayoutInflater.from(context).inflate(R.layout.proverb_layout, parent, false);
        }

        String item = getItem(position);
        TextView proverbDate = (TextView)row.findViewById(R.id.proverb_date);
        TextView proverbText = (TextView)row.findViewById(R.id.proverb_content);
        String[] data = item.split("----");
        proverbDate.setText(data[0]);
        proverbText.setText(data[1]);

        ImageButton emailButton = (ImageButton)row.findViewById(R.id.emailButton);
        ImageButton twitterButton = (ImageButton)row.findViewById(R.id.twitterButton);
        ImageButton facebookButton = (ImageButton)row.findViewById(R.id.facebookButton);

        emailButton.setOnClickListener(this.ma.new ProverbOnClickListener(data[0], data[1], "email", context));
        twitterButton.setOnClickListener(this.ma.new ProverbOnClickListener(data[0], data[1], "twitter", context));
        facebookButton.setOnClickListener(this.ma.new ProverbOnClickListener(data[0], data[1], "facebook", context));

        return row;
    }
}

public class ProverbOnClickListener implements OnClickListener {

     String proverb_date, proverb_content, eventType;
     Context context;

     public ProverbOnClickListener(String proverb_date, String proverb_content, String eventType, Context context) {
          this.proverb_date = proverb_date;
          this.proverb_content = proverb_content;
          this.eventType = eventType;
          this.context = context;
     }

     @Override
     public void onClick(View v) {
         Toast.makeText(this.context, this.eventType + ": " + this.proverb_content, Toast.LENGTH_SHORT).show();
         if(this.eventType == "email") {
             Intent i = new Intent(Intent.ACTION_SEND);
                     i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
             i.setType("message/rfc822");
             i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"recipient@example.com"});
             i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
             i.putExtra(Intent.EXTRA_TEXT   , "body of email");
             try {
                 context.startActivity(Intent.createChooser(i, "Send mail..."));
             } catch (android.content.ActivityNotFoundException ex) {
                 Toast.makeText(this.context, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
             }
         }
     }

  }

例外

致命异常:主要android.util.AndroidRuntimeException:从Activity上下文外部调用startActivity()需要FLAG_ACTIVITY_NEW_TASK标志。 这真的是你想要的吗?

我在编译器建议的时候添加了新标志,但它仍然会因同样的错误而崩溃。

* * 编辑 * ***

在第一个响应中遵循建议后,我的适配器现在看起来像:

private static class ProverbAdapter extends ArrayAdapter<String> {
    Context context;
    public ProverbAdapter(Context context, int layout, int resId, String[] items) {
        super(context, layout, resId, items);
        this.context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        if(row == null) {
            LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = li.inflate(R.layout.proverb_layout, parent, false);
        }

        String item = getItem(position);
        TextView proverbDate = (TextView)row.findViewById(R.id.proverb_date);
        TextView proverbText = (TextView)row.findViewById(R.id.proverb_content);
        String[] data = item.split("----");
        proverbDate.setText(data[0]);
        proverbText.setText(data[1]);

        ImageButton emailButton = (ImageButton)row.findViewById(R.id.emailButton);
        ImageButton twitterButton = (ImageButton)row.findViewById(R.id.twitterButton);
        ImageButton facebookButton = (ImageButton)row.findViewById(R.id.facebookButton);

        emailButton.setOnClickListener(new ProverbOnClickListener(data[0], data[1], "email", context));
        twitterButton.setOnClickListener(new ProverbOnClickListener(data[0], data[1], "twitter", context));
        facebookButton.setOnClickListener(new ProverbOnClickListener(data[0], data[1], "facebook", context));

        return row;
    }
}

但是我收到了编译错误。

不能访问封闭的实例类型的MainActivity。

尝试很少修改

this.ma = new MainActivity(); //it is wrong

不要实例化Activity

试试这样

 Context context;
    public ProverbAdapter(Context context, int layout, int resId, String[] items) {
        super(context, layout, resId, items);
       this.context=context;
    }

在需要上下文的地方使用此变量上下文

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM