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

时间:2017-01-23 15:31:18

标签: java android android-intent

我是android新手,尝试通过适配器转到下一个Activity,我使用onBindViewHolder() Intent,下面是代码:

@Override
    public void onBindViewHolder(CurrencyViewHolder holder, int position) {



        final  BureauRateObject br = itemList.get(position);
        holder.bureauname.setText(br.getBureau_name());
        holder.rates.setText(br.getBuysell());

        final String BureauId = br.getBureau_id();
        final String BureauName = br.getBureau_name();




        holder.root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

               Intent i = new Intent(context, SingleForexActivity.class);
               // i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                i.putExtra("bureau_id", BureauId);
                i.putExtra("bureau_name",BureauName);
                context.startActivity(i);

            }
        });



    }

这就是错误:

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:677)
                      at android.app.ContextImpl.startActivity(ContextImpl.java:664)
                      at android.content.ContextWrapper.startActivity(ContextWrapper.java:331)
                      at Adapters.CurrencySelectorAdapter$1.onClick(CurrencySelectorAdapter.java:64)
                      at android.view.View.performClick(View.java:5265)
                      at android.view.View$PerformClick.run(View.java:21534)
                      at android.os.Handler.handleCallback(Handler.java:815)
                      at android.os.Handler.dispatchMessage(Handler.java:104)
                      at android.os.Looper.loop(Looper.java:207)
                      at android.app.ActivityThread.main(ActivityThread.java:5728)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)

我尝试了这个解决方案,但是下一个活动不会回到之前的活动。

我的解决方案:(但没有效果)

i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

以下是整个适配器代码:

    package Adapters;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.naavanito.eamobileforex2.R;
import com.example.naavanito.eamobileforex2.SingleForexActivity;

import java.util.Currency;
import java.util.List;

import Holders.CurrencyViewHolder;
import model.BureauRateObject;

/**
 * Created by Admin on 1/21/2017.
 */

public class CurrencySelectorAdapter extends RecyclerView.Adapter<CurrencyViewHolder> {

    private List<BureauRateObject> itemList;
    private Context context;

    public CurrencySelectorAdapter(List<BureauRateObject> itemList, Context context) {
        this.itemList = itemList;
        this.context = context;
    }

    @Override
    public CurrencyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View layoutview = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_currency_rate,parent,false);
        CurrencyViewHolder rcv = new CurrencyViewHolder(layoutview,context);
        return rcv;
    }

    @Override
    public void onBindViewHolder(CurrencyViewHolder holder, int position) {



        final  BureauRateObject br = itemList.get(position);
        holder.bureauname.setText(br.getBureau_name());
        holder.rates.setText(br.getBuysell());

        final String BureauId = br.getBureau_id();
        final String BureauName = br.getBureau_name();




        holder.root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

               Intent i = new Intent(context, SingleForexActivity.class);
               // i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                i.putExtra("bureau_id", BureauId);
                i.putExtra("bureau_name",BureauName);
                context.startActivity(i);

            }
        });



    }

    @Override
    public int getItemCount() {
        return itemList.size();
    }
}

3 个答案:

答案 0 :(得分:4)

您传递到Context构造函数的CurrencySelectorAdapter应该是托管此Activity的{​​{1}}。然后,你不会得到这个错误。

答案 1 :(得分:2)

在我的案例中,用getApplicationContext()替换this

例如,我有:

adapter= new CustomAdapter(dataModels,getApplicationContext());

在我的活动文件中,然后将其更改为:

adapter= new CustomAdapter(dataModels,this);

并修正了错误。

答案 2 :(得分:0)

出现此类问题的另一个原因是在使用 rootView 上下文时

示例

val view = window.decorView.rootView


view.context.startActivity(intent)

对于那些出于某种原因这样做的人,例如将视图及其上下文用于单例类。您应该使用 xml 中的父视图而不是依赖 rootview 来避免这种行为。