2017-01-23 44 views
0

難道Android新功能,試圖通過一個適配器去到下一個活動,我使用onBindViewHolder()Intent,下面是代碼:從Activity上下文外部調用startActivity()需要FLAG_ACTIVITY_NEW_TASK標誌。這真的是你想要的嗎?

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

我試過這個解決方案,但Next Activity不回到前面的Activity。

我的解決辦法:(但沒有工作)

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(); 
    } 
} 
+0

發佈整個適配器代碼 – OBX

+0

我已經編輯我的代碼@Superman –

回答

3

Context您傳遞到CurrencySelectorAdapter的構造函數應該是Activity,它託管着這個RecyclerView。然後,你不會得到這個錯誤。

+0

,怎麼做完這些 –

+0

@FauziaNito:嗯,你有'新CurrencySelectorAdapter()'在你的代碼,確保第二個參數是你的活動。 – CommonsWare

+0

你是**意圖中的**,請不要讓你? –

1

替換getApplicationContext()this對我有幫助。

比如我有:

adapter= new CustomAdapter(dataModels,getApplicationContext()); 
在我的活動文件

將其更改爲前:

adapter= new CustomAdapter(dataModels,this); 

和固定的錯誤。

相關問題