Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

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

Am new in android, trying to go to the next Activity through an Adapter, and i am using onBindViewHolder() with an Intent , below is the code :

@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);

            }
        });



    }

and this is the error which comes :

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 tried out this solution , but the Next Activity doesn't goes back to the previous Activity.

My solution :(But didn't work)

i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Below is the entire Adapter code:

    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();
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The Context that you pass into the CurrencySelectorAdapter constructor should be the Activity that hosts this RecyclerView. Then, you will not get this error.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...